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
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 {
                    $ret = @()
                    $ret += @{ Name = 'v8.9.0' }
                    $ret += @{ Name = 'v9.0.0' }
                    return $ret
                }

                $versions = Get-NodeVersions
                $versions.Count | Should -Be 2
                $versions | Should -Be @('v9.0.0'; 'v8.9.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 {
                    $ret = @()
                    $ret += @{ Name = 'v8.9.0' }
                    $ret += @{ Name = 'v9.0.0' }
                    return $ret
                }

                $versions = Get-NodeVersions -Filter 'v8.9.0'
                $versions | Should -Be 'v8.9.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 'v8.9.0' | Should -BeNullOrEmpty
            }
        }

        Context "Remote versions" {
            It "Will list remote versions" {
                $mockJson = "[
                    {""version"":""v9.0.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"":""v8.9.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"":""v8.8.1"",""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"":""v9.0.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"":""v8.9.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"":""v8.8.1"",""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 "v8"
                $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" {
            $nodeVersion = 'v9.0.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 'v9.0.0'
                $versions | Should -Be 'v9.0.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 = '^9.0.0'
                        }
                    } | ConvertTo-Json
                }

                Install-NodeVersion

                $versions = Get-NodeVersions -Filter 'v9.*'
                $versions | Should -BeLike 'v9.*'
            }

            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" -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 $false }

                { Install-NodeVersion } | Should Throw "Version not given and no .nvmrc or package.json found in folder"
            }
        }

        Context "Installing with a specific version" {
            It "Install a requested version" -Skip:($env:include_integration_tests -ne $true) {
                Install-NodeVersion -Version 'v9.0.0'

                $versions = Get-NodeVersions -Filter 'v9.0.0'
                $versions | Should -Be 'v9.0.0'
            }

            It "Throws when version already exists" -Skip:($env:include_integration_tests -ne $true) {
                Install-NodeVersion -Version 'v9.0.0'
                { Install-NodeVersion -Version 'v9.0.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 'v9.0.0' -Force } | Should Not Throw
            }

            It "Can install without a 'v' prefix" -Skip:($env:include_integration_tests -ne $true) {
                { Install-NodeVersion -Version '9.0.0' -Force } | 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 '9'

                $versions = Get-NodeVersions -Filter 'v9'
                $versions | Should -Match 'v9'
            }
        }

        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 '9.0'

                $versions = Get-NodeVersions -Filter 'v9.0'
                $versions | Should -Match 'v9.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'
            }
        }
    }

    BeforeEach {
        $installLocation = Join-Path ([system.io.path]::GetTempPath()) '.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 {
        Context "auto-discovery" {
            $nodeVersion = 'v9.0.0'

            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 = '^9.0.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 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" {
                Mock Test-Path -ParameterFilter { $Path.Contains('.nvmrc') } {
                    return $false
                }
                Mock Test-Path { return $false } -ParameterFilter { $Path.Contains('./package.json') }

                { Set-NodeVersion } | Should Throw "Version not given and no .nvmrc or package.json found in folder"
            }
        }

        Context "Set from version string" {
            $nodeVersion = 'v9.0.0'

            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 @('v9.0.0'; 'v8.9.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 @('v9.0.0'; 'v8.9.0') }

                Set-NodeVersion '^9.0.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 @('v9.0.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 'v9.0.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 }
            }
        }
    }
    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 'v9.0.0'

            Assert-MockCalled -CommandName Remove-Item -Times 1 -ParameterFilter { $Path -eq (Join-Path $tmpDir 'v9.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 = 'v9.0.0'
            { Remove-NodeVersion $version } | Should -Throw "Could not find node version $version"
        }
    }
}