forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoop-Core.Tests.ps1
394 lines (320 loc) · 13 KB
/
Scoop-Core.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
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
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\install.ps1"
. "$psscriptroot\..\lib\unix.ps1"
. "$psscriptroot\Scoop-TestLib.ps1"
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
$isUnix = is_unix
describe "Get-AppFilePath" -Tag 'Scoop' {
beforeall {
$working_dir = setup_working "is_directory"
Mock versiondir { 'local' } -Verifiable -ParameterFilter { $global -eq $false }
Mock versiondir { '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-Aria2Enabled" -Tag 'Scoop' {
it "should return true if aria2 is installed" {
Mock Test-HelperInstalled { $true }
Mock get_config { $true }
Test-Aria2Enabled | Should -BeTrue
}
it "should return false if aria2 is not installed" {
Mock Test-HelperInstalled { $false }
Mock get_config { $false }
Test-Aria2Enabled | Should -BeFalse
Mock Test-HelperInstalled { $false }
Mock get_config { $true }
Test-Aria2Enabled | Should -BeFalse
Mock Test-HelperInstalled { $true }
Mock get_config { $false }
Test-Aria2Enabled | Should -BeFalse
}
}
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' {
$extract_dir = "subdir"
$extract_to = $null
beforeall {
$working_dir = setup_working "movedir"
}
it "moves directories with no spaces in path" -skip:$isUnix {
$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" -skip:$isUnix {
$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" -skip:$isUnix {
$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' {
beforeall {
$working_dir = setup_working "shim"
$shimdir = shimdir
$(ensure_in_path $shimdir) | out-null
}
it "links a file onto the user's path" -skip:$isUnix {
{ 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!"
}
context "user with quote" {
it "shims a file with quote in path" -skip:$isUnix {
{ 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' {
beforeall {
$working_dir = setup_working "shim"
$shimdir = shimdir
$(ensure_in_path $shimdir) | out-null
}
it "removes shim from path" -skip:$isUnix {
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_ps1_shim" -Tag 'Scoop' {
BeforeAll {
$working_dir = setup_working "shim"
$shimdir = shimdir
$(ensure_in_path $shimdir) | Out-Null
}
It "returns empty string if file does not exist" -skip:$isUnix {
get_app_name_from_ps1_shim "non-existent-file" | should -be ""
}
It "returns app name if file exists and is a shim to an app" -skip:$isUnix {
mkdir -p "$working_dir/mockapp/current/"
Write-Output "" | Out-File "$working_dir/mockapp/current/mockapp.ps1"
shim "$working_dir/mockapp/current/mockapp.ps1" $false "shim-test"
$shim_path = (get-command "shim-test.ps1").Path
get_app_name_from_ps1_shim "$shim_path" | should -be "mockapp"
}
It "returns empty string if file exists and is not a shim" -skip:$isUnix {
Write-Output "lorem ipsum" | Out-File -Encoding ascii "$working_dir/mock-shim.ps1"
get_app_name_from_ps1_shim "$working_dir/mock-shim.ps1" | should -be ""
}
AfterEach {
if (Get-Command "shim-test" -ErrorAction SilentlyContinue) {
rm_shim "shim-test" $shimdir -ErrorAction SilentlyContinue
}
Remove-Item -Force -Recurse -ErrorAction SilentlyContinue "$working_dir/mockapp"
Remove-Item -Force -ErrorAction SilentlyContinue "$working_dir/moch-shim.ps1"
}
}
describe "ensure_robocopy_in_path" -Tag 'Scoop' {
$shimdir = shimdir $false
mock versiondir { $repo_dir }
beforeall {
reset_aliases
}
context "robocopy is not in path" {
it "shims robocopy when not on path" -skip:$isUnix {
mock Test-CommandAvailable { $false }
Test-CommandAvailable robocopy | should -be $false
ensure_robocopy_in_path
"$shimdir/robocopy.ps1" | should -exist
"$shimdir/robocopy.exe" | should -exist
# clean up
rm_shim robocopy $(shimdir $false) | out-null
}
}
context "robocopy is in path" {
it "does not shim robocopy when it is in path" -skip:$isUnix {
mock Test-CommandAvailable { $true }
Test-CommandAvailable robocopy | should -be $true
ensure_robocopy_in_path
"$shimdir/robocopy.ps1" | should -not -exist
"$shimdir/robocopy.exe" | should -not -exist
}
}
}
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/[email protected]"
$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/[email protected]"
$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/[email protected]"
$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/[email protected]"
$app, $bucket, $version = parse_app $query
$app | should -be "test_app"
$bucket | should -be "test-bucket"
$version | should -be "1.8.0-rc2"
}
}