Skip to content

Commit 5c19b4f

Browse files
committed
Added automatic test for jenv local
1 parent 1d7e5d5 commit 5c19b4f

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

src/jenv-local.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ function Invoke-Local {
1818
# Remove the local JEnv
1919
if ($name -eq "remove") {
2020
$config.locals = @($config.locals | Where-Object { $_.path -ne (Get-Location) })
21-
Write-Host Your local JEnv was unset
21+
Write-Output "Your local JEnv was unset"
2222
return
2323
}
2424

2525
# Check if specified JEnv is avaible
2626
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
2727
if ($null -eq $jenv) {
28-
Write-Host Theres no JEnv with name $name Consider using "jenv list"
28+
Write-Output "Theres no JEnv with name $name Consider using `"jenv list`""
2929
return
3030
}
3131

@@ -34,7 +34,7 @@ function Invoke-Local {
3434
if ($jenv.path -eq (Get-Location)) {
3535
# if path is used replace with new version
3636
$jenv.name = $name
37-
Write-Host "Your replaced your java version for" (Get-Location) with $name
37+
Write-Output "Your replaced your java version for" (Get-Location) with $name
3838
return
3939
}
4040
}
@@ -45,6 +45,6 @@ function Invoke-Local {
4545
name = $name
4646
}
4747

48-
Write-Host $name "is now your local java version for" (Get-Location)
48+
Write-Output $name "is now your local java version for" (Get-Location)
4949
}
5050
}

tests/test.ps1

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,73 @@ Describe 'JEnv add command' {
131131
}
132132
}
133133

134+
Describe 'JEnv local command' {
135+
136+
BeforeAll {
137+
$env:Path = $userPath + ";" + $PSHOME + ";" + $systemPath
138+
}
139+
140+
It "Should add a valid local" {
141+
& $jenv local fake1 | Should -Be @('fake1', 'is now your local java version for', "C:\JEnv-for-Windows\tests")
142+
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json
143+
144+
$template = @([PSCustomObject]@{
145+
path = "C:\JEnv-for-Windows\tests"
146+
name = "fake1"
147+
})
148+
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
149+
}
150+
151+
It "Should add a valid local with different path and jdk" {
152+
Set-Location $HOME
153+
& $jenv local fake2 | Should -Be @('fake2', 'is now your local java version for', $HOME)
154+
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json
155+
156+
$template = @([PSCustomObject]@{
157+
path = "C:\JEnv-for-Windows\tests"
158+
name = "fake1"
159+
}, [PSCustomObject]@{
160+
path = $HOME
161+
name = "fake2"
162+
})
163+
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
164+
}
165+
166+
It "Should replace jenv for path if path already in config" {
167+
& $jenv local fake1 | Should -Be @('Your replaced your java version for', $HOME, 'with', 'fake1')
168+
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json
169+
170+
$template = @([PSCustomObject]@{
171+
path = "C:\JEnv-for-Windows\tests"
172+
name = "fake1"
173+
}, [PSCustomObject]@{
174+
path = $HOME
175+
name = "fake1"
176+
})
177+
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
178+
}
179+
180+
It "Should not set a local if jenv was not added to the config" {
181+
& $jenv local notavaible | Should -Be 'Theres no JEnv with name notavaible Consider using "jenv list"'
182+
}
183+
184+
It "Should remove jenv from config" {
185+
& $jenv local remove | Should -Be "Your local JEnv was unset"
186+
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json
187+
188+
$template = @([PSCustomObject]@{
189+
path = "C:\JEnv-for-Windows\tests"
190+
name = "fake1"
191+
})
192+
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
193+
}
194+
195+
AfterAll {
196+
Set-Location ((get-item $PSScriptRoot).parent.fullname + "/tests")
197+
}
198+
}
199+
200+
134201
AfterAll {
135202
Write-Host -----------------------------------------------
136203
Write-Host Restoring your system from backups

0 commit comments

Comments
 (0)