chocolatey/tests/InstallChocolatey.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
$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}
        }
    }

}