-
-
Notifications
You must be signed in to change notification settings - Fork 811
/
Copy pathTabExpansion.Tests.ps1
338 lines (299 loc) · 14.5 KB
/
TabExpansion.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
BeforeAll {
. $PSScriptRoot\Shared.ps1
}
Describe 'TabExpansion function test' -Skip:($PSVersionTable.PSVersion.Major -gt 5) {
It 'Windows PowerShell v5 exports a TabExpansion function' {
$module.ExportedFunctions.Keys -contains 'TabExpansion' | Should -Be $true
}
}
Describe 'TabExpansion Tests' {
Context 'Subcommand TabExpansion Tests' {
It 'Tab completes without subcommands' {
$result = & $module GitTabExpansionInternal 'git whatever '
$result | Should -Be @()
}
It 'Tab completes bisect subcommands' {
$result = & $module GitTabExpansionInternal 'git bisect '
$result -contains '' | Should -Be $false
$result -contains 'start' | Should -Be $true
$result -contains 'run' | Should -Be $true
$result2 = & $module GitTabExpansionInternal 'git bisect s'
$result2 -contains 'start' | Should -Be $true
$result2 -contains 'skip' | Should -Be $true
}
It 'Tab completes remote subcommands' {
$result = & $module GitTabExpansionInternal 'git remote '
$result -contains '' | Should -Be $false
$result -contains 'add' | Should -Be $true
$result -contains 'set-branches' | Should -Be $true
$result -contains 'get-url' | Should -Be $true
$result -contains 'update' | Should -Be $true
$result2 = & $module GitTabExpansionInternal 'git remote s'
$result2 -contains 'set-branches' | Should -Be $true
$result2 -contains 'set-head' | Should -Be $true
$result2 -contains 'set-url' | Should -Be $true
}
It 'Tab completes update-git-for-windows only on Windows' {
$result = & $module GitTabExpansionInternal 'git update-'
if ((($PSVersionTable.PSVersion.Major -eq 5) -or $IsWindows)) {
$result -contains '' | Should -Be $false
$result -contains 'update-git-for-windows' | Should -Be $true
}
else {
$result | Should -BeNullOrEmpty
}
}
}
Context 'Fetch/Push/Pull TabExpansion Tests' {
BeforeEach {
# Ensure master branch exists
&$gitbin branch -q master 2>$null
# Ensure an origin remote exists
&$gitbin remote add origin . 2>$null
# Ensure origin/master exists
&$gitbin update-ref refs/remotes/origin/master $(git rev-parse master) 2>$null
# Ensure origin/HEAD exists
&$gitbin symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master 2>$null
}
It 'Tab completes all remotes' {
(&$gitbin remote) -contains 'origin' | Should -Be $true
$result = & $module GitTabExpansionInternal 'git push '
$result -contains 'origin' | Should -Be $true
}
It 'Tab completes all branches' {
$result = & $module GitTabExpansionInternal 'git push origin '
$result -contains 'master' | Should -Be $true
$result -contains 'origin/master' | Should -Be $true
$result -contains 'origin/HEAD' | Should -Be $true
}
It 'Tab completes all :branches' {
$result = & $module GitTabExpansionInternal 'git push origin :'
$result -contains ':master' | Should -Be $true
}
It 'Tab completes matching remotes' {
$result = & $module GitTabExpansionInternal 'git push or'
$result | Should -BeExactly 'origin'
}
It 'Tab completes matching branches' {
$result = & $module GitTabExpansionInternal 'git push origin ma'
$result | Should -BeExactly 'master'
}
It 'Tab completes matching remote/branches' {
$result = & $module GitTabExpansionInternal 'git push origin origin/ma'
$result | Should -BeExactly 'origin/master'
}
It 'Tab completes matching :branches' {
$result = & $module GitTabExpansionInternal 'git push origin :ma'
$result | Should -BeExactly ':master'
}
It 'Tab completes matching ref:branches' {
$result = & $module GitTabExpansionInternal 'git push origin HEAD:ma'
$result | Should -BeExactly 'HEAD:master'
}
It 'Tab completes matching +ref:branches' {
$result = & $module GitTabExpansionInternal 'git push origin +HEAD:ma'
$result | Should -BeExactly '+HEAD:master'
}
It 'Tab completes matching remote with preceding parameters' {
$result = & $module GitTabExpansionInternal 'git push --follow-tags -u or'
$result | Should -BeExactly 'origin'
}
It 'Tab completes all branches with preceding parameters' {
$result = & $module GitTabExpansionInternal 'git push --follow-tags -u origin '
$result -contains 'master' | Should -Be $true
$result -contains 'origin/master' | Should -Be $true
$result -contains 'origin/HEAD' | Should -Be $true
}
It 'Tab completes matching branch with preceding parameters' {
$result = & $module GitTabExpansionInternal 'git push --follow-tags -u origin ma'
$result | Should -BeExactly 'master'
}
It 'Tab completes matching branch with intermixed parameters' {
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags ma'
$result | Should -BeExactly 'master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags ma'
$result | Should -BeExactly 'master'
}
It 'Tab completes matching ref:branch with intermixed parameters' {
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags HEAD:ma'
$result | Should -BeExactly 'HEAD:master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags +HEAD:ma'
$result | Should -BeExactly '+HEAD:master'
}
It 'Tab completes matching multiple push ref specs with intermixed parameters' {
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four ma'
$result | Should -BeExactly 'master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four --crazy-param ma'
$result | Should -BeExactly 'master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four HEAD:ma'
$result | Should -BeExactly 'HEAD:master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four --crazy-param HEAD:ma'
$result | Should -BeExactly 'HEAD:master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four +ma'
$result | Should -BeExactly '+master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four --crazy-param +ma'
$result | Should -BeExactly '+master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four +HEAD:ma'
$result | Should -BeExactly '+HEAD:master'
$result = & $module GitTabExpansionInternal 'git push -u origin --follow-tags one :two three:four --crazy-param +HEAD:ma'
$result | Should -BeExactly '+HEAD:master'
}
It 'Tab complete returns empty result for missing remote' {
$result = & $module GitTabExpansionInternal 'git push zy'
$result | Should -BeNullOrEmpty
}
It 'Tab complete returns empty result for missing branch' {
$result = & $module GitTabExpansionInternal 'git push origin zy'
$result | Should -BeNullOrEmpty
}
It 'Tab complete returns empty result for missing remotebranch' {
$result = & $module GitTabExpansionInternal 'git fetch origin/zy'
$result | Should -BeNullOrEmpty
}
It 'Tab completes branch names with - and -- in them' {
$branchName = 'branch--for-Pester-tests'
if (&$gitbin branch --list -q $branchName) {
&$gitbin branch -D $branchName
}
&$gitbin branch $branchName
try {
$result = & $module GitTabExpansionInternal 'git push origin branch-'
$result | Should -BeExactly $branchName
$result = & $module GitTabExpansionInternal 'git push --follow-tags -u origin '
$result -contains $branchName | Should -Be $true
}
finally {
&$gitbin branch -D $branchName
}
}
}
Context 'Restore Source Branch TabExpansion Tests' {
It 'Tab completes source branches -s' {
$result = & $module GitTabExpansionInternal 'git restore -s mas'
$result | Should -BeExactly 'master'
}
It 'Tab completes source branches --source=' {
$result = & $module GitTabExpansionInternal 'git restore --source=mas'
$result | Should -BeExactly '--source=master'
}
}
Context 'Vsts' {
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo
# Test with non-standard vsts pr alias name
&$gitbin config alias.test-vsts-pr "!f() { exec vsts code pr \`"`$`@\`"; }; f"
}
AfterEach {
RemoveGitTempRepo $repoPath
}
It 'Tab completes pr options' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr '
$result -contains 'abandon' | Should -Be $true
}
}
Context 'Add/Reset/Checkout TabExpansion Tests' {
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo
}
AfterEach {
RemoveGitTempRepo $repoPath
}
It 'Tab completes non-ASCII file name' {
&$gitbin config core.quotepath true # Problematic (default) config
$fileName = "posh$([char]8226)git.txt"
New-Item $fileName -ItemType File
$gitStatus = & $module Get-GitStatus
$result = & $module GitTabExpansionInternal 'git add ' $gitStatus
$result | Should -BeExactly $fileName
}
}
Context 'Alias TabExpansion Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo -MakeInitialCommit
$addedAliases = @()
function Add-GlobalTestAlias($Name, $Value) {
if (!(&$gitbin config --global "alias.$Name")) {
&$gitbin config --global "alias.$Name" $Value
$addedAliases += $Name
}
}
}
AfterAll {
$addedAliases | Where-Object { $_ } | ForEach-Object {
&$gitbin config --global --unset "alias.$_" 2>$null
}
RemoveGitTempRepo $repoPath
}
It 'Command completion includes unique list of aliases' {
$alias = "test-$(New-Guid)"
Add-GlobalTestAlias $alias config
&$gitbin config alias.$alias help
(&$gitbin config --get-all alias.$alias).Count | Should -Be 2
$result = @(& $module GitTabExpansionInternal "git $alias")
$result.Count | Should -Be 1
$result[0] | Should -BeExactly $alias
}
It 'Tab completes when there is one alias of a given name' {
$alias = "test-$(New-Guid)"
&$gitbin config alias.$alias checkout
@(&$gitbin config --get-all alias.$alias).Length | Should -Be 1
$result = & $module GitTabExpansionInternal "git $alias ma"
$result | Should -BeExactly 'master'
}
It 'Tab completes when there are multiple aliases of the same name' {
Add-GlobalTestAlias co checkout
&$gitbin config alias.co checkout
(&$gitbin config --get-all alias.co).Count | Should -BeGreaterThan 1
$result = & $module GitTabExpansionInternal 'git co ma'
$result | Should -BeExactly 'master'
}
}
Context 'PowerShell Special Chars Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo -MakeInitialCommit
}
AfterAll {
RemoveGitTempRepo $repoPath
}
AfterEach {
ResetGitTempRepoWorkingDir $repoPath
}
It 'Tab completes remote name with special char as quoted' {
&$gitbin remote add '#test' https://github.com/dahlbyk/posh-git.git 2> $null
$result = & $module GitTabExpansionInternal 'git push #'
$result | Should -BeExactly "'#test'"
}
It 'Tab completes branch name with special char as quoted' {
&$gitbin branch '#develop' 2>$null
$result = & $module GitTabExpansionInternal 'git checkout #'
$result | Should -BeExactly "'#develop'"
}
It 'Tab completes git feature branch name with special char as quoted' {
&$gitbin branch '#develop' 2>$null
$result = & $module GitTabExpansionInternal 'git flow feature list #'
$result | Should -BeExactly "'#develop'"
}
It 'Tab completes a tag name with special char as quoted' {
$tag = "v1.0.0;abcdef"
&$gitbin tag $tag
$result = & $module GitTabExpansionInternal 'git show v1'
$result | Should -BeExactly "'$tag'"
}
It 'Tab completes a tag name with single quote correctly' {
&$gitbin tag "v2.0.0'"
$result = & $module GitTabExpansionInternal 'git show v2'
$result | Should -BeExactly "'v2.0.0'''"
}
It 'Tab completes add file in working dir with special char as quoted' {
$filename = 'foo{bar} (x86).txt';
New-Item $filename -ItemType File
$gitStatus = & $module Get-GitStatus
$result = & $module GitTabExpansionInternal 'git add ' $gitStatus
$result | Should -BeExactly "'$filename'"
}
}
}