Skip to content

Commit b5e7d51

Browse files
committed
Added tests for add command
1 parent 68ba074 commit b5e7d51

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/jenv-add.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ function Invoke-Add {
2626
# Check if name is already used
2727
foreach ($jenv in $config.jenvs) {
2828
if ($jenv.name -eq $name) {
29-
Write-Host 'Theres already a JEnv with that name. Consider using "jenv list"'
29+
Write-Output 'Theres already a JEnv with that name. Consider using "jenv list"'
3030
return
3131
}
3232
}
3333

3434
# Check if the path is a valid java home
3535
if (!(Test-Path -Path $path/bin/java.exe -PathType Leaf)) {
36-
Write-Host ($path + "/bin/java.exe not found. Your Path is not a valid JAVA_HOME")
36+
Write-Output ($path + "/bin/java.exe not found. Your Path is not a valid JAVA_HOME")
3737
return
3838
}
3939

@@ -42,6 +42,6 @@ function Invoke-Add {
4242
name = $name
4343
path = $path
4444
}
45-
Write-Host ("Successfully added the new JEnv: " + $name)
45+
Write-Output ("Successfully added the new JEnv: " + $name)
4646
}
4747
}

tests/test.ps1

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ BeforeAll {
1010
New-Item -ItemType Directory -Force -Path $PSScriptRoot/backups/ | Out-Null
1111
}
1212

13-
# Storing location of pwsh or powershell
14-
($powershell = @(Get-Command -Name @("pwsh.exe", "powershell.exe") -All)[0].source) 2>$null
15-
$powershell = (Get-Item $powershell).Directory.FullName
13+
# Getting the script so it can be run
1614
$jenv = ((get-item $PSScriptRoot).parent.fullname + "\src\jenv.ps1")
1715

1816

@@ -94,15 +92,43 @@ Describe 'JEnv Batch file using correct powershell' {
9492
Describe 'JEnv add command' {
9593

9694
BeforeAll {
97-
if ($null -eq $powershell) {
98-
throw "Neither pwsh.exe nor powershell.exe have been found in the path. Please add one of them so the tests can use it"
99-
}
95+
$env:Path = $userPath + ";" + $PSHOME + ";" + $systemPath
10096
}
10197

10298
It "Should not accept remove as name" {
103-
$env:Path = $userPath + ";" + $powershell + ";" + $systemPath
10499
& $jenv add remove wrongpath | Should -Be 'Your JEnv name cannot be "remove". Checkout "jenv remove"'
105100
}
101+
102+
It "Should add a valid java version" {
103+
& $jenv add fake1 $PSScriptRoot/Fake-Executables/java/v1 | Should -Be 'Successfully added the new JEnv: fake1'
104+
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json
105+
$template = [PSCustomObject]@{
106+
name = "fake1"
107+
path = "$($PSScriptRoot)/Fake-Executables/java/v1"
108+
}
109+
$config.jenvs | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
110+
}
111+
112+
It "Should add another valid java version" {
113+
& $jenv add fake2 $PSScriptRoot/Fake-Executables/java/v2 | Should -Be 'Successfully added the new JEnv: fake2'
114+
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json
115+
$template = @([PSCustomObject]@{
116+
name = "fake1"
117+
path = "$($PSScriptRoot)/Fake-Executables/java/v1"
118+
}, [PSCustomObject]@{
119+
name = "fake2"
120+
path = "$($PSScriptRoot)/Fake-Executables/java/v2"
121+
})
122+
$config.jenvs | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
123+
}
124+
125+
It "Should not add another jenv with same name" {
126+
& $jenv add fake1 $PSScriptRoot/Fake-Executables/java/v2 | Should -Be 'Theres already a JEnv with that name. Consider using "jenv list"'
127+
}
128+
129+
It "Should not add an invalid jenv" {
130+
& $jenv add invalid $PSScriptRoot/Fake-Executables/java/ | Should -Be $PSScriptRoot/Fake-Executables/java/"/bin/java.exe not found. Your Path is not a valid JAVA_HOME"
131+
}
106132
}
107133

108134
AfterAll {

0 commit comments

Comments
 (0)