Skip to content

Commit 61751d0

Browse files
Extract shared TestData exposure helper
1 parent cdfd9ae commit 61751d0

5 files changed

Lines changed: 101 additions & 288 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
if ([string]::IsNullOrWhiteSpace($env:PSMODULE_TEST_DATA)) {
2+
Write-Host 'No test data was provided by the calling workflow.'
3+
return
4+
}
5+
try {
6+
$data = $env:PSMODULE_TEST_DATA | ConvertFrom-Json -ErrorAction Stop
7+
} catch {
8+
throw "The 'TestData' secret must be valid JSON with 'secrets' and/or 'variables' maps."
9+
}
10+
if ($null -eq $data -or $data -isnot [pscustomobject]) {
11+
throw "The 'TestData' secret must be a JSON object with 'secrets' and/or 'variables' maps."
12+
}
13+
$allowedTopLevelKeys = @('secrets', 'variables')
14+
foreach ($propertyName in $data.PSObject.Properties.Name) {
15+
if ($allowedTopLevelKeys -notcontains $propertyName) {
16+
throw "The 'TestData' secret only supports 'secrets' and 'variables' maps."
17+
}
18+
}
19+
$reservedNames = @('CI', 'HOME', 'PATH', 'PWD', 'SHELL', 'PSMODULE_TEST_DATA')
20+
$reservedPrefixes = @('GITHUB_', 'RUNNER_', 'ACTIONS_')
21+
function Assert-EnvironmentName {
22+
param([string] $Name)
23+
if ($Name -notmatch '^[A-Za-z_][A-Za-z0-9_]*$') {
24+
throw 'TestData keys must be valid environment variable names.'
25+
}
26+
$normalized = $Name.ToUpperInvariant()
27+
if ($reservedNames -contains $normalized) {
28+
throw 'TestData keys must not override reserved environment variables.'
29+
}
30+
foreach ($prefix in $reservedPrefixes) {
31+
if ($normalized.StartsWith($prefix)) {
32+
throw 'TestData keys must not override reserved environment variables.'
33+
}
34+
}
35+
}
36+
function Assert-Map {
37+
param(
38+
[object] $Map,
39+
[string] $Name
40+
)
41+
if ($null -eq $Map) { return }
42+
if ($Map -isnot [pscustomobject]) {
43+
throw "The 'TestData.$Name' value must be a JSON object."
44+
}
45+
}
46+
function Get-EnvironmentValue {
47+
param(
48+
[object] $Value,
49+
[string] $Name
50+
)
51+
if ($null -eq $Value) { return '' }
52+
if ($Value -is [pscustomobject] -or ($Value -is [System.Collections.IEnumerable] -and $Value -isnot [string])) {
53+
throw "Values in 'TestData.$Name' must be scalar values."
54+
}
55+
return [string]$Value
56+
}
57+
function Add-EnvFromMap {
58+
param(
59+
[object] $Map,
60+
[string] $Name,
61+
[switch] $Mask
62+
)
63+
Assert-Map -Map $Map -Name $Name
64+
if ($null -eq $Map) { return }
65+
$count = 0
66+
foreach ($item in $Map.PSObject.Properties) {
67+
$name = $item.Name
68+
Assert-EnvironmentName -Name $name
69+
$value = Get-EnvironmentValue -Value $item.Value -Name $Name
70+
if ($Mask) {
71+
foreach ($line in ($value -split "`n")) {
72+
$line = $line.TrimEnd("`r")
73+
if ($line.Length -gt 0) {
74+
Write-Host "::add-mask::$line"
75+
}
76+
}
77+
}
78+
do {
79+
$delimiter = "GHENV_$([guid]::NewGuid().ToString('N'))"
80+
} while ($value.Contains($delimiter))
81+
Add-Content -Path $env:GITHUB_ENV -Value "$name<<$delimiter"
82+
Add-Content -Path $env:GITHUB_ENV -Value $value
83+
Add-Content -Path $env:GITHUB_ENV -Value $delimiter
84+
$count++
85+
}
86+
if ($count -gt 0) {
87+
if ($Mask) {
88+
Write-Host "Exposed $count secret value(s) as environment variables."
89+
} else {
90+
Write-Host "Exposed $count variable value(s) as environment variables."
91+
}
92+
}
93+
}
94+
95+
Add-EnvFromMap -Map $data.secrets -Name 'secrets' -Mask
96+
Add-EnvFromMap -Map $data.variables -Name 'variables'

