Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/quality-gates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
# arm64 Windows on ARM. The runtime harness picks a PowerShell host per architecture, and
# on an ARM64 device the answers differ from x64: the System32 host is native ARM64,
# the SysWOW64 host is emulated x86, and there is no x64 host at all. The x64 runner
# cannot exercise those branches, so the unit suites run again on GitHub's ARM64 image.
# cannot exercise those branches, so the unit suites run again on GitHub's ARM64 image,
# with the tests of the unit folders that start processes in a second run.
#
# help Ubuntu. The command help gate: the PlatyPS Markdown under docs/ is valid and
# complete, the committed MAML in en-US/ was built from it, the generated rule
Expand Down Expand Up @@ -222,6 +223,9 @@ jobs:
run: |
$ErrorActionPreference = 'Stop'
./Tests/Invoke-Tests.ps1 -TestType Unit -OutputPath ./reports -SummaryPath $env:GITHUB_STEP_SUMMARY
# The tests of the unit folders that start processes: the arm64 host and the x86 one that
# Windows on ARM emulates are what they cover here
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Integration -OutputPath ./reports/process -SummaryPath $env:GITHUB_STEP_SUMMARY
Comment thread
fadwen-ai marked this conversation as resolved.
Resolved

- name: Upload test results
if: always()
Expand Down
30 changes: 21 additions & 9 deletions Build/Invoke-Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
3. Build/Test-MarkdownLink.ps1: the anchors and relative links of the Markdown.
4. Build/Invoke-Analysis.ps1: PSScriptAnalyzer with the repository's settings, in a new
process.
5. Tests/Invoke-Tests.ps1 with -TestType in this host.
6. Tests/Invoke-Tests.ps1 -TestType Unit under Windows PowerShell 5.1, on Windows, unless
-SkipWindowsPowerShell.
5. Tests/Invoke-Tests.ps1 with -TestType in this host. With Unit, the default, the tests of the
unit folders tagged Integration, which start processes, run after the unit tests.
6. The same under Windows PowerShell 5.1, on Windows, unless -SkipWindowsPowerShell.

Steps 1 and 2 rewrite en-US/IntuneScriptLab-Help.xml and docs/Rules.md. A change to either
belongs in the same commit as its cause. The script ends with the list of the two files whose
Expand Down Expand Up @@ -172,16 +172,28 @@ if ($LASTEXITCODE -ne 0) {
}

