Skip to content

Commit a73d106

Browse files
Showcase passing a secret and variable to tests via TestData
1 parent 8d00954 commit a73d106

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

.github/workflows/Process-PSModule.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ jobs:
3030
uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@ea19a3eb1293246d1b00e4faae1544442c3be0ec # v6.1.1
3131
secrets:
3232
APIKEY: ${{ secrets.APIKEY }}
33+
# Showcase: send data down to the module tests (see tests/Environment.Tests.ps1).
34+
# TestData is a single-line JSON object with optional "secrets" (masked in logs) and
35+
# "variables" (not masked) maps. Every entry is exposed to the module test jobs as
36+
# $env:<name>. Reference a repository secret with the direct "${{ secrets.NAME }}"
37+
# form (single-line values, no quotes or backslashes) and a repository variable with
38+
# ${{ toJSON(vars.NAME) }} so any characters are encoded safely. The folded ">-" scalar
39+
# keeps the whole value on one line so GitHub registers a single log mask. Omit
40+
# TestData entirely when your tests need no data.
41+
TestData: >-
42+
{ "secrets": { "TEST_SECRET": "${{ secrets.TEST_SECRET }}" },
43+
"variables": { "TEST_VARIABLE": ${{ toJSON(vars.TEST_VARIABLE) }} } }

tests/Environment.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
2+
'PSReviewUnusedParameter', '',
3+
Justification = 'Required for Pester tests'
4+
)]
5+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
6+
'PSUseDeclaredVarsMoreThanAssignments', '',
7+
Justification = 'Required for Pester tests'
8+
)]
9+
[CmdletBinding()]
10+
param()
11+
12+
Describe 'TestData is exposed to the module tests' {
13+
# Showcase for the Process-PSModule 'TestData' feature. The calling workflow
14+
# (.github/workflows/Process-PSModule.yml) passes a repository secret and a repository
15+
# variable through a single TestData object, and the framework exposes each of them to
16+
# the module tests as an environment variable. To see these run, add a repository secret
17+
# named 'TEST_SECRET' and a repository variable named 'TEST_VARIABLE'. When they are not
18+
# configured the tests skip, so a fresh repository created from this template stays green.
19+
20+
It 'Exposes the secret from the "secrets" map as $env:TEST_SECRET' -Skip:([string]::IsNullOrEmpty($env:TEST_SECRET)) {
21+
# Values in the "secrets" map are masked in the workflow logs.
22+
$env:TEST_SECRET | Should -Not -BeNullOrEmpty
23+
}
24+
25+
It 'Exposes the variable from the "variables" map as $env:TEST_VARIABLE' -Skip:([string]::IsNullOrEmpty($env:TEST_VARIABLE)) {
26+
# Values in the "variables" map are not masked.
27+
$env:TEST_VARIABLE | Should -Not -BeNullOrEmpty
28+
}
29+
}

0 commit comments

Comments
 (0)