Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better poshgit install and upgrade experience #48

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
210 changes: 210 additions & 0 deletions poshgit/tests/InstallChocolatey.Tests.ps1
@@ -0,0 +1,210 @@
$packageName = "poshgit"
cpack

function Setup-Environment {
Cleanup
$env:poshGit = join-path (Resolve-Path .\Tests ) dahlbyk-posh-git-60be436.zip
$profileScript = "function Prompt(){ `$host.ui.RawUI.WindowTitle = `"My Prompt`" }"
(Set-Content $Profile -value $profileScript -Force)
}

function Cleanup {
Clean-Temp
Remove-Item $env:ChocolateyInstall\lib\$packageName* -Recurse -Force
}

function Clean-Temp {
if(Test-Path $env:Temp\Chocolatey\$packageName) {Remove-Item $env:Temp\Chocolatey\$packageName -Recurse -Force}
}

function RunInstall {
cinst $packageName -source (Resolve-Path .)
}
$binRoot = join-path $env:systemdrive 'tools'
if($env:chocolatey_bin_root -ne $null){$binRoot = join-path $env:systemdrive $env:chocolatey_bin_root}
$poshgitPath = join-path $binRoot 'poshgit'
if(Test-Path $Profile) { $currentProfileScript = (Get-Content $Profile) }

function Clean-Environment {
Set-Content $Profile -value $currentProfileScript -Force
}

Describe "Install-Posh-Git" {

It "WillRemvePreviousInstallVersion" {
Setup-Environment
try{
Add-Content $profile -value ". '$poshgitPath\posh-git\profile.example.ps1'"

RunInstall

$newProfile = (Get-Content $Profile)
$pgitDir = [Array](Dir "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime)[-1]
($newProfile -like ". '$poshgitPath\posh-git\profile.example.ps1'").Count.should.be(0)
($newProfile -like ". '$pgitDir\profile.example.ps1'").Count.should.be(1)
}
catch {
write-host (Get-Content $Profile)
throw
}
finally {Clean-Environment}
}

It "WillNotAddDuplicateCallOnRepeatInstall" {
Setup-Environment
try{
RunInstall
Cleanup

RunInstall

$newProfile = (Get-Content $Profile)
$pgitDir = [Array](Dir "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime)[-1]
($newProfile -like ". '$pgitDir\profile.example.ps1'").Count.should.be(1)
}
catch {
write-host (Get-Content $Profile)
throw
}
finally {Clean-Environment}
}

It "WillPreserveOldPromptLogic" {
Setup-Environment
try{
RunInstall
. $Profile
$host.ui.RawUI.WindowTitle = "bad"

Prompt

$host.ui.RawUI.WindowTitle.should.be("My Prompt")
}
catch {
write-host (Get-Content function:\prompt)
throw
}
finally {
Clean-Environment
}
}

It "WillOutputVcsStatus" {
Setup-Environment
try{
RunInstall
mkdir PoshTest
Pushd PoshTest
git init
. $Profile
$global:wh=""
New-Item function:\global:Write-Host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) try{Write-Output `$object;[string]`$global:wh += `$object.ToString()} catch{}"

Prompt

Popd
$wh.should.be("$pwd\PoshTest [master]")
}
catch {
write-output (Get-Content $Profile)
throw
}
finally {
Clean-Environment
if( Test-Path function:\Write-Host ) {Remove-Item function:\Write-Host}
if( Test-Path PoshTest ) {Remove-Item PoshTest -Force -Recurse}
}
}

It "WillSucceedOnEmptyProfile" {
Setup-Environment
try{
Remove-Item $Profile -Force
RunInstall
mkdir PoshTest
Pushd PoshTest
git init
. $Profile
$global:wh=""
New-Item function:\global:Write-Host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) try{Write-Output `$object;[string]`$global:wh += `$object.ToString()} catch{}"

Prompt

Popd
$wh.should.be("$pwd\PoshTest [master]")
}
catch {
write-output (Get-Content $Profile)
throw
}
finally {
Clean-Environment
if( Test-Path function:\Write-Host ) {Remove-Item function:\Write-Host}
if( Test-Path PoshTest ) {Remove-Item PoshTest -Force -Recurse}
}
}

It "WillSucceedOnProfileWithPromptWithWriteHost" {
Cleanup
Setup-Environment
try{
Remove-Item $Profile -Force
Add-Content $profile -value "function prompt {Write-Host 'Hi'}" -Force
RunInstall
mkdir PoshTest
Pushd PoshTest
git init
. $Profile
$global:wh=""
New-Item function:\global:Write-Host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) try{Write-Output `$object;[string]`$global:wh += `$object.ToString()} catch{}"

Prompt

Remove-Item function:\global:Write-Host
Popd
$wh.should.be("$pwd\PoshTest [master]")
}
catch {
write-output (Get-Content $Profile)
throw
}
finally {
Clean-Environment
if( Test-Path function:\Write-Host ) {Remove-Item function:\Write-Host}
if( Test-Path PoshTest ) {Remove-Item PoshTest -Force -Recurse}
}
}