if (-not $SkipTests) {
Write-BuildStep -Text "Tests ($TestType) in PowerShell $($PSVersionTable.PSVersion)"
& $runner -TestType $TestType
# The unit run leaves out the tests of the unit folders that start processes (TST-47); the
# build runs them after it, so it checks what it checked before they were tagged
$runs = @(@{ TestType = $TestType })
if ($TestType -eq 'Unit') {
$runs += @{ TestType = 'Unit'; Tag = 'Integration' }
}
foreach ($run in $runs) {
Write-BuildStep -Text "Tests ($($run.TestType) $($run['Tag'])) in PowerShell $($PSVersionTable.PSVersion)"
& $runner @run
}

$onWindows = $PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows
$inDesktop = $PSVersionTable.PSEdition -eq 'Desktop'
if ($onWindows -and -not $inDesktop -and -not $SkipWindowsPowerShell) {
Write-BuildStep -Text 'Unit tests under Windows PowerShell 5.1'
& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $runner -TestType Unit
if ($LASTEXITCODE -ne 0) {
throw "The unit tests failed under Windows PowerShell 5.1 (exit code $LASTEXITCODE)."
$desktopRuns = @(, @('-TestType', 'Unit')) + @(, @('-TestType', 'Unit', '-Tag', 'Integration'))
foreach ($desktopArguments in $desktopRuns) {
$described = @($desktopArguments | Select-Object -Skip 1) -join ' '
Write-BuildStep -Text "Tests ($described) under Windows PowerShell 5.1"
& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $runner @desktopArguments
if ($LASTEXITCODE -ne 0) {
throw "The tests failed under Windows PowerShell 5.1 (exit code $LASTEXITCODE)."
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ entries are carried over from the manifest's release notes.

### Changed

- A test that starts a process, reads the real machine or runs a script as a caller would is
tagged `Integration`, and `Tests/Invoke-Tests.ps1 -TestType Unit` leaves it out: the unit run
takes 69 seconds instead of 157 on a Windows 11 laptop. `-TestType Integration` runs those tests
with the integration suites, and the build and the ARM job run them in a run of their own (#70).
- The test suites run under strict mode, as the module does: `Tests/Invoke-Tests.ps1` no longer
turns it off around the Pester run (ERR-03, #70).
- `SECURITY.md` classifies the data the module handles and lists the threats to it by STRIDE
Expand Down
14 changes: 12 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ on `mobileApps/{id}` is refused (derived-type property) - fetch the app without

A `Mock` defined in `BeforeEach` is not active in a Context's `BeforeAll`; define mocks in the
Describe's `BeforeAll`. `Should-Invoke` for a call made in `BeforeAll` needs `-Scope Context`.
A test that starts a process, reads the real machine (CIM, the registry of the host, a logon
session) or runs a script as a caller would, through `Invoke-CallerScript`, is tagged
`Integration`, wherever its file is. `-TestType Unit` leaves those out, so that the unit run stays
within seconds (TST-47); `-TestType Integration` runs them with `Tests/Integration`, and the build,
the 5.1 job and the ARM job run them too. A unit test mocks the clock rather than waiting on it.
Comment thread
fadwen-ai marked this conversation as resolved.
Resolved

`Tests/Invoke-Tests.ps1` shuffles every run, locally and in each CI job, and puts the seed in its
failure message; a test that only passes in declaration order fails there first, and
`-ShuffleSeed` runs that order again.
Expand Down Expand Up @@ -256,12 +262,13 @@ vault is searched, and the secret stays a `SecureString` until the publish call.
## Checks

```powershell
./Build/Invoke-Build.ps1 # all of it in order: help, rule reference, links, analysis, unit suites on 7 and 5.1
./Build/Invoke-Build.ps1 # all of it in order: help, rule reference, links, analysis, unit and process tests on 7 and 5.1
./Tests/Invoke-Tests.ps1 # every suite, shuffled; suites that need elevation, a lab account or PSScriptAnalyzer skip themselves
./Tests/Invoke-Tests.ps1 -CodeCoverage # the same with the 80% coverage gate, as the tests job runs it
./Tests/Invoke-Tests.ps1 -TestType Performance # the log readers on a generated log, not part of the default run
./Build/Test-PerformanceBaseline.ps1 -ResultPath <file> -Record # baselines from ISL_PERFORMANCE_RESULTS, see the tests job
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Security # where a -Credential password goes, and the checks on input
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Integration # the tests of the unit folders that start processes
./Build/Invoke-Analysis.ps1 # PSScriptAnalyzer with the settings file, as the tests job runs it
./Build/Build-Help.ps1 -SkipUpdate # rebuild MAML after editing docs/, the form CI compares
./Build/Build-RuleReference.ps1 # rebuild docs/Rules.md after changing a rule
Expand All @@ -281,10 +288,13 @@ module's `Should-*` aliases and reports every Pester assertion as an alias.

`Tests/Invoke-Tests.ps1` is the one way the suites are run: it fails when Pester's result is not
`Passed` or when no tests ran, which a check of `FailedCount` alone does not. Run the unit suites
under Windows PowerShell 5.1 as well before pushing:
and the tagged tests of the unit folders under Windows PowerShell 5.1 as well before pushing, or
`./Build/Invoke-Build.ps1`, which runs both in both hosts. The second run holds the 5.1 checks that
matter most: the child imports, the module loading under 5.1, the host comparisons:

```powershell
powershell -NoProfile -File ./Tests/Invoke-Tests.ps1 -TestType Unit
powershell -NoProfile -File ./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Integration
```

The script sets strict mode, and the suites and the validation kit run under it, as the module
Expand Down
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Everything specific to this module is in `CLAUDE.md`. The ones most often trippe
# a lab account or PSScriptAnalyzer skip themselves
./Tests/Invoke-Tests.ps1 -TestType Performance # the log readers on a generated log, with the limits CI holds
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Security # where a -Credential password goes, and the checks on input
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Integration # the tests of the unit folders that start processes
./Build/Invoke-Analysis.ps1 # PSScriptAnalyzer, as CI runs it
./Build/Build-Help.ps1 -SkipUpdate # after editing docs/
./Build/Build-RuleReference.ps1 # after changing a rule
Expand All @@ -58,10 +59,12 @@ file out that way. `Invoke-Analysis.ps1` checks `Validation/Probe.ps1`,
`Validation/Win32Install.ps1` and `Validation/Fixtures.ps1` with the default rules only: they keep
the layout they ran with in the experiments.

`Invoke-Build.ps1` also runs the unit suites under Windows PowerShell 5.1. To run them by hand:
`Invoke-Build.ps1` also runs the unit suites, and the tests of the unit folders that start
processes, under Windows PowerShell 5.1. To run them by hand:

```powershell
powershell -NoProfile -File ./Tests/Invoke-Tests.ps1 -TestType Unit
powershell -NoProfile -File ./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Integration
```

A failure message of `Invoke-Tests.ps1` carries the shuffle seed; `-ShuffleSeed <seed>` runs the
Expand Down
54 changes: 43 additions & 11 deletions Tests/Invoke-Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ script needs neither elevation nor a tenant. It runs on PowerShell 7 and on Wind
5.1.

.PARAMETER TestType
Which suites to run. Unit is Tests/Unit and Validation/Tests/Unit. Integration is
Tests/Integration, the suites that start real Windows PowerShell processes. All, the default,
is both. Performance is Tests/Performance, which measures the log readers on a generated log; it
is not part of All, and its files keep their declared order (#pester:no-shuffle).
Which tests to run. A test that starts a process, reads the real machine or runs a script as a
caller would is tagged Integration, wherever its file is; the rest is a unit test.

Unit is the tests of Tests/Unit and Validation/Tests/Unit without the Integration tag, the ones
that run in seconds (TST-47). Integration is every test tagged Integration: the suites of
Tests/Integration and the tagged tests of the unit folders; with -Tag, only the tests of
Tests/Integration that have one of those tags. All, the default, is both.
Performance is Tests/Performance, which measures the log readers on a generated log; it is not
part of All, and its files keep their declared order (#pester:no-shuffle).

.PARAMETER Tag
Tags of the tests to run, for example Security for the tests of credential handling and input
validation. Left out, every test of the suites runs.
validation. Left out, every test of the suites runs. With -TestType Unit, -Tag Integration runs
the tagged tests of the unit folders, which the unit run otherwise leaves out.

.PARAMETER ExcludeTag
Tags of tests to leave out, for example Elevated for the tests that run as SYSTEM. Those tests
Expand Down Expand Up @@ -67,6 +73,11 @@ Runs every suite in a new shuffled order.

Runs the unit suites in the order that seed gives, to repeat a failure.

.EXAMPLE
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Integration

Runs the tests of the unit folders that start processes, which the unit run leaves out.

.EXAMPLE
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Security

Expand Down Expand Up @@ -153,7 +164,26 @@ $suites = @{
(Join-Path -Path $PSScriptRoot -ChildPath 'Performance')
)
}
$testPaths = if ($TestType -eq 'All') { $suites['Unit'] + $suites['Integration'] } else { $suites[$TestType] }
# The Integration tag decides what is a unit test, not the folder: a test in Tests/Unit that starts
# a process belongs to the integration run
$includeTags = @($Tag | Where-Object { $_ })
$testPaths = switch ($TestType) {
Comment thread
fadwen-ai marked this conversation as resolved.
Resolved
'All' { $suites['Unit'] + $suites['Integration'] }
# Pester's tag filter takes a test with any one of the tags, so with tags of the caller's own
# the unit folders would bring their tests with those tags too: the run keeps to
# Tests/Integration then, as it did before the tag decided
'Integration' {
if ($includeTags.Count -eq 0) { $suites['Unit'] + $suites['Integration'] } else { $suites['Integration'] }
}
default { $suites[$TestType] }
}
$excludeTags = @($ExcludeTag | Where-Object { $_ })
if ($TestType -eq 'Unit' -and $includeTags -notcontains 'Integration') {
$excludeTags += 'Integration'
}
if ($TestType -eq 'Integration' -and $includeTags.Count -eq 0) {
Comment thread
fadwen-ai marked this conversation as resolved.
Resolved
$includeTags = @('Integration')
}

$configuration = New-PesterConfiguration
$configuration.Run.Path = $testPaths
Expand All @@ -163,11 +193,11 @@ $configuration.Run.ShuffleSeed = $ShuffleSeed
$configuration.Output.Verbosity = $Output
# Every test file uses the Pester 6 assertions, so the Pester 5 form can stay off
$configuration.Should.DisableV5 = $true
if ($Tag) {
$configuration.Filter.Tag = $Tag
if ($includeTags.Count -gt 0) {
$configuration.Filter.Tag = $includeTags
}
if ($ExcludeTag) {
$configuration.Filter.ExcludeTag = $ExcludeTag
if ($excludeTags.Count -gt 0) {
$configuration.Filter.ExcludeTag = $excludeTags
}

$reportFolder = $null
Expand Down Expand Up @@ -235,7 +265,9 @@ if ($SummaryPath) {
# Written before the checks below, so that a run which fails them still leaves its summary
$summaryFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($SummaryPath)
$architecture = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
$heading = "## Pester, $TestType tests, PowerShell $($PSVersionTable.PSVersion) on $architecture"
# The tags tell two runs of one type apart, such as the unit run and the run of its tagged tests
$tagged = if ($Tag) { " tagged $($Tag -join ', ')" } else { '' }
$heading = "## Pester, $TestType tests$tagged, PowerShell $($PSVersionTable.PSVersion) on $architecture"
$markdown = "$heading`n`n$summary`n`n"
[System.IO.File]::AppendAllText($summaryFile, $markdown, [System.Text.UTF8Encoding]::new($false))
}
Expand Down
69 changes: 68 additions & 1 deletion Tests/Unit/Build/Invoke-Tests.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
a filter left every test out is Passed, and its TotalCount still counts those tests, so the
runner counts the tests that passed or failed. The runner is started in a new process with a
filter that leaves out every unit test, this file's included.

And what each test type selects, on a repository of its own: a copy of the runner, a module that
is only a manifest, and one fixture file in Tests/Unit and one in Tests/Integration, with a
test of each kind. The Integration tag, not the folder, decides what the unit run leaves out.
#>

BeforeDiscovery {
Expand All @@ -18,9 +22,47 @@ BeforeAll {
$testsFolder = Join-Path -Path $repositoryRoot -ChildPath 'Tests'
$script:runner = Join-Path -Path $testsFolder -ChildPath 'Invoke-Tests.ps1'
$script:shell = (Get-Process -Id $PID).Path

# The fixture repository: the runner imports the manifest at its root and runs these suites
$script:FixtureRoot = Join-Path -Path $TestDrive -ChildPath 'repository'
$fixtureTests = Join-Path -Path $script:FixtureRoot -ChildPath 'Tests'
$fixtureValidation = Join-Path -Path $script:FixtureRoot -ChildPath 'Validation'
$fixtureValidation = Join-Path -Path $fixtureValidation -ChildPath 'Tests'
$folders = @(
(Join-Path -Path $fixtureTests -ChildPath 'Unit')
(Join-Path -Path $fixtureTests -ChildPath 'Integration')
(Join-Path -Path $fixtureValidation -ChildPath 'Unit')
)
foreach ($folder in $folders) {
$null = New-Item -ItemType Directory -Path $folder -Force
}
Copy-Item -LiteralPath $script:runner -Destination $fixtureTests
$utf8 = [System.Text.UTF8Encoding]::new($false)
$rootModule = Join-Path -Path $script:FixtureRoot -ChildPath 'IntuneScriptLab.psm1'
[System.IO.File]::WriteAllText($rootModule, '', $utf8)
$manifest = "@{ RootModule = 'IntuneScriptLab.psm1'; ModuleVersion = '1.0.0'; FunctionsToExport = @() }"
$manifestPath = Join-Path -Path $script:FixtureRoot -ChildPath 'IntuneScriptLab.psd1'
[System.IO.File]::WriteAllText($manifestPath, $manifest, $utf8)
$unitFixture = @(
"Describe 'Fixture unit' -Tag 'Unit' {"
" It 'unit' { }"
" It 'process' -Tag 'Integration' { }"
" It 'unit security' -Tag 'Security' { }"
'}'
) -join "`n"
$unitFile = Join-Path -Path $folders[0] -ChildPath 'Fixture.Tests.ps1'
[System.IO.File]::WriteAllText($unitFile, $unitFixture, $utf8)
$integrationFixture = @(
"Describe 'Fixture integration' -Tag 'Integration' {"
" It 'integration' { }"
" It 'integration security' -Tag 'Security' { }"
'}'
) -join "`n"
$integrationFile = Join-Path -Path $folders[1] -ChildPath 'Fixture.Tests.ps1'
[System.IO.File]::WriteAllText($integrationFile, $integrationFixture, $utf8)
}

Describe 'Invoke-Tests.ps1' -Tag 'Unit', 'Build' {
Describe 'Invoke-Tests.ps1' -Tag 'Integration', 'Build' {

Context 'Error Handling' {
It 'fails when a filter leaves every test out' -Skip:$script:InsideRunner {
Expand All @@ -41,4 +83,29 @@ Describe 'Invoke-Tests.ps1' -Tag 'Unit', 'Build' {
$exitCode | Should-Be 1
}
}

Context 'What each test type runs' {
It '-TestType <TestType> <Arguments> runs <Expected>' -Skip:$script:InsideRunner -ForEach @(
@{ TestType = 'Unit'; Arguments = ''; Expected = 'unit, unit security' }
@{ TestType = 'Unit'; Arguments = '-Tag Integration'; Expected = 'process' }
@{ TestType = 'Unit'; Arguments = '-Tag Security'; Expected = 'unit security' }
@{ TestType = 'Integration'; Arguments = ''; Expected = 'integration, integration security, process' }
@{ TestType = 'Integration'; Arguments = '-Tag Security'; Expected = 'integration security' }
@{
TestType = 'All'; Arguments = ''
Expected = 'integration, integration security, process, unit, unit security'
}
) {
$fixtureTests = Join-Path -Path $script:FixtureRoot -ChildPath 'Tests'
$fixtureRunner = Join-Path -Path $fixtureTests -ChildPath 'Invoke-Tests.ps1'
$run = "& '$fixtureRunner' -TestType $TestType $Arguments -Output None -PassThru 6>`$null"
$names = "($run).Tests | Where-Object { `$_.Result -eq 'Passed' } | ForEach-Object { `$_.Name }"
$command = "(@($names) | Sort-Object) -join ', '"
$arguments = @('-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', $command)
$output = & $script:shell @arguments

"$output" | Should-Be $Expected
$LASTEXITCODE | Should-Be 0
}
}
}
4 changes: 2 additions & 2 deletions Tests/Unit/Module.Contract.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Describe 'IntuneScriptLab module contract' -Tag 'Unit', 'Contract' {
Should-Throw -ExceptionMessage '*neverAssignedAnywhere*'
}

Context 'When a part of the module cannot be loaded' {
Context 'When a part of the module cannot be loaded' -Tag 'Integration' {
BeforeAll {
# Each copy is imported in a new process: a second module of this name imported here
# would stand beside the one under test for the rest of the run
Expand Down Expand Up @@ -317,7 +317,7 @@ Describe 'IntuneScriptLab module contract' -Tag 'Unit', 'Contract' {
}
}

It 'loads on Windows PowerShell 5.1 as well as PowerShell 7' {
It 'loads on Windows PowerShell 5.1 as well as PowerShell 7' -Tag 'Integration' {
# The rules reference PowerShell 7 AST types by name only; a hard type reference would
# break import on 5.1, which is where many admins will run this.
$manifest = Test-ModuleManifest -Path $script:ManifestPath
Expand Down
Loading