From 3dc4416e4ef5ea2340cfa7b161e4c86177e31fc7 Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Sun, 25 Jun 2023 12:39:09 -0700 Subject: [PATCH] (#3225) Add Pester Tests to ensure environment Make use of the test-environment package that already exists to get the environment variables set by Chocolatey and ensure that key variables are set as they should be. --- .../features/EnvironmentVariables.Tests.ps1 | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/chocolatey-tests/features/EnvironmentVariables.Tests.ps1 diff --git a/tests/chocolatey-tests/features/EnvironmentVariables.Tests.ps1 b/tests/chocolatey-tests/features/EnvironmentVariables.Tests.ps1 new file mode 100644 index 000000000..cd3a6a2fa --- /dev/null +++ b/tests/chocolatey-tests/features/EnvironmentVariables.Tests.ps1 @@ -0,0 +1,43 @@ +Describe "Ensuring Chocolatey Environment variables are correct (<_>)" -ForEach @( + "config" + "cli" +) -Tag EnvironmentVariables, Chocolatey { + BeforeDiscovery { + $TestedVariables = @( + @{ Name = 'TEMP' ; Value = "C:\Temp\$PID" } + @{ Name = 'TMP' ; Value = "C:\Temp\$PID" } + @{ Name = 'ChocolateyPackageFolder' ; Value = '{0}\lib\test-environment' } + @{ Name = 'ChocolateyPackageName' ; Value = 'test-environment' } + @{ Name = 'ChocolateyPackageTitle' ; Value = 'test-environment (Install)' } + @{ Name = 'ChocolateyPackageVersion' ; Value = '1.0.0' } + ) + } + + BeforeAll { + Initialize-ChocolateyTestInstall + New-ChocolateyInstallSnapshot + $cacheDir = "C:\Temp\$PID" + switch ($_) { + 'config' { + Invoke-Choco config set --name=cachelocation --value $cacheDir + } + 'cli' { + $cacheArg = "--cache-location='$cacheDir'" + } + } + $Output = Invoke-Choco install test-environment --version 1.0.0 $cacheArg + } + + AfterAll { + Remove-ChocolateyTestInstall + } + + It "Should exit with success (0)" { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It 'Should Output the expected value for environment variable' -ForEach $TestedVariables { + $ExpectedLine = "$Name=$Value" -f $env:ChocolateyInstall + $Output.Lines | Should -Contain $ExpectedLine -Because $Output.String + } +}