Skip to content

Commit 5040369

Browse files
committed
(#3681) Add tests for System Account variables
This commit adds Pester tests to run `refreshenv` as the System account and ensure that it does not inadvertently pick up the wrong temp variables.
1 parent e1175ba commit 5040369

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function ConvertTo-Base64String {
2+
<#
3+
.Synopsis
4+
Helper function to Convert a string to a Base64 encoded string.
5+
#>
6+
[CmdletBinding()]
7+
param(
8+
# The string to be converted to base64.
9+
[Parameter(ValueFromPipeline)]
10+
[string]$InputObject
11+
)
12+
$bytes = [system.text.encoding]::Unicode.GetBytes($InputObject)
13+
[Convert]::ToBase64String($bytes)
14+
}

tests/pester-tests/features/EnvironmentVariables.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,32 @@ Describe "Ensuring Chocolatey Environment variables are not present (<_>)" -Tag
8787
$Output.Lines | Should -Not -Contain $ExpectedLine -Because $Output.String
8888
}
8989
}
90+
91+
Describe "Ensuring variables are not incorrectly set for the SYSTEM account" -Tag EnvironmentVariables, Chocolatey, SystemAccount {
92+
BeforeAll {
93+
Initialize-ChocolateyTestInstall
94+
New-ChocolateyInstallSnapshot
95+
Invoke-Choco install psexec -s https://community.chocolatey.org/api/v2/
96+
97+
# We execute this as an encoded command as it doesn't seem to like the strings unless it's encoded.
98+
$encodedCommand = @'
99+
"Temp: $($env:TEMP)" > {1}/before.txt
100+
"Tmp: $($env:TMP)" >> {1}/before.txt
101+
Import-Module {0}/helpers/chocolateyProfile.psm1 -Verbose *>&1
102+
refreshenv
103+
"Temp: $($env:TEMP)" > {1}/after.txt
104+
"Tmp: $($env:TMP)" >> {1}/after.txt
105+
'@ -f (Get-ChocolateyTestLocation), $PSScriptRoot | ConvertTo-Base64String
106+
$Output = psexec /accepteula /s powershell.exe -NoProfile -EncodedCommand $encodedCommand 2>$null
107+
}
108+
109+
AfterAll {
110+
Remove-ChocolateyTestInstall
111+
}
112+
113+
It 'Should not change the temp environment variables' {
114+
$Before = Get-Content $PSScriptRoot/before.txt
115+
$After = Get-Content $PSScriptRoot/after.txt
116+
$Before | Should -Be $After -Because $Output
117+
}
118+
}

0 commit comments

Comments
 (0)