Skip to content

Commit

Permalink
(chocolatey#310) Add tests for some helper commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vexx32 committed May 6, 2024
1 parent 3276361 commit 00cba70
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Describe 'Get-EnvironmentVariable helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Gets the named environment variable value at the <Scope> scope' -TestCases @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
BeforeDiscovery {
$variables = [Environment]::GetEnvironmentVariables($Scope).Keys | ForEach-Object { @{ Name = $_ } }
}

It 'Gets the environment variable "<Name>"' -TestCases $variables {
$expectedValue = [Environment]::GetEnvironmentVariable($Name, $Scope)
Get-EnvironmentVariable -Name $Name -Scope $Scope | Should -BeExactly $expectedValue
}
}

Context 'Can retrieve the PATH variable without expanding environment names for the <Scope> scope' {
It 'Retrieves the <Scope> PATH value with un-expanded environment names' {
Get-EnvironmentVariable -Name 'PATH' -Scope 'Machine' | Should -Match '%[^%;\]+%'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Describe 'Get-EnvironmentVariable helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Gets the named environment variable value at the target scope' -TestCases @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
BeforeDiscovery {
$variables = | ForEach-Object { @{ Name = $_ } }
}

It 'Gets the environment variable "<Name>"' -TestCases $variables {
$expectedValue = [Environment]::GetEnvironmentVariables($Scope).Keys
Get-EnvironmentVariableNames -Scope $Scope | Should -Be $expectedValue
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Describe 'Install-ChocolateyPath helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Adding and removing PATH values' -TestCases @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
Context 'Path "<_>"' -TestCases @("C:\test", "C:\tools") {
It 'stores the value in the desired PATH scope' {
Install-ChocolateyPath -Path $_ -Scope $Scope
[Environment]::GetEnvironmentVariable('PATH', $_, $Scope) -split [IO.Path]::PathSeparator | Should -Contain $_
}
}
}

Context 'Edge cases' {
AfterEach {
Uninstall-ChocolateyPath -Path "C:\test" -Scope Process
}

It 'successfully detects that a path is already present when it is missing a trailing slash that is present in PATH' {
Install-ChocolateyPath -Path "C:\test\" -Scope Process
Install-ChocolateyPath -Path "C:\test" -Scope Process

@($env:PATH -split [IO.Path]::PathSeparator) -match "C:\test" | Should -HaveCount 1 -Because "Install-ChocolateyPath should not add the same path more than once"
}

It 'successfully detects that a path is already present when it has a trailing slash that is not present in PATH' {
Install-ChocolateyPath -Path "C:\test" -Scope Process
Install-ChocolateyPath -Path "C:\test\" -Scope Process

@($env:PATH -split [IO.Path]::PathSeparator) -match "C:\test" | Should -HaveCount 1 -Because "Install-ChocolateyPath should not add the same path more than once"
}

It 'allows a subpath of a path already in PATH to be added' {
Install-ChocolateyPath -Path "C:\test" -Scope Process
Install-ChocolateyPath -Path "C:\test\subpath" -Scope Process

@($env:PATH -split [IO.Path]::PathSeparator) | Should -Contain "C:\test\subpath"
}

AfterAll {
Uninstall-ChocolateyPath -Path "C:\test" -Scope Process
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Describe 'Set-EnvironmentVariable helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Sets an environment variable value at the target scope' -TestCases @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
BeforeDiscovery {
$variables = @(
@{ Name = "Test"; Value = "TestValue" }
@{ Name = "Environment"; Value = "1234" }
@{ Name = "Variable"; Value = "C:\test\path" }
)
}

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

if ($Scope -ne 'Process') {
[Environment]::GetEnvironmentVariable($Name) | Should -BeExactly $Value
}
}

AfterAll {
Set-EnvironmentVariable -Name $Name -Value "" -Scope $Scope
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Describe 'Test-ProcessAdminRights helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

It 'should report true in an admin context' {
Test-ProcessAdminRights | Should -BeTrue -Because "We should run these tests exclusively as admin"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Describe 'Uninstall-ChocolateyPath helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Adding and removing PATH values' -TestCases @(
@{ Scope = 'Process' }
@{ Scope = 'User' }
@{ Scope = 'Machine' }
) {
Context 'Path "<_>"' -TestCases @("C:\test", "C:\tools") {
BeforeEach {
Install-ChocolateyPath -Path $_ -Scope $Scope
}

It 'removes a stored PATH value in the desired PATH scope' {
Uninstall-ChocolateyPath -Path $_ -Scope $Scope
[Environment]::GetEnvironmentVariable('PATH', $_, $Scope) -split [IO.Path]::PathSeparator | Should -Not -Contain $_
}
}
}

Context 'Edge cases' {
It 'successfully detects that a path is present and removes it when it is missing a trailing slash that is present in PATH' {
Install-ChocolateyPath -Path "C:\test\" -Scope Process
Uninstall-ChocolateyPath -Path "C:\test" -Scope Process

@($env:PATH -split [IO.Path]::PathSeparator) -match "C:\test" | Should -BeNullOrEmpty
}

It 'successfully detects that a path is present and removes it when it has a trailing slash that is not present in PATH' {
Install-ChocolateyPath -Path "C:\test" -Scope Process
Uninstall-ChocolateyPath -Path "C:\test\" -Scope Process

@($env:PATH -split [IO.Path]::PathSeparator) -match "C:\test" | Should -BeNullOrEmpty
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Describe 'Update-SessionEnvironment helper function tests' {
BeforeAll {
Initialize-ChocolateyTestInstall

$testLocation = Get-ChocolateyTestLocation
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
}

Context 'Refreshing environment' {
BeforeAll {
[Environment]::SetEnvironmentVariable("Test", "user-value", "User")
[Environment]::SetEnvironmentVariable("Test2", "machine-value", "Machine")
$env:Test3 = "process-value"
}

It 'successfully refreshes values into the process' {
$env:Test | Should -BeExactly 'user-value'
$env:Test2 | Should -BeExactly 'machine-value'
$env:Test3 | Should -BeNullOrEmpty -Because 'Process-only values should not be preserved'
}

AfterAll {
[Environment]::SetEnvironmentVariable("Test", [string]::Empty, "User")
[Environment]::SetEnvironmentVariable("Test2", [string]::Empty, "Machine")
}
}
}

0 comments on commit 00cba70

Please sign in to comment.