1
1
# Requires -RunAsAdministrator
2
2
[cmdletbinding ()]param ()
3
3
4
-
5
4
BeforeAll {
6
5
Start-Transcript - Path $PSScriptRoot / test-log .txt
7
6
Write-Host Creating Backups
8
-
7
+
9
8
# Create backups folder if neccessary. Pipe to null to avoid created message
10
9
if (! (test-path $PSScriptRoot / backups/ )) {
11
10
New-Item - ItemType Directory - Force - Path $PSScriptRoot / backups/ | Out-Null
12
11
}
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
+
14
19
Write-Host Backing up your path environment vars
15
20
$userPath = [System.Environment ]::GetEnvironmentVariable(" PATH" , " User" )
16
21
Out-File - FilePath $PSScriptRoot / backups/ jenv.userPath.bak - InputObject $userPath
@@ -20,11 +25,11 @@ BeforeAll {
20
25
Out-File - FilePath $PSScriptRoot / backups/ jenv.systemPath.bak - InputObject $systemPath
21
26
Write-Verbose " Backed up the following SystemPath:"
22
27
Write-Verbose $systemPath
23
-
28
+
24
29
Out-File - FilePath $PSScriptRoot / backups/ jenv.path.bak - InputObject $env: Path
25
30
Write-Verbose " Backed up the following Path:"
26
31
Write-Verbose $env: Path
27
-
32
+
28
33
Write-Host Backing up your JEnv Config
29
34
if (test-path $Env: APPDATA \JEnv\jenv.config.json) {
30
35
Copy-Item - Path $Env: APPDATA \JEnv\jenv.config.json - Destination $PSScriptRoot / backups/ jenv.config.bak
@@ -46,46 +51,60 @@ BeforeAll {
46
51
Write-Verbose " Changed SystemPath to:"
47
52
Write-Verbose $systemPath
48
53
$env: Path = $userPath + " ;" + $systemPath
49
-
50
- function Invoke-JEnvCommand {
54
+
55
+ function Invoke-JEnvBatch {
51
56
param (
52
57
$arguments = @ ()
53
58
)
54
-
59
+
55
60
Start-Process - FilePath ((get-item $PSScriptRoot ).parent.fullname + " \jenv.bat" ) - ArgumentList $arguments - Wait - NoNewWindow - RedirectStandardOutput $PSScriptRoot / jenv.test.stdout - RedirectStandardError $PSScriptRoot / jenv.test.stderr
56
61
$stdout = Get-Content - Path jenv.test.stdout
57
62
Remove-Item - Path jenv.test.stdout
58
63
$stderr = Get-Content - Path jenv.test.stderr
59
64
Remove-Item - Path jenv.test.stderr
60
65
return $stdout , $stderr
61
-
66
+
62
67
}
63
-
68
+
64
69
Write-Host -----------------------------------------------
65
70
}
66
-
71
+
67
72
Describe ' JEnv Batch file using correct powershell' {
68
73
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" )
70
75
$stdout | Should - Be @ (' Neither pwsh.exe nor powershell.exe was found in your path.' , ' Please install powershell it is required' )
71
76
}
72
77
It " If theres powershell, it should use it" {
73
78
$env: Path = ($env: Path + " ;" + $PSScriptRoot + " /Fake-Executables/powershell/powershell" )
74
- $stdout , $stderr = Invoke-JEnvCommand @ (" list" )
79
+ $stdout , $stderr = Invoke-JEnvBatch @ (" list" )
75
80
$stdout | Should - Be " JEnv is using powershell"
76
81
}
77
82
It " If theres pwsh, it should use it" {
78
83
$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" )
80
85
$stdout | Should - Be " JEnv is using pwsh"
81
86
}
82
87
It " If theres powershell and pwsh it should use pwsh" {
83
88
$env: Path = ($env: Path + " ;" + $PSScriptRoot + " /Fake-Executables/powershell/powershell" )
84
- $stdout , $stderr = Invoke-JEnvCommand @ (" list" )
89
+ $stdout , $stderr = Invoke-JEnvBatch @ (" list" )
85
90
$stdout | Should - Be " JEnv is using pwsh"
86
91
}
87
92
}
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
+ }
88
101
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
+
89
108
AfterAll {
90
109
Write-Host -----------------------------------------------
91
110
Write-Host Restoring your system from backups
@@ -98,11 +117,11 @@ AfterAll {
98
117
[System.Environment ]::SetEnvironmentVariable(" PATH" , $systemPath , [System.EnvironmentVariableTarget ]::Machine)
99
118
Write-Verbose " Your SystemPath was restored from the backup to:"
100
119
Write-Verbose $systemPath
101
-
120
+
102
121
$env: Path = (Get-Content - Path $PSScriptRoot / backups/ jenv.path.bak)
103
122
Write-Verbose " Your path was restored from the backup to:"
104
123
Write-Verbose $env: Path
105
-
124
+
106
125
Write-Host Restoring your JEnv config
107
126
if (test-path $PSScriptRoot / backups/ jenv.config.bak) {
108
127
Copy-Item - Path $PSScriptRoot / backups/ jenv.config.bak - Destination $Env: APPDATA \JEnv\jenv.config.json
@@ -114,4 +133,3 @@ AfterAll {
114
133
}
115
134
Stop-Transcript
116
135
}
117
-
0 commit comments