Skip to content
This repository was archived by the owner on Jul 16, 2026. It is now read-only.

Commit 753a760

Browse files
Import-TestData: use Write-Host for workflow commands/status; validate GITHUB_ENV
Addresses Copilot review threads: switch the four Write-Output calls (no-op notice, ::add-mask::, and the two 'Exposed N ...' status lines) to Write-Host so they go to the information stream instead of the success/pipeline stream. This keeps the secret embedded in ::add-mask:: out of the function's return value and prevents status text from interfering with callers using Import-TestData in an expression context. Module already carries a module-level PSAvoidUsingWriteHost suppression, so no new suppression is needed. Also validate GITHUB_ENV up front and throw a clear message instead of letting Add-Content fail with a generic parameter-binding error when run outside GitHub Actions. Adds tests/Import-TestData.Tests.ps1 (wired into Action-Test.yml) to close the coverage gap: asserts nothing reaches the success stream, masking still happens on the information stream, the no-op path is silent, and the GITHUB_ENV guard fires.
1 parent 001135a commit 753a760

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

.github/workflows/Action-Test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ jobs:
4242
- name: Run Install-PSModule regression tests
4343
shell: pwsh
4444
run: ./tests/Install-PSModule.Tests.ps1
45+
46+
- name: Run Import-TestData tests
47+
shell: pwsh
48+
run: ./tests/Import-TestData.Tests.ps1

src/Helpers/Helpers.psm1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,9 +1207,12 @@ function Import-TestData {
12071207
param()
12081208

12091209
if ([string]::IsNullOrWhiteSpace($env:PSMODULE_TEST_DATA)) {
1210-
Write-Output 'No test data was provided by the calling workflow.'
1210+
Write-Host 'No test data was provided by the calling workflow.'
12111211
return
12121212
}
1213+
if ([string]::IsNullOrWhiteSpace($env:GITHUB_ENV)) {
1214+
throw 'Import-TestData requires the GITHUB_ENV environment file, which is only available inside GitHub Actions.'
1215+
}
12131216
try {
12141217
$data = $env:PSMODULE_TEST_DATA | ConvertFrom-Json -ErrorAction Stop
12151218
} catch {
@@ -1298,7 +1301,7 @@ function Import-TestData {
12981301
foreach ($line in ($value -split "`n")) {
12991302
$line = $line.TrimEnd("`r")
13001303
if ($line.Length -gt 0) {
1301-
Write-Output "::add-mask::$line"
1304+
Write-Host "::add-mask::$line"
13021305
}
13031306
}
13041307
}
@@ -1312,9 +1315,9 @@ function Import-TestData {
13121315
}
13131316
if ($count -gt 0) {
13141317
if ($Mask) {
1315-
Write-Output "Exposed $count secret value(s) as environment variables."
1318+
Write-Host "Exposed $count secret value(s) as environment variables."
13161319
} else {
1317-
Write-Output "Exposed $count variable value(s) as environment variables."
1320+
Write-Host "Exposed $count variable value(s) as environment variables."
13181321
}
13191322
}
13201323
}

tests/Import-TestData.Tests.ps1

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
$ErrorActionPreference = 'Stop'
5+
6+
$repoRoot = Split-Path -Path $PSScriptRoot -Parent
7+
$helpersManifestPath = Join-Path -Path $repoRoot -ChildPath 'src/Helpers/Helpers.psd1'
8+
$testRoot = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "Import-TestData-$([guid]::NewGuid())"
9+
10+
$assert = {
11+
param(
12+
[bool] $Condition,
13+
[string] $Message
14+
)
15+
16+
if (-not $Condition) {
17+
throw $Message
18+
}
19+
}
20+
21+
$originalTestData = $env:PSMODULE_TEST_DATA
22+
$originalGitHubEnv = $env:GITHUB_ENV
23+
24+
try {
25+
New-Item -Path $testRoot -ItemType Directory -Force | Out-Null
26+
27+
Remove-Module -Name Helpers -Force -ErrorAction SilentlyContinue
28+
Import-Module -Name $helpersManifestPath -Force
29+
30+
$githubEnvPath = Join-Path -Path $testRoot -ChildPath 'github_env'
31+
32+
# Secrets and variables are written to GITHUB_ENV, and nothing leaks to the success/pipeline stream.
33+
Set-Content -Path $githubEnvPath -Value '' -NoNewline
34+
$env:GITHUB_ENV = $githubEnvPath
35+
$env:PSMODULE_TEST_DATA = '{"secrets":{"MY_SECRET":"not-a-real-secret"},"variables":{"MY_VARIABLE":"plain-text-value"}}'
36+
37+
$pipelineOutput = Import-TestData 3> $null 4> $null 5> $null 6> $null
38+
39+
& $assert ($null -eq $pipelineOutput) 'Import-TestData must not write workflow commands or status messages to the success/pipeline output stream.'
40+
41+
$envContent = Get-Content -Path $githubEnvPath -Raw
42+
& $assert ($envContent -match 'MY_SECRET<<') 'Expected the secret to be written to GITHUB_ENV using heredoc syntax.'
43+
& $assert ($envContent -like '*not-a-real-secret*') 'Expected the secret value to be written to GITHUB_ENV.'
44+
& $assert ($envContent -match 'MY_VARIABLE<<') 'Expected the variable to be written to GITHUB_ENV using heredoc syntax.'
45+
& $assert ($envContent -like '*plain-text-value*') 'Expected the variable value to be written to GITHUB_ENV.'
46+
47+
# The mask command is emitted on the information stream (never the success stream).
48+
Set-Content -Path $githubEnvPath -Value '' -NoNewline
49+
$env:PSMODULE_TEST_DATA = '{"secrets":{"MY_SECRET":"not-a-real-secret"}}'
50+
$information = Import-TestData 6>&1 | Out-String
51+
& $assert ($information -match '::add-mask::not-a-real-secret') 'Expected the secret value to be masked via ::add-mask:: on the information stream.'
52+
53+
# A no-op when no test data is provided; nothing on the success/pipeline stream.
54+
$env:PSMODULE_TEST_DATA = ''
55+
$noopOutput = Import-TestData 3> $null 4> $null 5> $null 6> $null
56+
& $assert ($null -eq $noopOutput) 'The no-op path must not emit anything to the success/pipeline output stream.'
57+
58+
# Missing GITHUB_ENV fails fast with a clear message instead of a generic parameter-binding error.
59+
$env:PSMODULE_TEST_DATA = '{"variables":{"MY_VARIABLE":"plain-text-value"}}'
60+
$env:GITHUB_ENV = ''
61+
$clearError = $false
62+
try {
63+
Import-TestData 6> $null
64+
} catch {
65+
$clearError = $_.Exception.Message -like '*GITHUB_ENV*'
66+
}
67+
& $assert $clearError 'Expected a clear error mentioning GITHUB_ENV when the environment file is not available.'
68+
} finally {
69+
$env:PSMODULE_TEST_DATA = $originalTestData
70+
$env:GITHUB_ENV = $originalGitHubEnv
71+
Remove-Module -Name Helpers -Force -ErrorAction SilentlyContinue
72+
if (Test-Path -Path $testRoot) {
73+
Remove-Item -Path $testRoot -Recurse -Force
74+
}
75+
}

0 commit comments

Comments
 (0)