Skip to content

test: run the suites under strict mode - #156

Merged
fadwen merged 2 commits into
mainfrom
test/strict-mode-suites
Nov 26, 2026
Merged

fadwen merged 2 commits into
mainfrom
test/strict-mode-suites

Conversation

@fadwen

@fadwen fadwen commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Summary

Part of #70 (ERR-03). The module runs under Set-StrictMode -Version Latest; the suites did not, and Tests/Invoke-Tests.ps1 turned strict mode off around the Pester run for that reason. With it on, 47 unit tests failed on PowerShell 7.6, all in test code: none points at the module. The validation kit's own failures that #70 counted are gone since #125. This makes the suites pass under strict mode and removes the switch.

Changes

  • Mock bodies that read an argument the call may leave out read it from $PesterBoundParameters, which gives $null for it; Pester makes no variable for an argument that was not bound (Invoke-IslWin32ContentCommand, Test-IslPolicyScript, Get-IslExportReport).
  • Tests/TestHelpers/TestHelpers.ps1 gains Test-ParameterMandatory and Get-ParameterValidValue, which read the Parameter and ValidateSet attributes of a parameter only. Ten files read .Attributes.Mandatory and .Attributes.ValidValues across all of a parameter's attributes, and on a command without the parameter, which strict mode refuses; they use the helpers, and the four that did not load TestHelpers.ps1 now do.
  • Repair-IntuneScript.Tests.ps1: the cases that give no Architecture are read with Get-Variable -ValueOnly -ErrorAction SilentlyContinue, since a key a -ForEach case leaves out is no variable.
  • Two test names used <Limit> and <value> without -ForEach data, which Pester expands as variables; they name the values instead.
  • Test-IntuneScript.Tests.ps1 reads RuleName from each finding rather than from a result that can be empty; the two Graph request suites set $script:StubCreated on both branches; the Win32 test of a run as the current user asserts that no credential was passed.
  • Tests/Invoke-Tests.ps1 runs Pester under its own strict mode. CLAUDE.md says how a mock and a parameter test are written for it; CHANGELOG.md has a Changed entry.

Verification

  • Every suite, unit, validation-kit unit and integration, under Set-StrictMode -Version Latest on PowerShell 7.6, not elevated: 2,312 passed, 0 failed, 8 skipped before the last three fixes, then those three files 21 of 21.
  • ./Build/Invoke-Build.ps1 with the runner's switch removed: 2,262 passed on PowerShell 7.6 and 2,226 passed (37 skipped) on Windows PowerShell 5.1, no failures; PSScriptAnalyzer clean in 355 files.
  • The integration suites that need elevation or a lab account skip themselves here; CI's elevated runners run them under strict mode for the first time on this PR.

Notes

ERR-03. 47 unit tests failed with strict mode on: mock bodies read arguments the call had not bound, tests read Mandatory and ValidValues across all of a parameter's attributes, two test names used <...> templates without data, and a few read a property of an empty result or a variable set on one branch only. Mocks read from PesterBoundParameters, two helpers in TestHelpers read the attributes, and Tests/Invoke-Tests.ps1 no longer turns strict mode off around the Pester run.

Part of #70

@fadwen-ai fadwen-ai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Three nits, all in the new helpers in Tests/TestHelpers/TestHelpers.ps1. The rest of the change looks right: the $PesterBoundParameters reads in the mocks, the Get-Variable reads for -ForEach keys a case leaves out, the $script:StubCreated initialisation, and the Win32 empty-credential assertion, which matches Invoke-IslWin32ContentCommand passing -Credential only for another account.

  1. Get-ParameterValidValue fails with a strict-mode error for a parameter the command lacks, which its help and its sibling helper don't lead you to expect.
  2. Test-ParameterMandatory returns $false for an unknown parameter, so a Should-BeFalse assertion passes vacuously after a rename.
  3. STY-07: one blank line before Test-ParameterMandatory, where the file uses two.

Out of scope: none

🤖 Generated with Claude Code

Comment thread Tests/TestHelpers/TestHelpers.ps1 Resolved
Comment thread Tests/TestHelpers/TestHelpers.ps1 Resolved
Comment thread Tests/TestHelpers/TestHelpers.ps1 Resolved
…elpers

Test-ParameterMandatory returned false for an unknown name, so a test that a parameter is optional passed when it was renamed or removed, and Get-ParameterValidValue read Attributes from null under strict mode. Both now end with ParameterNotFound; the input-length test checks that a command has -Path before asking whether it is mandatory.
@fadwen

fadwen commented Nov 26, 2026

Copy link
Copy Markdown
Owner Author

Round 1: 3 fixed, 0 not changed.

  • Tests/TestHelpers/TestHelpers.ps1:533: Get-ParameterValidValue refuses an unknown parameter with ParameterNotFound instead of failing on .Attributes of null.
  • Tests/TestHelpers/TestHelpers.ps1:482: Test-ParameterMandatory refuses an unknown parameter too, so a test that a parameter is optional fails when it is removed; the input-length test checks for -Path before asking.
  • Tests/TestHelpers/TestHelpers.ps1:443: STY-07 spacing.

The ten files that use the helpers: 183 passed under strict mode on PowerShell 7.6 and 5.1; PSScriptAnalyzer clean. New head f0bc0f1.

🤖 Generated with Claude Code

@fadwen-ai fadwen-ai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 2: all three round 1 findings are fixed in f0bc0f1, and I've resolved their threads. Both helpers now end with ParameterNotFound for a parameter the command doesn't have, and their help says so. The input-length test checks ContainsKey('Path') before it asks. The two blank lines are in.

I re-read the whole diff against main. Every caller of the two helpers names a parameter that exists, so the new error can't trip a test that was passing. Under Set-StrictMode -Version Latest the helpers return True for Get-Item -Path and raise ParameterNotFound,Test-ParameterMandatory for an unknown name. The ten files that use them pass on PowerShell 7.6 (183 passed, 0 failed). No new findings.

Out of scope: none

🤖 Generated with Claude Code

@fadwen
fadwen merged commit 2e935d0 into main Nov 26, 2026
5 checks passed
@fadwen
fadwen deleted the test/strict-mode-suites branch November 26, 2026 01:22
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