Skip to content

Commit

Permalink
(chocolatey#310) Fix test issues
Browse files Browse the repository at this point in the history
Fix up Set-EnvironmentVariable and Update-SessionEnvironment tests.
  • Loading branch information
vexx32 authored and gep13 committed May 30, 2024
1 parent 71df5cc commit dfc2b92
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ Describe 'Get-EnvironmentVariable helper function tests' -Tags Cmdlets {
}

Context 'Can retrieve the PATH variable without expanding environment names for the Machine scope' {
BeforeAll {
Install-ChocolateyPath -Path "%systemroot%\test" -PathType Machine
}

AfterAll {
Uninstall-ChocolateyPath -Path "%systemroot%\test" -PathType Machine
}

It 'Retrieves the Machine PATH value with un-expanded environment names' {
# We expect there to be an entry similar to the following: "%SystemRoot%\system32", since this
# is there by default in a Windows install
Get-EnvironmentVariable -Name 'PATH' -Scope 'Machine' | Should -Match '%[^%;\\]+%'
Get-EnvironmentVariable -Name 'PATH' -Scope 'Machine' -PreserveVariables | Should -Match '%systemroot%\\test'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ Describe 'Set-EnvironmentVariable helper function tests' -Tags Cmdlets {
}

Describe 'Setting environment variable <Name>' -ForEach $variables {
It 'Sets the target environment variable in the proper scope, as well as current process scope' {
BeforeAll {
Set-EnvironmentVariable -Name $Name -Value $Value -Scope $Scope
[Environment]::GetEnvironmentVariable($Name, $Scope) | Should -BeExactly $Value

# We are explicitly checking the Process variable here.
[Environment]::GetEnvironmentVariable($Name, 'Process') | Should -BeExactly $Value
}

AfterAll {
Set-EnvironmentVariable -Name $Name -Value "" -Scope $Scope
}

It 'sets the target environment variable in the proper scope' {
[Environment]::GetEnvironmentVariable($Name, $Scope) | Should -BeExactly $Value
}

It 'propagates the change to the current process' {
Get-Content "Env:\$Name" | Should -BeExactly $Value
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
Update-SessionEnvironment
$env:Test | Should -BeExactly 'user-value'
$env:Test2 | Should -BeExactly 'machine-value'
$env:Test3 | Should -BeNullOrEmpty -Because 'Process-only values should not be preserved'
$env:Test3 | Should -BeExactly 'process-value'
}

AfterAll {
[Environment]::SetEnvironmentVariable("Test", [string]::Empty, "User")
[Environment]::SetEnvironmentVariable("Test2", [string]::Empty, "Machine")
$env:Test3 = ''
}
}
}

0 comments on commit dfc2b92

Please sign in to comment.