.github/workflows/AfterAll-ModuleLocal.yml

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -36,102 +36,7 @@ jobs:
3636
env:
3737
PSMODULE_TEST_DATA: ${{ secrets.TestData }}
3838
run: |
39-
if ([string]::IsNullOrWhiteSpace($env:PSMODULE_TEST_DATA)) {
40-
Write-Host 'No test data was provided by the calling workflow.'
41-
return
42-
}
43-
try {
44-
$data = $env:PSMODULE_TEST_DATA | ConvertFrom-Json -ErrorAction Stop
45-
} catch {
46-
throw "The 'TestData' secret must be valid JSON with 'secrets' and/or 'variables' maps."
47-
}
48-
if ($null -eq $data -or $data -isnot [pscustomobject]) {
49-
throw "The 'TestData' secret must be a JSON object with 'secrets' and/or 'variables' maps."
50-
}
51-
$allowedTopLevelKeys = @('secrets', 'variables')
52-
foreach ($propertyName in $data.PSObject.Properties.Name) {
53-
if ($allowedTopLevelKeys -notcontains $propertyName) {
54-
throw "The 'TestData' secret only supports 'secrets' and 'variables' maps."
55-
}
56-
}
57-
$reservedNames = @('CI', 'HOME', 'PATH', 'PWD', 'SHELL', 'PSMODULE_TEST_DATA')
58-
$reservedPrefixes = @('GITHUB_', 'RUNNER_', 'ACTIONS_')
59-
function Assert-EnvironmentName {
60-
param([string] $Name)
61-
if ($Name -notmatch '^[A-Za-z_][A-Za-z0-9_]*$') {
62-
throw "TestData keys must be valid environment variable names."
63-
}
64-
$normalized = $Name.ToUpperInvariant()
65-
if ($reservedNames -contains $normalized) {
66-
throw "TestData keys must not override reserved environment variables."
67-
}
68-
foreach ($prefix in $reservedPrefixes) {
69-
if ($normalized.StartsWith($prefix)) {
70-
throw "TestData keys must not override reserved environment variables."
71-
}
72-
}
73-
}
74-
function Assert-Map {
75-
param(
76-
[object] $Map,
77-
[string] $Name
78-
)
79-
if ($null -eq $Map) { return }
80-
if ($Map -isnot [pscustomobject]) {
81-
throw "The 'TestData.$Name' value must be a JSON object."
82-
}
83-
}
84-
function Get-EnvironmentValue {
85-
param(
86-
[object] $Value,
87-
[string] $Name
88-
)
89-
if ($null -eq $Value) { return '' }
90-
if ($Value -is [pscustomobject] -or ($Value -is [System.Collections.IEnumerable] -and $Value -isnot [string])) {
91-
throw "Values in 'TestData.$Name' must be scalar values."
92-
}
93-
return [string]$Value
94-
}
95-
function Add-EnvFromMap {
96-
param(
97-
[object] $Map,
98-
[string] $Name,
99-
[switch] $Mask
100-
)
101-
Assert-Map -Map $Map -Name $Name
102-
if ($null -eq $Map) { return }
103-
$count = 0
104-
foreach ($item in $Map.PSObject.Properties) {
105-
$name = $item.Name
106-
Assert-EnvironmentName -Name $name
107-
$value = Get-EnvironmentValue -Value $item.Value -Name $Name
108-
if ($Mask) {
109-
foreach ($line in ($value -split "`n")) {
110-
$line = $line.TrimEnd("`r")
111-
if ($line.Length -gt 0) {
112-
Write-Host "::add-mask::$line"
113-
}
114-
}
115-
}
116-
do {
117-
$delimiter = "GHENV_$([guid]::NewGuid().ToString('N'))"
118-
} while ($value.Contains($delimiter))
119-
Add-Content -Path $env:GITHUB_ENV -Value "$name<<$delimiter"
120-
Add-Content -Path $env:GITHUB_ENV -Value $value
121-
Add-Content -Path $env:GITHUB_ENV -Value $delimiter
122-
$count++
123-
}
124-
if ($count -gt 0) {
125-
if ($Mask) {
126-
Write-Host "Exposed $count secret value(s) as environment variables."
127-
} else {
128-
Write-Host "Exposed $count variable value(s) as environment variables."
129-
}
130-
}
131-
}
132-
133-
Add-EnvFromMap -Map $data.secrets -Name 'secrets' -Mask
134-
Add-EnvFromMap -Map $data.variables -Name 'variables'
39+
./.github/scripts/Expose-TestData.ps1
13540
13641
- name: Run AfterAll Teardown Scripts
13742
if: always()

