-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathScoop-Core.Tests.ps1
More file actions
474 lines (396 loc) · 17.2 KB
/
Scoop-Core.Tests.ps1
File metadata and controls
474 lines (396 loc) · 17.2 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
BeforeAll {
. "$PSScriptRoot\Scoop-TestLib.ps1"
. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\system.ps1"
. "$PSScriptRoot\..\lib\install.ps1"
}
Describe 'Get-AppFilePath' -Tag 'Scoop' {
BeforeAll {
$working_dir = setup_working 'is_directory'
Mock currentdir { 'local' } -Verifiable -ParameterFilter { $global -eq $false }
Mock currentdir { 'global' } -Verifiable -ParameterFilter { $global -eq $true }
}
It 'should return locally installed program' {
Mock Test-Path { $true } -Verifiable -ParameterFilter { $Path -eq 'local\i_am_a_file.txt' }
Mock Test-Path { $false } -Verifiable -ParameterFilter { $Path -eq 'global\i_am_a_file.txt' }
Get-AppFilePath -App 'is_directory' -File 'i_am_a_file.txt' | Should -Be 'local\i_am_a_file.txt'
}
It 'should return globally installed program' {
Mock Test-Path { $false } -Verifiable -ParameterFilter { $Path -eq 'local\i_am_a_file.txt' }
Mock Test-Path { $true } -Verifiable -ParameterFilter { $Path -eq 'global\i_am_a_file.txt' }
Get-AppFilePath -App 'is_directory' -File 'i_am_a_file.txt' | Should -Be 'global\i_am_a_file.txt'
}
It 'should return null if program is not installed' {
Get-AppFilePath -App 'is_directory' -File 'i_do_not_exist' | Should -BeNullOrEmpty
}
It 'should throw if parameter is wrong or missing' {
{ Get-AppFilePath -App 'is_directory' -File } | Should -Throw
{ Get-AppFilePath -App -File 'i_am_a_file.txt' } | Should -Throw
{ Get-AppFilePath -App -File } | Should -Throw
}
}
Describe 'Get-HelperPath' -Tag 'Scoop' {
BeforeAll {
$working_dir = setup_working 'is_directory'
}
It 'should return path if program is installed' {
Mock Get-AppFilePath { '7zip\current\7z.exe' }
Get-HelperPath -Helper 7zip | Should -Be '7zip\current\7z.exe'
}
It 'should return null if program is not installed' {
Mock Get-AppFilePath { $null }
Get-HelperPath -Helper 7zip | Should -BeNullOrEmpty
}
It 'should throw if parameter is wrong or missing' {
{ Get-HelperPath -Helper } | Should -Throw
{ Get-HelperPath -Helper Wrong } | Should -Throw
}
}
Describe 'Test-HelperInstalled' -Tag 'Scoop' {
It 'should return true if program is installed' {
Mock Get-HelperPath { '7z.exe' }
Test-HelperInstalled -Helper 7zip | Should -BeTrue
}
It 'should return false if program is not installed' {
Mock Get-HelperPath { $null }
Test-HelperInstalled -Helper 7zip | Should -BeFalse
}
It 'should throw if parameter is wrong or missing' {
{ Test-HelperInstalled -Helper } | Should -Throw
{ Test-HelperInstalled -Helper Wrong } | Should -Throw
}
}
Describe 'Test-CommandAvailable' -Tag 'Scoop' {
It 'should return true if command exists' {
Test-CommandAvailable 'Write-Host' | Should -BeTrue
}
It "should return false if command doesn't exist" {
Test-CommandAvailable 'Write-ThisWillProbablyNotExist' | Should -BeFalse
}
It 'should throw if parameter is wrong or missing' {
{ Test-CommandAvailable } | Should -Throw
}
}
Describe 'is_directory' -Tag 'Scoop' {
BeforeAll {
$working_dir = setup_working 'is_directory'
}
It 'is_directory recognize directories' {
is_directory "$working_dir\i_am_a_directory" | Should -Be $true
}
It 'is_directory recognize files' {
is_directory "$working_dir\i_am_a_file.txt" | Should -Be $false
}
It 'is_directory is falsey on unknown path' {
is_directory "$working_dir\i_do_not_exist" | Should -Be $false
}
}
Describe 'movedir' -Tag 'Scoop', 'Windows' {
BeforeAll {
$working_dir = setup_working 'movedir'
$extract_dir = 'subdir'
$extract_to = $null
}
It 'moves directories with no spaces in path' {
$dir = "$working_dir\user"
movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to"
"$dir\test.txt" | Should -FileContentMatch 'this is the one'
"$dir\_tmp\$extract_dir" | Should -Not -Exist
}
It 'moves directories with spaces in path' {
$dir = "$working_dir\user with space"
movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to"
"$dir\test.txt" | Should -FileContentMatch 'this is the one'
"$dir\_tmp\$extract_dir" | Should -Not -Exist
# test trailing \ in from dir
movedir "$dir\_tmp\$null" "$dir\another"
"$dir\another\test.txt" | Should -FileContentMatch 'testing'
"$dir\_tmp" | Should -Not -Exist
}
It 'moves directories with quotes in path' {
$dir = "$working_dir\user with 'quote"
movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to"
"$dir\test.txt" | Should -FileContentMatch 'this is the one'
"$dir\_tmp\$extract_dir" | Should -Not -Exist
}
}
Describe 'shim' -Tag 'Scoop', 'Windows' {
BeforeAll {
$working_dir = setup_working 'shim'
$shimdir = shimdir
Add-Path $shimdir
}
It "links a file onto the user's path" {
{ Get-Command 'shim-test' -ea stop } | Should -Throw
{ Get-Command 'shim-test.ps1' -ea stop } | Should -Throw
{ Get-Command 'shim-test.cmd' -ea stop } | Should -Throw
{ shim-test } | Should -Throw
shim "$working_dir\shim-test.ps1" $false 'shim-test'
{ Get-Command 'shim-test' -ea stop } | Should -Not -Throw
{ Get-Command 'shim-test.ps1' -ea stop } | Should -Not -Throw
{ Get-Command 'shim-test.cmd' -ea stop } | Should -Not -Throw
shim-test | Should -Be 'Hello, world!'
}
It 'shims a file with quote in path' {
{ Get-Command 'shim-test' -ea stop } | Should -Throw
{ shim-test } | Should -Throw
shim "$working_dir\user with 'quote\shim-test.ps1" $false 'shim-test'
{ Get-Command 'shim-test' -ea stop } | Should -Not -Throw
shim-test | Should -Be 'Hello, world!'
}
AfterEach {
rm_shim 'shim-test' $shimdir
}
}
Describe 'rm_shim' -Tag 'Scoop', 'Windows' {
BeforeAll {
$working_dir = setup_working 'shim'
$shimdir = shimdir
Add-Path $shimdir
}
It 'removes shim from path' {
shim "$working_dir\shim-test.ps1" $false 'shim-test'
rm_shim 'shim-test' $shimdir
{ Get-Command 'shim-test' -ea stop } | Should -Throw
{ Get-Command 'shim-test.ps1' -ea stop } | Should -Throw
{ Get-Command 'shim-test.cmd' -ea stop } | Should -Throw
{ shim-test } | Should -Throw
}
}
Describe 'get_app_name_from_shim' -Tag 'Scoop', 'Windows' {
BeforeAll {
$working_dir = setup_working 'shim'
$shimdir = shimdir
Add-Path $shimdir
Mock appsdir { $working_dir }
}
It 'returns empty string if file does not exist' {
get_app_name_from_shim 'non-existent-file' | Should -Be ''
}
It 'returns app name if file exists and is a shim to an app' {
ensure "$working_dir/mockapp/current/"
Write-Output '' | Out-File "$working_dir/mockapp/current/mockapp1.ps1"
shim "$working_dir/mockapp/current/mockapp1.ps1" $false 'shim-test1'
$shim_path1 = (Get-Command 'shim-test1.ps1').Path
get_app_name_from_shim "$shim_path1" | Should -Be 'mockapp'
ensure "$working_dir/mockapp/1.0.0/"
Write-Output '' | Out-File "$working_dir/mockapp/1.0.0/mockapp2.ps1"
shim "$working_dir/mockapp/1.0.0/mockapp2.ps1" $false 'shim-test2'
$shim_path2 = (Get-Command 'shim-test2.ps1').Path
get_app_name_from_shim "$shim_path2" | Should -Be 'mockapp'
}
It 'returns empty string if file exists and is not a shim' {
Write-Output 'lorem ipsum' | Out-File -Encoding ascii "$working_dir/mock-shim.ps1"
get_app_name_from_shim "$working_dir/mock-shim.ps1" | Should -Be ''
}
AfterAll {
if (Get-Command 'shim-test1' -ErrorAction SilentlyContinue) {
rm_shim 'shim-test1' $shimdir -ErrorAction SilentlyContinue
}
if (Get-Command 'shim-test2' -ErrorAction SilentlyContinue) {
rm_shim 'shim-test2' $shimdir -ErrorAction SilentlyContinue
}
Remove-Item -Force -Recurse -ErrorAction SilentlyContinue "$working_dir/mockapp"
Remove-Item -Force -ErrorAction SilentlyContinue "$working_dir/moch-shim.ps1"
}
}
Describe 'cache_path' -Tag 'Scoop' {
It 'returns the correct cache path for a given input' {
$url = 'https://example.com/git.zip'
$ret = cache_path 'git' '2.44.0' $url
$inputStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url))
$sha = (Get-FileHash -Algorithm SHA256 -InputStream $inputStream).Hash.ToLower().Substring(0, 7)
$ret | Should -Be "$cachedir\git#2.44.0#$sha.zip"
}
# # NOTE: Remove this 6 months after the feature ships.
It 'returns the old format cache path for a given input' {
Mock Test-Path { $true }
$ret = cache_path 'git' '2.44.0' 'https://example.com/git.zip'
$ret | Should -Be "$cachedir\git#2.44.0#https_example.com_git.zip"
}
}
Describe 'sanitary_path' -Tag 'Scoop' {
It 'removes invalid path characters from a string' {
$path = 'test?.json'
$valid_path = sanitary_path $path
$valid_path | Should -Be 'test.json'
}
}
Describe 'app' -Tag 'Scoop' {
It 'parses the bucket name from an app query' {
$query = 'C:\test.json'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'C:\test.json'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = 'test.json'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test.json'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = '.\test.json'
$app, $bucket, $version = parse_app $query
$app | Should -Be '.\test.json'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = '..\test.json'
$app, $bucket, $version = parse_app $query
$app | Should -Be '..\test.json'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = '\\share\test.json'
$app, $bucket, $version = parse_app $query
$app | Should -Be '\\share\test.json'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = 'https://example.com/test.json'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'https://example.com/test.json'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = 'test'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = 'extras/enso'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'enso'
$bucket | Should -Be 'extras'
$version | Should -BeNullOrEmpty
$query = 'test-app'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test-app'
$bucket | Should -BeNullOrEmpty
$version | Should -BeNullOrEmpty
$query = 'test-bucket/test-app'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test-app'
$bucket | Should -Be 'test-bucket'
$version | Should -BeNullOrEmpty
$query = 'test-bucket/test-app@1.8.0'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test-app'
$bucket | Should -Be 'test-bucket'
$version | Should -Be '1.8.0'
$query = 'test-bucket/test-app@1.8.0-rc2'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test-app'
$bucket | Should -Be 'test-bucket'
$version | Should -Be '1.8.0-rc2'
$query = 'test-bucket/test_app'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test_app'
$bucket | Should -Be 'test-bucket'
$version | Should -BeNullOrEmpty
$query = 'test-bucket/test_app@1.8.0'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test_app'
$bucket | Should -Be 'test-bucket'
$version | Should -Be '1.8.0'
$query = 'test-bucket/test_app@1.8.0-rc2'
$app, $bucket, $version = parse_app $query
$app | Should -Be 'test_app'
$bucket | Should -Be 'test-bucket'
$version | Should -Be '1.8.0-rc2'
}
}
Describe 'Get-PEMachine' -Tag 'Scoop', 'Windows' {
It 'returns machine type for a valid PE file' {
$shim_path = get_shim_path
if ($shim_path -and (Test-Path $shim_path)) {
$machine = Get-PEMachine $shim_path
# Should be a known machine type (I386 (x86): 0x014c, amd64 (x64): 0x8664, arm64: 0xAA64)
# https://learn.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants
$machine | Should -BeIn @(0x014c, 0x8664, 0xAA64)
} else {
Set-ItResult -Skipped -Because 'shim exe not found'
}
}
It 'returns 0 for a non-existent file' {
Get-PEMachine 'C:\nonexistent\fake.exe' | Should -Be 0
}
It 'returns 0 for a non-PE file' {
$working_dir = setup_working 'shim'
Get-PEMachine "$working_dir\shim-test.ps1" | Should -Be 0
}
}
Describe 'WoW64 path rewriting in shim' -Tag 'Scoop', 'Windows' {
It 'rewrites System32 to Sysnative for x86 shim on x64 OS' {
$sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32')
$sysnative = [System.IO.Path]::Combine($env:SystemRoot, 'Sysnative')
$testPath = "$sysdir\notepad.exe"
if ([System.Environment]::Is64BitOperatingSystem) {
$result = $testPath -replace [regex]::Escape($sysdir), $sysnative
$result | Should -Be "$sysnative\notepad.exe"
} else {
Set-ItResult -Skipped -Because 'not a x64 OS'
}
}
It 'rewrites SysWOW64 to System32 for x86 shim on x64 OS' {
$sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32')
$syswow = [System.IO.Path]::Combine($env:SystemRoot, 'SysWOW64')
$testPath = "$syswow\notepad.exe"
if ([System.Environment]::Is64BitOperatingSystem) {
$result = $testPath -replace [regex]::Escape($syswow), $sysdir
$result | Should -Be "$sysdir\notepad.exe"
} else {
Set-ItResult -Skipped -Because 'not a x64 OS'
}
}
It 'does not rewrite paths outside System32 and SysWOW64' {
$sysdir = [System.IO.Path]::Combine($env:SystemRoot, 'System32')
$syswow = [System.IO.Path]::Combine($env:SystemRoot, 'SysWOW64')
$testPath = 'C:\Program Files\test\app.exe'
$result = $testPath
if ($result -like "$sysdir\*") {
$result = $result -replace [regex]::Escape($sysdir), 'Sysnative'
} elseif ($result -like "$syswow\*") {
$result = $result -replace [regex]::Escape($syswow), $sysdir
}
$result | Should -Be $testPath
}
}
Describe 'Format Architecture String' -Tag 'Scoop' {
It 'should keep correct architectures' {
Format-ArchitectureString '32bit' | Should -Be '32bit'
Format-ArchitectureString '32' | Should -Be '32bit'
Format-ArchitectureString 'x86' | Should -Be '32bit'
Format-ArchitectureString 'X86' | Should -Be '32bit'
Format-ArchitectureString 'i386' | Should -Be '32bit'
Format-ArchitectureString '386' | Should -Be '32bit'
Format-ArchitectureString 'i686' | Should -Be '32bit'
Format-ArchitectureString '64bit' | Should -Be '64bit'
Format-ArchitectureString '64' | Should -Be '64bit'
Format-ArchitectureString 'x64' | Should -Be '64bit'
Format-ArchitectureString 'X64' | Should -Be '64bit'
Format-ArchitectureString 'amd64' | Should -Be '64bit'
Format-ArchitectureString 'AMD64' | Should -Be '64bit'
Format-ArchitectureString 'x86_64' | Should -Be '64bit'
Format-ArchitectureString 'x86-64' | Should -Be '64bit'
Format-ArchitectureString 'arm64' | Should -Be 'arm64'
Format-ArchitectureString 'arm' | Should -Be 'arm64'
Format-ArchitectureString 'aarch64' | Should -Be 'arm64'
Format-ArchitectureString 'ARM64' | Should -Be 'arm64'
Format-ArchitectureString 'ARM' | Should -Be 'arm64'
Format-ArchitectureString 'AARCH64' | Should -Be 'arm64'
}
It 'should fallback to the default architecture on empty input' {
Format-ArchitectureString '' | Should -Be $(Get-DefaultArchitecture)
Format-ArchitectureString $null | Should -Be $(Get-DefaultArchitecture)
}
It 'should show an error with an invalid architecture' {
{ Format-ArchitectureString 'PPC' } | Should -Throw "Invalid architecture: 'ppc'"
}
}
Describe 'substitute' -Tag 'Scoop' {
It 'should properly handle keys that are substrings of other keys' {
$params = @{}
# Run repeatedly (10 times) to reduce the likelihood of accidental correct ordering.
1 .. 10 | ForEach-Object {
$params["`$name$($_)"] = "$($_).exe"
$params["`$name$($_)NoExt"] = "$_"
substitute ("`$name$($_)NoExt") $params $false | Should -Be "$_"
}
}
}