forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#310) Add tests for some helper commands
- Loading branch information
Showing
7 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
tests/pester-tests/powershell-commands/Get-EnvironmentVariable.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '%[^%;\]+%' | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/pester-tests/powershell-commands/Get-EnvironmentVariableNames.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/pester-tests/powershell-commands/Install-ChocolateyPath.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/pester-tests/powershell-commands/Set-EnvironmentVariable.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/pester-tests/powershell-commands/Test-ProcessAdminRights.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/pester-tests/powershell-commands/Uninstall-ChocolateyPath.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/pester-tests/powershell-commands/Update-SessionEnvironment.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |