-
Notifications
You must be signed in to change notification settings - Fork 25
/
nvm.tests.ps1
481 lines (402 loc) · 22.4 KB
/
nvm.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
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
475
476
477
478
479
480
481
Remove-Module nvm -Force -ErrorAction SilentlyContinue
Import-Module ./nvm.psd1
Describe "Get-NodeVersions" {
InModuleScope nvm {
Context "Local versions" {
It "Gets known versions" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { Join-Path $tmpDir '.nvm\settings.json' }
Mock Test-Path { return $true }
Mock Get-ChildItem {
[PSCustomObject]@{
Name = 'v20.13.0'
Path = "$Path\v20.13.0"
}
[PSCustomObject]@{
Name = 'v22.1.0'
Path = "$Path\v22.1.0"
}
}
Mock Get-ChildItem -ParameterFilter { $Filter -match 'node' } {
[PSCustomObject]@{
Name = 'node.exe'
VersionInfo = [PSCustomObject]@{
ProductVersion = ( Split-Path -Path $Path -Leaf ).Replace('v', '')
}
}
}
$versions = Get-NodeVersions
$versions.Count | Should -Be 2
$versions | Should -Be @('v22.1.0'; 'v20.13.0')
}
It "Gets known versions with filter" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { Join-Path $tmpDir '.nvm\settings.json' }
Mock Test-Path { return $true }
Mock Get-ChildItem {
[PSCustomObject]@{
Name = 'v20.13.0'
Path = "$Path\v20.13.0"
}
[PSCustomObject]@{
Name = 'v22.1.0'
Path = "$Path\v22.1.0"
}
}
Mock Get-ChildItem -ParameterFilter { $Filter -match 'node' } {
[PSCustomObject]@{
Name = 'node.exe'
VersionInfo = [PSCustomObject]@{
ProductVersion = ( Split-Path -Path $Path -Leaf ).Replace('v', '')
}
}
}
$versions = Get-NodeVersions -Filter 'v20.13.0'
$versions | Should -Be 'v20.13.0'
}
It "Returns nothing when no versions are installed" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { Join-Path $tmpDir '.nvm\settings.json' }
Mock Test-Path { return $false }
Get-NodeVersions -Filter 'v20.13.0' | Should -BeNullOrEmpty
}
}
Context "Remote versions" {
It "Will list remote versions" {
$mockJson = "[
{""version"":""v22.1.0"",""date"":""2017-10-31"",""files"":[""aix-ppc64"",""headers"",""linux-arm64"",""linux-armv6l"",""linux-armv7l"",""linux-ppc64le"",""linux-x64"",""linux-x86"",""osx-x64-pkg"",""osx-x64-tar"",""src"",""sunos-x64"",""sunos-x86"",""win-x64-7z"",""win-x64-exe"",""win-x64-msi"",""win-x64-zip"",""win-x86-7z"",""win-x86-exe"",""win-x86-msi"",""win-x86-zip""],""npm"":""5.5.1"",""v8"":""6.2.414.32"",""uv"":""1.15.0"",""zlib"":""1.2.11"",""openssl"":""1.0.2l"",""modules"":""59"",""lts"":false},
{""version"":""v20.13.0"",""date"":""2017-10-31"",""files"":[""aix-ppc64"",""headers"",""linux-arm64"",""linux-armv6l"",""linux-armv7l"",""linux-ppc64le"",""linux-x64"",""linux-x86"",""osx-x64-pkg"",""osx-x64-tar"",""src"",""sunos-x64"",""sunos-x86"",""win-x64-7z"",""win-x64-exe"",""win-x64-msi"",""win-x64-zip"",""win-x86-7z"",""win-x86-exe"",""win-x86-msi"",""win-x86-zip""],""npm"":""5.5.1"",""v8"":""6.1.534.46"",""uv"":""1.15.0"",""zlib"":""1.2.11"",""openssl"":""1.0.2l"",""modules"":""57"",""lts"":""Carbon""},
{""version"":""v20.0.0"",""date"":""2017-10-25"",""files"":[""aix-ppc64"",""headers"",""linux-arm64"",""linux-armv6l"",""linux-armv7l"",""linux-ppc64le"",""linux-x64"",""linux-x86"",""osx-x64-pkg"",""osx-x64-tar"",""src"",""sunos-x64"",""sunos-x86"",""win-x64-7z"",""win-x64-exe"",""win-x64-msi"",""win-x64-zip"",""win-x86-7z"",""win-x86-exe"",""win-x86-msi"",""win-x86-zip""],""npm"":""5.4.2"",""v8"":""6.1.534.42"",""uv"":""1.15.0"",""zlib"":""1.2.11"",""openssl"":""1.0.2l"",""modules"":""57"",""lts"":false}
]"
Mock Invoke-WebRequest { return $mockJson }
$versions = Get-NodeVersions -Remote
$versions.Count | Should -Be 3
}
It "Will list remote versions with filter" {
$mockJson = "[
{""version"":""v22.1.0"",""date"":""2017-10-31"",""files"":[""aix-ppc64"",""headers"",""linux-arm64"",""linux-armv6l"",""linux-armv7l"",""linux-ppc64le"",""linux-x64"",""linux-x86"",""osx-x64-pkg"",""osx-x64-tar"",""src"",""sunos-x64"",""sunos-x86"",""win-x64-7z"",""win-x64-exe"",""win-x64-msi"",""win-x64-zip"",""win-x86-7z"",""win-x86-exe"",""win-x86-msi"",""win-x86-zip""],""npm"":""5.5.1"",""v8"":""6.2.414.32"",""uv"":""1.15.0"",""zlib"":""1.2.11"",""openssl"":""1.0.2l"",""modules"":""59"",""lts"":false},
{""version"":""v20.13.0"",""date"":""2017-10-31"",""files"":[""aix-ppc64"",""headers"",""linux-arm64"",""linux-armv6l"",""linux-armv7l"",""linux-ppc64le"",""linux-x64"",""linux-x86"",""osx-x64-pkg"",""osx-x64-tar"",""src"",""sunos-x64"",""sunos-x86"",""win-x64-7z"",""win-x64-exe"",""win-x64-msi"",""win-x64-zip"",""win-x86-7z"",""win-x86-exe"",""win-x86-msi"",""win-x86-zip""],""npm"":""5.5.1"",""v8"":""6.1.534.46"",""uv"":""1.15.0"",""zlib"":""1.2.11"",""openssl"":""1.0.2l"",""modules"":""57"",""lts"":""Carbon""},
{""version"":""v20.0.0"",""date"":""2017-10-25"",""files"":[""aix-ppc64"",""headers"",""linux-arm64"",""linux-armv6l"",""linux-armv7l"",""linux-ppc64le"",""linux-x64"",""linux-x86"",""osx-x64-pkg"",""osx-x64-tar"",""src"",""sunos-x64"",""sunos-x86"",""win-x64-7z"",""win-x64-exe"",""win-x64-msi"",""win-x64-zip"",""win-x86-7z"",""win-x86-exe"",""win-x86-msi"",""win-x86-zip""],""npm"":""5.4.2"",""v8"":""6.1.534.42"",""uv"":""1.15.0"",""zlib"":""1.2.11"",""openssl"":""1.0.2l"",""modules"":""57"",""lts"":false}
]"
Mock Invoke-WebRequest { return $mockJson }
$versions = Get-NodeVersions -Remote -Filter "v20"
$versions.Count | Should -Be 2
}
}
}
}
Describe "Get-NodeInstallLocation" {
InModuleScope nvm {
It "Should return the location when it exists" {
$tmpDir = [system.io.path]::GetTempPath()
$installPath = Join-Path $tmpDir '.nvm'
Mock Test-Path { return $true }
Mock Get-Content { return @{ InstallPath = $installPath } | ConvertTo-Json }
$location = Get-NodeInstallLocation
$location | Should -Be $installPath
}
}
}
Describe "Install-NodeVersion" {
InModuleScope nvm {
Context "auto-discovery" {
BeforeEach {
$nodeVersion = 'v22.1.0'
}
It "Install version from the .nvmrc file" -Skip:($env:include_integration_tests -ne $true) {
Mock Test-Path -ParameterFilter { $Path -match '.nvmrc$' } { return $true }
Mock Get-Content -ParameterFilter { $Path -match '.nvmrc$' } { return $nodeVersion }
Install-NodeVersion
$versions = Get-NodeVersions -Filter 'v22.1.0'
$versions | Should -Be 'v22.1.0'
}
It "Install version from the package.json field" -Skip:($env:include_integration_tests -ne $true) {
Mock Test-Path -ParameterFilter { $Path -match '.nvmrc$' } { return $false }
Mock Test-Path -ParameterFilter { $Path -match 'package.json$' } { return $true }
Mock Get-Content -ParameterFilter { $Path -match 'package.json$' } {
return @{
engines = @{
node = '^22.1.0'
}
} | ConvertTo-Json
}
Install-NodeVersion
$versions = Get-NodeVersions -Filter 'v22.*'
$versions | Should -BeLike 'v22.*'
}
It "Will error if no version in the package.json field" -Skip:($env:include_integration_tests -ne $true) {
Mock Test-Path -ParameterFilter { $Path -match '.nvmrc$' } { return $false }
Mock Test-Path -ParameterFilter { $Path -match 'package.json$' } { return $true }
Mock Get-Content -ParameterFilter { $Path -match 'package.json$' } {
return @{
engines = @{
}
} | ConvertTo-Json
}
{ Install-NodeVersion } | Should -Throw
}
It "Will error if no version, no .nvmrc and no package.json, no default" -Skip:($env:include_integration_tests -ne $true) {
Mock Get-NodeInstallLocation { return "/" }
Mock Test-Path -ParameterFilter { $Path -eq '/default' } { return $false }
Mock Test-Path -ParameterFilter { $Path -match '.nvmrc$' } { return $false }
Mock Test-Path -ParameterFilter { $Path -match 'package.json$' } { return $false }
{ Install-NodeVersion } | Should -Throw "Version not given, no .nvmrc found in folder, and package.json missing or does not contain node engines field"
}
}
Context "Installing with a specific version" {
It "Install a requested version" -Skip:($env:include_integration_tests -ne $true) {
Install-NodeVersion -Version 'v22.1.0'
$versions = Get-NodeVersions -Filter 'v22.1.0'
$versions | Should -Be 'v22.1.0'
}
It "Throws when version already exists" -Skip:($env:include_integration_tests -ne $true) {
Install-NodeVersion -Version 'v22.1.0'
{ Install-NodeVersion -Version 'v22.1.0' } | Should -Throw
}
It "Won't throw when version already exists if you use the -Force flag" -Skip:($env:include_integration_tests -ne $true) {
{ Install-NodeVersion -Version 'v22.1.0' -Force } | Should -Not -Throw
}
It "Can install without a 'v' prefix" -Skip:($env:include_integration_tests -ne $true) {
{ Install-NodeVersion -Version '22.1.0' -Force } | Should -Not -Throw
}
It "Can install multiple versions" -Skip:($env:include_integration_tests -ne $true) {
{ Install-NodeVersion -Version '22.1.0', '20.13.0' } | Should -Not -Throw
}
}
Context "Major version installing" {
It "Can install from just a major version" -Skip:($env:include_integration_tests -ne $true) {
Install-NodeVersion -Version '22'
$versions = Get-NodeVersions -Filter 'v22'
$versions | Should -Match 'v22'
}
}
Context "Major and minor version installing" {
It "Can install from just a major and minor version" -Skip:($env:include_integration_tests -ne $true) {
Install-NodeVersion -Version '22.0'
$versions = Get-NodeVersions -Filter 'v22.0'
$versions | Should -Match 'v22.0'
}
}
Context "Installing with a keyword" {
It "Installs under the `latest` flag" -Skip:($env:include_integration_tests -ne $true) {
Install-NodeVersion -Version 'latest'
$versions = Get-NodeVersions
$versions.GetType() | Should -Be 'SemVer.Version'
}
}
Context "Incomplete installation" {
BeforeEach {
Mock Get-Command -ParameterFilter { $Name -match 'node' -or $Name -match 'npm' } {
throw (
"The term '$Name' is not recognized as the name of a cmdlet, function, script file, or " +
"operable program. Check the spelling of the name, or if a path was included, verify that " +
"the path is correct and try again."
)
}
}
It "Will error if node or npm can't be called" -Skip:($env:include_integration_tests -ne $true) {
{ Install-NodeVersion latest } | Should -Throw
}
}
}
BeforeEach {
$basePath = if ($IsWindows) { $env:SystemDrive } else { [system.io.path]::GetTempPath() }
$installLocation = Join-Path $basePath '.nvm'
Set-NodeInstallLocation -Path $installLocation
}
AfterEach {
if (Test-Path $installLocation) {
Remove-Item -Recurse -Force $installLocation
}
$settingsFile = Join-Path $PSScriptRoot 'settings.json'
if ((Test-Path $settingsFile) -eq $true) {
Remove-Item -Force $settingsFile
}
}
}
Describe "Set-NodeVersion" {
InModuleScope nvm {
BeforeEach {
$nodeVersion = 'v22.1.0'
}
Context "auto-discovery" {
It "Will set from the .nvmrc file" {
$tmpDir = [system.io.path]::GetTempPath()
$nvmDir = Join-Path $tmpDir '.nvm'
Mock Test-Path { return $true } -ParameterFilter { $Path.StartsWith('variable') -eq $false }
Mock Get-Content -ParameterFilter { $Path -match '\.nvmrc$' } { return $nodeVersion }
Mock Get-NodeInstallLocation { return $nvmDir }
Set-NodeVersion -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
It "Will set from the engines package.json field" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Test-Path -ParameterFilter { $Path.StartsWith('variable') -eq $false } {
return (-not ($Path -match '\.nvmrc$'))
}
Mock Get-Content -ParameterFilter { $Path -match 'package.json$' } {
return @{
engines = @{
node = '^22.1.0'
}
} | ConvertTo-Json
}
Mock Get-NodeVersions { return 'v9.1.0' }
Mock Get-NodeInstallLocation { return Join-Path $tmpDir '.nvm' }
Set-NodeVersion -InformationVariable infos
$infos | Should -Be "Switched to node version v9.1.0"
}
It "Will set from the default file" {
$tmpDir = [system.io.path]::GetTempPath()
$nvmDir = Join-Path $tmpDir '.nvm'
Mock Test-Path { return $false } -ParameterFilter { $Path.Contains('.nvmrc') }
Mock Test-Path { return $false } -ParameterFilter { $Path.Contains('./package.json') }
Mock Test-Path { return $true } -ParameterFilter { $Path.Contains((Join-Path $nvmDir 'default')) }
Mock Get-Content -ParameterFilter { $Path -match 'default$' } { return $nodeVersion }
Mock Get-NodeInstallLocation { return $nvmDir }
Set-NodeVersion -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
It "Will error if no version in the package.json field" {
Mock Test-Path -ParameterFilter { $Path.StartsWith('variable') -eq $false } {
return (-not ($Path -match '\.nvmrc$'))
}
Mock Get-Content -ParameterFilter { $Path -match 'package.json$' } {
return @{
engines = @{
}
} | ConvertTo-Json
}
{ Set-NodeVersion } | Should -Throw
}
It "Will error if no version, no .nvmrc and no package.json, no default" {
Mock Get-NodeInstallLocation { return "/" }
Mock Test-Path { return $false } -ParameterFilter { $Path -eq (Join-Path (Get-NodeInstallLocation) 'default') }
Mock Test-Path { return $false } -ParameterFilter { $Path.Contains('.nvmrc') }
Mock Test-Path { return $false } -ParameterFilter { $Path.Contains('./package.json') }
{ Set-NodeVersion } | Should -Throw "Version not given, no .nvmrc found in folder, and package.json missing or does not contain node engines field"
}
}
Context "Set from version string" {
It "Will set from the supplied version" {
Set-NodeVersion $nodeVersion -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
It "Will set from a version range" {
Mock Get-NodeVersions { return @('v22.1.0'; 'v20.13.0') }
Set-NodeVersion 'v9' -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
It "Will set from a version range with caret" {
Mock Get-NodeVersions { return @('v22.1.0'; 'v20.13.0') }
Set-NodeVersion '^22.1.0' -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
It "Will throw error on unmatched version range" {
{
Mock Get-NodeVersions { return @() }
Set-NodeVersion 'v7'
} | Should -Throw "No version found that matches v7"
}
It "Will set npm config path" {
Mock Get-NodeVersions { return @('v22.1.0') }
Set-NodeVersion 'v9' -InformationVariable infos
$env:NPM_CONFIG_GLOBALCONFIG | Should -Not -Be $null
}
It "Will update environment path" {
$explicitVersion = "v9.32.99"
$nvmPath = Get-NodeInstallLocation
Set-NodeVersion -Version 'v22.1.0' -InformationVariable infos
Set-NodeVersion -Version $explicitVersion -InformationVariable infos
$separator = [System.IO.Path]::PathSeparator
[System.String[]]$nvmPaths = ($env:PATH -split $separator) | Where-Object { $_.StartsWith($nvmPath) }
$nvmPaths.Count | Should -Be 1
$nvmPaths | Should -Match $explicitVersion
}
BeforeEach {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { return Join-Path $tmpDir '.nvm' }
Mock Test-Path { return $true } -ParameterFilter { $Path.StartsWith((Join-Path $tmpDir '.nvm')) -eq $true }
}
}
Context "pipeline" {
BeforeEach {
$nodeVersion = "v22.1.0"
Mock Test-Path -ParameterFilter { $Path -match 'vs' } { return $true }
Mock Get-ChildItem {
[PSCustomObject]@{
Name = 'v22.1.0'
Path = "$Path\v22.1.0"
}
}
Mock Get-ChildItem -ParameterFilter { $Filter -match 'node' } {
[PSCustomObject]@{
Name = 'node.exe'
VersionInfo = [PSCustomObject]@{
ProductVersion = ( Split-Path -Path $Path -Leaf ).Replace('v', '')
}
}
}
}
It "Will set from the supplied version via Install-NodeVersion pipeline output" {
[PSCustomObject]@{
Name = 'node.exe'
Version = '22.1.0'
} | Set-NodeVersion -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
It "Will set from the supplied version via Get-NodeVersion pipeline output" {
[SemVer.Version]::new($nodeVersion, $true) | Set-NodeVersion -InformationVariable infos
$infos | Should -Be "Switched to node version $nodeVersion"
}
}
}
AfterEach {
$settingsFile = Join-Path $PSScriptRoot 'settings.json'
if ((Test-Path $settingsFile) -eq $true) {
Remove-Item -Force $settingsFile
}
}
}
Describe "Remove-NodeVersion" {
InModuleScope nvm {
It "Should remove a version" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { return $tmpDir }
Mock Test-Path { return $true }
Mock Remove-Item { }
Remove-NodeVersion 'v22.1.0'
Assert-MockCalled -CommandName Remove-Item -Times 1 -ParameterFilter { $Path -eq (Join-Path $tmpDir 'v22.1.0') }
}
It "Should remove multiple versions" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { return $tmpDir }
Mock Test-Path { return $true }
Mock Remove-Item { }
Remove-NodeVersion 'v22.1.0', 'v10.0.0'
Assert-MockCalled -CommandName Remove-Item -Times 1 -ParameterFilter { $Path -eq (Join-Path $tmpDir 'v22.1.0') }
Assert-MockCalled -CommandName Remove-Item -Times 1 -ParameterFilter { $Path -eq (Join-Path $tmpDir 'v10.0.0') }
}
It "Should remove versions passed from the pipeline" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { return $tmpDir }
Mock Test-Path { return $true }
Mock Remove-Item { }
Mock Get-NodeVersions {
'v22.1.0'
'v10.0.0'
}
Get-NodeVersions | Remove-NodeVersion
Assert-MockCalled -CommandName Remove-Item -Times 1 -ParameterFilter { $Path -eq (Join-Path $tmpDir 'v22.1.0') }
Assert-MockCalled -CommandName Remove-Item -Times 1 -ParameterFilter { $Path -eq (Join-Path $tmpDir 'v10.0.0') }
}
It "Should throw when version doesn't exist" {
$tmpDir = [system.io.path]::GetTempPath()
Mock Get-NodeInstallLocation { return $tmpDir }
Mock Test-Path { return $false }
Mock Remove-Item { }
$version = 'v22.1.0'
{ Remove-NodeVersion $version } | Should -Throw "Could not find node version $version"
}
}
}