Skip to content

🚀 [Feature]: Import-TestData is available from the shared Helpers module#20

Merged
Marius Storhaug (MariusStorhaug) merged 5 commits into
mainfrom
feat/import-testdata
Jul 10, 2026
Merged

🚀 [Feature]: Import-TestData is available from the shared Helpers module#20
Marius Storhaug (MariusStorhaug) merged 5 commits into
mainfrom
feat/import-testdata

Conversation

@MariusStorhaug

@MariusStorhaug Marius Storhaug (MariusStorhaug) commented Jul 10, 2026

Copy link
Copy Markdown
Member

The shared Helpers module now provides an Import-TestData command, so every repo that installs the helpers gets it automatically — the same way Install-PSModule is already provided. It reads the PSMODULE_TEST_DATA environment variable (a single-line JSON object with optional secrets and variables maps) and exposes each entry as an environment variable for the remaining steps in the same job, masking secret values in the logs.

New: Import-TestData in the Helpers module

Test data no longer has to be wired up per repo. Previously this logic lived in a caller-shipped ./.github/scripts/Expose-TestData.ps1 that every consumer had to carry, and most never did (Template-PSModule, Confluence, …), so the Test/BeforeAll/AfterAll-ModuleLocal workflows failed with script not found. Shipping the logic in the installed Helpers module means every consumer gets it for free.

Call it from any step after the helpers are installed:

- name: Expose caller-provided test data
  shell: pwsh
  env:
    PSMODULE_TEST_DATA: ${{ secrets.TestData }}
  run: Import-TestData

Behavior:

  • secrets values are masked in the logs via ::add-mask::; variables values are not.
  • Keys are validated: they must be legal environment-variable names, must not override reserved or GitHub-provided variables, must not be duplicated across the two maps, and values must be scalar.
  • When no data is provided it is a no-op.
  • Outside GitHub Actions (no GITHUB_ENV) it fails fast with a clear message instead of a generic parameter-binding error.

Technical Details

  • src/Helpers/Helpers.psm1: adds the Import-TestData function. Its validators (Assert-EnvironmentName, Assert-Map, Get-EnvironmentValue, Add-EnvFromMap) are nested inside it so they stay private — the manifest has no FunctionsToExport, so every top-level function is exported and nesting avoids leaking generic helper names. Named Import-TestData (not Expose-…) because module functions must use an approved verb (PSUseApprovedVerbs is enforced by super-linter across the ecosystem).
  • Values are written to GITHUB_ENV using heredoc syntax, and the writes are terminating (-ErrorAction Stop) even when the caller's ErrorActionPreference is Continue.
  • tests/Import-TestData.Tests.ps1: new standalone test covering masking, GITHUB_ENV heredoc output, the no-op path, missing GITHUB_ENV, an unwritable GITHUB_ENV path, and every validation error (invalid JSON, unexpected top-level key, invalid env name, reserved override, duplicate across maps, non-scalar value).
  • .github/workflows/Action-Test.yml: runs the new test alongside the existing Install-PSModule regression test.
  • Validation: PSScriptAnalyzer clean with the repo settings; the module imports without an unapproved-verb warning; Import-TestData is exported while the validators stay private; the existing regression test still passes.
  • Release chain: release Install-PSModuleHelpers with Import-TestData → pin Process-PSModule to that version → Template-PSModule bumps.

Exposes caller-provided TestData (the PSMODULE_TEST_DATA JSON blob with optional 'secrets' and 'variables' maps) as environment variables for later steps: 'secrets' values are masked via ::add-mask::, 'variables' are not. Ships the logic in the installed Helpers module so every consumer gets it, instead of a caller-shipped .github/scripts/Expose-TestData.ps1.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an Import-TestData function to the shared Helpers PowerShell module so GitHub Actions workflows can provide a JSON payload via PSMODULE_TEST_DATA, have it validated, and then exported to subsequent job steps through GITHUB_ENV (with secrets masked via ::add-mask::).

Changes:

  • Added Import-TestData cmdlet that parses PSMODULE_TEST_DATA as JSON and supports secrets + variables sections.
  • Implemented validation for allowed top-level keys, env-var-safe names, reserved name/prefix protections, scalar-only values, and duplicate key prevention across maps.
  • Implemented GITHUB_ENV heredoc writing and per-line masking for secret values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Helpers/Helpers.psm1
Comment thread src/Helpers/Helpers.psm1 Outdated
Comment thread src/Helpers/Helpers.psm1
…e 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.
Copilot AI review requested due to automatic review settings July 10, 2026 21:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread src/Helpers/Helpers.psm1
Comment thread tests/Import-TestData.Tests.ps1 Outdated
Comment thread tests/Import-TestData.Tests.ps1 Outdated
Comment thread tests/Import-TestData.Tests.ps1
super-linter enforces PSScriptAnalyzer's PSAvoidLongLines (max 150), which is not in the default local ruleset; shorten the ::add-mask:: assertion message.
Copilot AI review requested due to automatic review settings July 10, 2026 21:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/Helpers/Helpers.psm1
…dator tests

Fixes a real bug in Add-EnvFromMap: the loop variable \ clobbered the case-insensitive \ parameter, so Get-EnvironmentValue received the entry key (e.g. MY_SECRET) instead of the section name (secrets/variables), producing misleading 'TestData.<key>' error messages. Renamed the loop variable to \ so \ keeps the section name.

Makes the no-op path silent by default (Write-Verbose instead of Write-Host) so unconditionally calling Import-TestData in workflows adds no log noise when no data is provided.

Tests: initialise the GITHUB_ENV fixture with -Encoding utf8 to match Add-Content, and add representative validation failure-case assertions (invalid JSON, unknown top-level key, invalid/reserved env-var names, duplicate keys across maps). The non-scalar case asserts the error names the section ('TestData.secrets'), which is a regression guard for the clobber bug above.
Copilot AI review requested due to automatic review settings July 10, 2026 21:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/Helpers/Helpers.psm1 Outdated
… Stop

Add-Content emits non-terminating errors by default, so a failed write to GITHUB_ENV (e.g. an unwritable path) could let the function continue and even log 'Exposed ...' while nothing was written. Add -ErrorAction Stop to the three writes so a write failure is always terminating, independent of the caller's ErrorActionPreference.

Adds a regression test that points GITHUB_ENV at a non-existent directory under ErrorActionPreference=Continue and asserts Import-TestData throws.
Copilot AI review requested due to automatic review settings July 10, 2026 21:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@MariusStorhaug Marius Storhaug (MariusStorhaug) changed the title Add Import-TestData to the Helpers module 🚀 [Feature]: Import-TestData is available from the shared Helpers module Jul 10, 2026
@MariusStorhaug Marius Storhaug (MariusStorhaug) merged commit 8bfb84d into main Jul 10, 2026
19 checks passed
@MariusStorhaug Marius Storhaug (MariusStorhaug) deleted the feat/import-testdata branch July 10, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants