Skip to content

Commit

Permalink
Added automatic test for jenv local
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixSelter committed Apr 7, 2022
1 parent 1d7e5d5 commit 5c19b4f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/jenv-local.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function Invoke-Local {
# Remove the local JEnv
if ($name -eq "remove") {
$config.locals = @($config.locals | Where-Object { $_.path -ne (Get-Location) })
Write-Host Your local JEnv was unset
Write-Output "Your local JEnv was unset"
return
}

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

Expand All @@ -34,7 +34,7 @@ function Invoke-Local {
if ($jenv.path -eq (Get-Location)) {
# if path is used replace with new version
$jenv.name = $name
Write-Host "Your replaced your java version for" (Get-Location) with $name
Write-Output "Your replaced your java version for" (Get-Location) with $name
return
}
}
Expand All @@ -45,6 +45,6 @@ function Invoke-Local {
name = $name
}

Write-Host $name "is now your local java version for" (Get-Location)
Write-Output $name "is now your local java version for" (Get-Location)
}
}
67 changes: 67 additions & 0 deletions tests/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,73 @@ Describe 'JEnv add command' {
}
}

Describe 'JEnv local command' {

BeforeAll {
$env:Path = $userPath + ";" + $PSHOME + ";" + $systemPath
}

It "Should add a valid local" {
& $jenv local fake1 | Should -Be @('fake1', 'is now your local java version for', "C:\JEnv-for-Windows\tests")
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json

$template = @([PSCustomObject]@{
path = "C:\JEnv-for-Windows\tests"
name = "fake1"
})
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
}

It "Should add a valid local with different path and jdk" {
Set-Location $HOME
& $jenv local fake2 | Should -Be @('fake2', 'is now your local java version for', $HOME)
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json

$template = @([PSCustomObject]@{
path = "C:\JEnv-for-Windows\tests"
name = "fake1"
}, [PSCustomObject]@{
path = $HOME
name = "fake2"
})
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
}

It "Should replace jenv for path if path already in config" {
& $jenv local fake1 | Should -Be @('Your replaced your java version for', $HOME, 'with', 'fake1')
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json

$template = @([PSCustomObject]@{
path = "C:\JEnv-for-Windows\tests"
name = "fake1"
}, [PSCustomObject]@{
path = $HOME
name = "fake1"
})
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
}

It "Should not set a local if jenv was not added to the config" {
& $jenv local notavaible | Should -Be 'Theres no JEnv with name notavaible Consider using "jenv list"'
}

It "Should remove jenv from config" {
& $jenv local remove | Should -Be "Your local JEnv was unset"
$config = Get-Content -Path ($Env:APPDATA + "\JEnv\jenv.config.json") -Raw | ConvertFrom-Json

$template = @([PSCustomObject]@{
path = "C:\JEnv-for-Windows\tests"
name = "fake1"
})
$config.locals | ConvertTo-Json | Should -Be ($template | ConvertTo-Json)
}

AfterAll {
Set-Location ((get-item $PSScriptRoot).parent.fullname + "/tests")
}
}


AfterAll {
Write-Host -----------------------------------------------
Write-Host Restoring your system from backups
Expand Down

0 comments on commit 5c19b4f

Please sign in to comment.