It "WillSucceedOnUpdatingFrom040" {
Cleanup
Setup-Environment
try{
Remove-Item $Profile -Force
Add-Content $profile -value ". 'C:\tools\poshgit\dahlbyk-posh-git-60be436\profile.example.ps1'" -Force
RunInstall
mkdir PoshTest
Pushd PoshTest
git init
write-output (Get-Content function:\prompt)
. $Profile
$global:wh=""
New-Item function:\global:Write-Host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) try{Write-Output `$object;[string]`$global:wh += `$object.ToString()} catch{}"

Prompt

Remove-Item function:\global:Write-Host
Popd
$wh.should.be("$pwd\PoshTest [master]")
}
catch {
write-output (Get-Content $Profile)
throw
}
finally {
Clean-Environment
if( Test-Path function:\Write-Host ) {Remove-Item function:\Write-Host}
if( Test-Path PoshTest ) {Remove-Item PoshTest -Force -Recurse}
}
}

}
Binary file added poshgit/tests/dahlbyk-posh-git-60be436.zip
Binary file not shown.
43 changes: 39 additions & 4 deletions poshgit/tools/chocolateyInstall.ps1
@@ -1,19 +1,54 @@
function Insert-Script([ref]$originalScript, $script) {
if(!($originalScript.Value -Contains $script)) { $originalScript.Value += $script }
}

try {
$binRoot = join-path $env:systemdrive 'tools'

$oldPromptOverride = "if(Test-Path Function:\Prompt) {Rename-Item Function:\Prompt PrePoshGitPrompt -Force}"
$newPromptOverride = "function Prompt() {if(Test-Path Function:\PrePoshGitPrompt){++`$global:poshScope; New-Item function:\script:Write-host -value `"param([object] ```$object, ```$backgroundColor, ```$foregroundColor, [switch] ```$nonewline) `" -Force | Out-Null;`$private:p = PrePoshGitPrompt; if(--`$global:poshScope -eq 0) {Remove-Item function:\Write-Host -Force}}PoshGitPrompt}"
### Using an environment variable to to define the bin root until we implement YAML configuration ###
if($env:chocolatey_bin_root -ne $null){$binRoot = join-path $env:systemdrive $env:chocolatey_bin_root}
$poshgitPath = join-path $binRoot 'poshgit'
$poshGitInstall = if($env:poshGit -ne $null){ $env:poshGit } else {'https://github.com/dahlbyk/posh-git/zipball/v0.4'}
Install-ChocolateyZipPackage 'poshgit' $poshGitInstall $poshgitPath
$pgitDir = [Array](Dir "$poshgitPath\*posh-git*\" | Sort-Object -Property LastWriteTime)[-1]

if(Test-Path $PROFILE) {
$oldProfile = [string[]](Get-Content $PROFILE)
$newProfile = @()
#If old profile exists replace with new one and make sure prompt preservation function is on top
$pgitExample = "$pgitDir\profile.example.ps1"
foreach($line in $oldProfile) {
if($line.ToLower().Contains("$poshgitPath".ToLower())) {
Insert-Script ([REF]$newProfile) $oldPromptOverride
$line = ". '$pgitExample'"
}
if($line.Trim().Length -gt 0) { $newProfile += $line }
}
#Save any previous Prompt logic
Insert-Script ([REF]$newProfile) $oldPromptOverride
Set-Content -path $profile -value $newProfile -Force
}

Install-ChocolateyZipPackage 'poshgit' 'https://github.com/dahlbyk/posh-git/zipball/v0.4' $poshgitPath

#------- ADDITIONAL SETUP -------#
$installer = Join-Path $poshgitPath 'dahlbyk-posh-git-60be436'
$installer = Join-Path $installer 'install.ps1'
$installer = (Get-Item "$pgitDir\install.ps1")
& $installer

$newProfile = [string[]](Get-Content $PROFILE)
Insert-Script ([REF]$newProfile) "Rename-Item Function:\Prompt PoshGitPrompt -Force"

#function that will run previous prompt logic and then the poshgit logic
#all output from previous prompts will be swallowed
Insert-Script ([REF]$newProfile) $newPromptOverride
Set-Content -path $profile -value $newProfile -Force

Write-ChocolateySuccess 'poshgit'
} catch {
try {
if($oldProfile){ Set-Content -path $PROFILE -value $oldProfile -Force }
}
catch{}
Write-ChocolateyFailure 'poshgit' $($_.Exception.Message)
throw
}