.github/workflows/BeforeAll-ModuleLocal.yml

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -36,102 +36,7 @@ jobs:
3636
env:
3737
PSMODULE_TEST_DATA: ${{ secrets.TestData }}
3838
run: |
39-
if ([string]::IsNullOrWhiteSpace($env:PSMODULE_TEST_DATA)) {
40-
Write-Host 'No test data was provided by the calling workflow.'
41-
return
42-
}
43-
try {
44-
$data = $env:PSMODULE_TEST_DATA | ConvertFrom-Json -ErrorAction Stop
45-
} catch {
46-
throw "The 'TestData' secret must be valid JSON with 'secrets' and/or 'variables' maps."
47-
}
48-
if ($null -eq $data -or $data -isnot [pscustomobject]) {
49-
throw "The 'TestData' secret must be a JSON object with 'secrets' and/or 'variables' maps."
50-
}
51-
$allowedTopLevelKeys = @('secrets', 'variables')
52-
foreach ($propertyName in $data.PSObject.Properties.Name) {
53-
if ($allowedTopLevelKeys -notcontains $propertyName) {
54-
throw "The 'TestData' secret only supports 'secrets' and 'variables' maps."
55-
}
56-
}
57-
$reservedNames = @('CI', 'HOME', 'PATH', 'PWD', 'SHELL', 'PSMODULE_TEST_DATA')
58-
$reservedPrefixes = @('GITHUB_', 'RUNNER_', 'ACTIONS_')
59-
function Assert-EnvironmentName {
60-
param([string] $Name)
61-
if ($Name -notmatch '^[A-Za-z_][A-Za-z0-9_]*$') {
62-
throw "TestData keys must be valid environment variable names."
63-
}
64-
$normalized = $Name.ToUpperInvariant()
65-
if ($reservedNames -contains $normalized) {
66-
throw "TestData keys must not override reserved environment variables."
67-
}
68-
foreach ($prefix in $reservedPrefixes) {
69-
if ($normalized.StartsWith($prefix)) {
70-
throw "TestData keys must not override reserved environment variables."
71-
}
72-
}
73-
}
74-
function Assert-Map {
75-
param(
76-
[object] $Map,
77-
[string] $Name
78-
)
79-
if ($null -eq $Map) { return }
80-
if ($Map -isnot [pscustomobject]) {
81-
throw "The 'TestData.$Name' value must be a JSON object."
82-
}
83-
}
84-
function Get-EnvironmentValue {
85-
param(
86-
[object] $Value,
87-
[string] $Name
88-
)
89-
if ($null -eq $Value) { return '' }
90-
if ($Value -is [pscustomobject] -or ($Value -is [System.Collections.IEnumerable] -and $Value -isnot [string])) {
91-
throw "Values in 'TestData.$Name' must be scalar values."
92-
}
93-
return [string]$Value
94-
}
95-
function Add-EnvFromMap {
96-
param(
97-
[object] $Map,
98-
[string] $Name,
99-
[switch] $Mask
100-
)
101-
Assert-Map -Map $Map -Name $Name
102-
if ($null -eq $Map) { return }
103-
$count = 0
104-
foreach ($item in $Map.PSObject.Properties) {
105-
$name = $item.Name
106-
Assert-EnvironmentName -Name $name
107-
$value = Get-EnvironmentValue -Value $item.Value -Name $Name
108-
if ($Mask) {
109-
foreach ($line in ($value -split "`n")) {
110-
$line = $line.TrimEnd("`r")
111-
if ($line.Length -gt 0) {
112-
Write-Host "::add-mask::$line"
113-
}
114-
}
115-
}
116-
do {
117-
$delimiter = "GHENV_$([guid]::NewGuid().ToString('N'))"
118-
} while ($value.Contains($delimiter))
119-
Add-Content -Path $env:GITHUB_ENV -Value "$name<<$delimiter"
120-
Add-Content -Path $env:GITHUB_ENV -Value $value
121-
Add-Content -Path $env:GITHUB_ENV -Value $delimiter
122-
$count++
123-
}
124-
if ($count -gt 0) {
125-
if ($Mask) {
126-
Write-Host "Exposed $count secret value(s) as environment variables."
127-
} else {
128-
Write-Host "Exposed $count variable value(s) as environment variables."
129-
}
130-
}
131-
}
132-
133-
Add-EnvFromMap -Map $data.secrets -Name 'secrets' -Mask
134-
Add-EnvFromMap -Map $data.variables -Name 'variables'
39+
./.github/scripts/Expose-TestData.ps1
13540
13641
- name: Run BeforeAll Setup Scripts
13742
uses: PSModule/GitHub-Script@1ee97bbc652d19c38ae12f6e1e47e9d9fbd12d0a # v1.8.0

0 commit comments

Comments
 (0)