Skip to content

Commit b462fcc

Browse files
committed
Improved the e2e test system. Added test for 'jenv add renove path'
1 parent 9db68a0 commit b462fcc

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

tests/test.ps1

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#Requires -RunAsAdministrator
22
[cmdletbinding()]param()
33

4-
54
BeforeAll {
65
Start-Transcript -Path $PSScriptRoot/test-log.txt
76
Write-Host Creating Backups
8-
7+
98
# Create backups folder if neccessary. Pipe to null to avoid created message
109
if (!(test-path $PSScriptRoot/backups/)) {
1110
New-Item -ItemType Directory -Force -Path $PSScriptRoot/backups/ | Out-Null
1211
}
13-
12+
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
16+
$jenv = ((get-item $PSScriptRoot).parent.fullname + "\src\jenv.ps1")
17+
18+
1419
Write-Host Backing up your path environment vars
1520
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User")
1621
Out-File -FilePath $PSScriptRoot/backups/jenv.userPath.bak -InputObject $userPath
@@ -20,11 +25,11 @@ BeforeAll {
2025
Out-File -FilePath $PSScriptRoot/backups/jenv.systemPath.bak -InputObject $systemPath
2126
Write-Verbose "Backed up the following SystemPath:"
2227
Write-Verbose $systemPath
23-
28+
2429
Out-File -FilePath $PSScriptRoot/backups/jenv.path.bak -InputObject $env:Path
2530
Write-Verbose "Backed up the following Path:"
2631
Write-Verbose $env:Path
27-
32+
2833
Write-Host Backing up your JEnv Config
2934
if (test-path $Env:APPDATA\JEnv\jenv.config.json) {
3035
Copy-Item -Path $Env:APPDATA\JEnv\jenv.config.json -Destination $PSScriptRoot/backups/jenv.config.bak
@@ -46,46 +51,60 @@ BeforeAll {
4651
Write-Verbose "Changed SystemPath to:"
4752
Write-Verbose $systemPath
4853
$env:Path = $userPath + ";" + $systemPath
49-
50-
function Invoke-JEnvCommand {
54+
55+
function Invoke-JEnvBatch {
5156
param (
5257
$arguments = @()
5358
)
54-
59+
5560
Start-Process -FilePath ((get-item $PSScriptRoot).parent.fullname + "\jenv.bat") -ArgumentList $arguments -Wait -NoNewWindow -RedirectStandardOutput $PSScriptRoot/jenv.test.stdout -RedirectStandardError $PSScriptRoot/jenv.test.stderr
5661
$stdout = Get-Content -Path jenv.test.stdout
5762
Remove-Item -Path jenv.test.stdout
5863
$stderr = Get-Content -Path jenv.test.stderr
5964
Remove-Item -Path jenv.test.stderr
6065
return $stdout, $stderr
61-
66+
6267
}
63-
68+
6469
Write-Host -----------------------------------------------
6570
}
66-
71+
6772
Describe 'JEnv Batch file using correct powershell' {
6873
It "If theres no powershell or pwsh installed it should throw an error" {
69-
$stdout, $stderr = Invoke-JEnvCommand @("list")
74+
$stdout, $stderr = Invoke-JEnvBatch @("list")
7075
$stdout | Should -Be @('Neither pwsh.exe nor powershell.exe was found in your path.', 'Please install powershell it is required')
7176
}
7277
It "If theres powershell, it should use it" {
7378
$env:Path = ($env:Path + ";" + $PSScriptRoot + "/Fake-Executables/powershell/powershell")
74-
$stdout, $stderr = Invoke-JEnvCommand @("list")
79+
$stdout, $stderr = Invoke-JEnvBatch @("list")
7580
$stdout | Should -Be "JEnv is using powershell"
7681
}
7782
It "If theres pwsh, it should use it" {
7883
$env:Path = ($env:Path.Replace(";" + $PSScriptRoot + "/Fake-Executables/powershell/powershell", "") + ";" + $PSScriptRoot + "/Fake-Executables/powershell/pwsh")
79-
$stdout, $stderr = Invoke-JEnvCommand @("list")
84+
$stdout, $stderr = Invoke-JEnvBatch @("list")
8085
$stdout | Should -Be "JEnv is using pwsh"
8186
}
8287
It "If theres powershell and pwsh it should use pwsh" {
8388
$env:Path = ($env:Path + ";" + $PSScriptRoot + "/Fake-Executables/powershell/powershell")
84-
$stdout, $stderr = Invoke-JEnvCommand @("list")
89+
$stdout, $stderr = Invoke-JEnvBatch @("list")
8590
$stdout | Should -Be "JEnv is using pwsh"
8691
}
8792
}
93+
94+
Describe 'JEnv add command' {
95+
96+
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+
}
100+
}
88101

102+
It "Should not accept remove as name" {
103+
$env:Path = $userPath + ";" + $powershell + ";" + $systemPath
104+
& $jenv add remove wrongpath | Should -Be 'Your JEnv name cannot be "remove". Checkout "jenv remove"'
105+
}
106+
}
107+
89108
AfterAll {
90109
Write-Host -----------------------------------------------
91110
Write-Host Restoring your system from backups
@@ -98,11 +117,11 @@ AfterAll {
98117
[System.Environment]::SetEnvironmentVariable("PATH", $systemPath , [System.EnvironmentVariableTarget]::Machine)
99118
Write-Verbose "Your SystemPath was restored from the backup to:"
100119
Write-Verbose $systemPath
101-
120+
102121
$env:Path = (Get-Content -Path $PSScriptRoot/backups/jenv.path.bak)
103122
Write-Verbose "Your path was restored from the backup to:"
104123
Write-Verbose $env:Path
105-
124+
106125
Write-Host Restoring your JEnv config
107126
if (test-path $PSScriptRoot/backups/jenv.config.bak) {
108127
Copy-Item -Path $PSScriptRoot/backups/jenv.config.bak -Destination $Env:APPDATA\JEnv\jenv.config.json
@@ -114,4 +133,3 @@ AfterAll {
114133
}
115134
Stop-Transcript
116135
}
117-

0 commit comments

Comments
 (0)