From 75627e31a48fc279b1beec40d7f274bb065f1380 Mon Sep 17 00:00:00 2001 From: Jeffrey Stuhr <110697945+fadwen@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:37:25 -0800 Subject: [PATCH 1/2] test: decide the skip conditions of four tests during discovery The three tests of the SYSTEM-context integration suite and the file-version test of Test-IntuneWin32Rule were skipped on every run, in an elevated session and on a computer that has notepad.exe as well. Their -Skip condition read a script-scoped variable that the file set in BeforeAll. Pester evaluates -Skip during discovery, before any BeforeAll runs, so the variable was unset, the condition was true, and a skipped test still leaves the run Passed. Both variables are now set in BeforeDiscovery, as the other suites with a skip condition do. The SYSTEM-context tests are the only ones that launch a script through a scheduled task as NT AUTHORITY\SYSTEM, so that path is tested again wherever the session is elevated. Refs #83 --- CHANGELOG.md | 5 +++++ Tests/Integration/SystemContext.Tests.ps1 | 11 ++++++++--- Tests/Unit/Public/Test-IntuneWin32Rule.Tests.ps1 | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7261a20..65bb2a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,11 @@ entries are carried over from the manifest's release notes. - A test of `Get-IslAgentLogEvent` read the event table before anything had built it, so it failed whenever the shuffled order of the CI run put it first in its file. It builds the table first. +- Four tests were skipped on every run: the three of the SYSTEM-context integration suite, which + are the only ones that launch a script through a scheduled task as SYSTEM, and the file-version + test of `Test-IntuneWin32Rule`. Their skip condition read a variable that was set only after + Pester had evaluated the condition. It is decided during discovery now, so the SYSTEM-context + suite runs in an elevated session and is skipped in any other. ## [0.34.0] - 2026-10-15 diff --git a/Tests/Integration/SystemContext.Tests.ps1 b/Tests/Integration/SystemContext.Tests.ps1 index 3e95942..4e2ad71 100644 --- a/Tests/Integration/SystemContext.Tests.ps1 +++ b/Tests/Integration/SystemContext.Tests.ps1 @@ -8,13 +8,18 @@ #pester:no-parallel +BeforeDiscovery { + # Pester evaluates -Skip during discovery, before any BeforeAll runs. Set there, this was still + # unset when the conditions read it, and the tests were skipped in an elevated session too + $identity = [Security.Principal.WindowsIdentity]::GetCurrent() + $script:IsAdmin = ([Security.Principal.WindowsPrincipal]$identity).IsInRole( + [Security.Principal.WindowsBuiltInRole]::Administrator) +} + BeforeAll { $script:ModuleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') -Force . (Join-Path $script:ModuleRoot 'Tests\TestHelpers\TestHelpers.ps1') - $identity = [Security.Principal.WindowsIdentity]::GetCurrent() - $script:IsAdmin = ([Security.Principal.WindowsPrincipal]$identity).IsInRole( - [Security.Principal.WindowsBuiltInRole]::Administrator) } AfterAll { diff --git a/Tests/Unit/Public/Test-IntuneWin32Rule.Tests.ps1 b/Tests/Unit/Public/Test-IntuneWin32Rule.Tests.ps1 index 0a4db38..5766165 100644 --- a/Tests/Unit/Public/Test-IntuneWin32Rule.Tests.ps1 +++ b/Tests/Unit/Public/Test-IntuneWin32Rule.Tests.ps1 @@ -7,6 +7,13 @@ afterwards; the 32-bit registry view is read from a Windows value that differs between views. #> +BeforeDiscovery { + # Pester evaluates -Skip during discovery, before any BeforeAll runs, so the condition of the + # file-version test is decided here. BeforeAll looks again, because a variable set during + # discovery does not reach the run + $script:HasVersioned = Test-Path -LiteralPath (Join-Path $env:WINDIR 'System32\notepad.exe') +} + BeforeAll { $script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') -Force From 693aa7ee1353336317585c55e06eea3824c87182 Mon Sep 17 00:00:00 2001 From: Jeffrey Stuhr <110697945+fadwen@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:59:31 -0800 Subject: [PATCH 2/2] ci: say that the SYSTEM-context tests run on the hosted runners Two comments in quality-gates.yml said that the hosted runner's token is not elevated and that the SYSTEM-context tests therefore skip in CI. They never ran because their skip condition was evaluated before its variable was set, not because of the runner: with the condition decided during discovery, the tests job and the Windows PowerShell 5.1 job each run the three tests, and the file takes 14 seconds where it took 62 milliseconds. The comments now say that the runner is elevated and that those jobs register and remove IntuneScriptLab-* scheduled tasks as SYSTEM on it. Refs #83 --- .github/workflows/quality-gates.yml | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/quality-gates.yml b/.github/workflows/quality-gates.yml index 41ffb01..717a811 100644 --- a/.github/workflows/quality-gates.yml +++ b/.github/workflows/quality-gates.yml @@ -5,10 +5,12 @@ # tests Windows, PowerShell 7. PSScriptAnalyzer at Error and Warning over the module, the # kit and the tests, the 115-character line limit, then the whole Pester suite (the # module's unit and integration suites and the validation kit's unit suite) with an -# 80% coverage gate over Public/ and Private/. Nothing here -# reaches a tenant: every Graph call in the suites goes through the mocked -# Invoke-IslGraphRequest seam, and the suites that need an elevated session, a lab -# account or a Graph connection skip themselves. +# 80% coverage gate over Public/ and Private/. Nothing here reaches a tenant: every +# Graph call in the suites goes through the mocked Invoke-IslGraphRequest seam, and +# the suites that need a lab account or a Graph connection skip themselves. The +# hosted Windows runner is elevated, so the SYSTEM-context tests run: they register +# scheduled tasks named IntuneScriptLab-* as NT AUTHORITY\SYSTEM on the runner, run +# a script through each and remove them. # # Every Pester run goes through Tests/Invoke-Tests.ps1, the script a developer runs as well. It # shuffles the order, so a test that only passes because of what ran before it fails here @@ -185,8 +187,9 @@ jobs: "Pester $((Get-Module Pester).Version) on PowerShell $($PSVersionTable.PSVersion)" | Write-Host # The integration suite as well: it launches the 5.1 hosts the harness uses on a device. - # The hosted runner's token is not elevated (IsInRole Administrator is false), so the - # SYSTEM-context tests skip here as they do under pwsh; they run on the lab devices + # The hosted runner is elevated, so the SYSTEM-context tests run here as they do under + # pwsh: they register and remove IntuneScriptLab-* scheduled tasks as SYSTEM on the runner. + # The user-context suite needs a lab account and skips itself. - name: Pester under Windows PowerShell 5.1 shell: powershell run: |