Skip to content

test: Fix the certificate mock scoping and cover the signing surface - #228

Open
tablackburn wants to merge 4 commits into
mainfrom
test/216-217-signing-coverage
Open

test: Fix the certificate mock scoping and cover the signing surface#228
tablackburn wants to merge 4 commits into
mainfrom
test/216-217-signing-coverage

Conversation

@tablackburn

@tablackburn tablackburn commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Closes #216. Closes #217.

What was wrong

Six Mock Get-ChildItem statements in tests/Get-PSBuildCertificate.tests.ps1 had no
-ModuleName, so they never reached the call Get-PSBuildCertificate makes inside the module.
Three of the tests they backed asserted the real certificate store was empty: green on CI and on
any workstation without a code-signing certificate, red on a maintainer's. Reproduced first —
Should -Invoke Get-ChildItem -Times 0 -Exactly passed, and a certificate stood into the store
turned the test red.

Invoke-PSBuildModuleSigning had eleven passing tests and none called it; its two file-discovery
tests counted Get-ChildItem results in the test body, asserting a fact about PowerShell rather
than about this module.

What changed

Certificate tests. The six mocks are module-scoped, following the SkipValidation context
from #194. The three store tests now return an expired certificate, one with no private key, and
one whose thumbprint is not the one asked for, so each exercises the filter it is named after; two
more cover a store that holds a usable certificate. Three guard clauses that had never run are
covered too: the empty thumbprint, the unset variable, and the Store source's refusal off Windows.

The post-load validation block — private key, expiry, Code Signing extended key usage — runs for
the first time, driven by objects carrying the properties the function reads. Generated
certificates cannot do this job: EnhancedKeyUsageList comes from PowerShell's own type data, and
one and the same certificate reports the usage on Windows PowerShell 5.1 and an empty list on
PowerShell 7.

One module change. The EnvVar and PfxFile sources constructed an X509Certificate2
directly, and a constructor is not a command, so nothing could stand in for it — which is why the
validation after it had never executed. Private/Import-PSBuildX509Certificate.ps1 names that
load as a command: same overloads, same exceptions propagating, no validation, no store writes.
Three tests drive it with a real PFX — password-less with the password variable unset, protected
with the password supplied, and protected with the wrong password, which proves the password is
used at all.

An earlier revision guarded the password so a $null from an unset variable would not become
"". That distinction does not exist, so the guard and its comment are gone. Measured on both
editions against a password-less PFX, a protected one, and one exported with an explicitly empty
password, each loaded with a genuine null ([NullString]::Value), with "", and via the
single-argument overload: null and empty behave identically in every cell, and only the
single-argument overload differs at all. The guard could not have acted on the distinction anyway
— an unbound [string] parameter is "", not $null, and [NullString]::Value collapses to
"" when bound to one.

Signing tests. The two misnamed tests are replaced by tests that call the function with
Set-AuthenticodeSignature mocked inside the module: the search recurses, the Include patterns
filter, the certificate, timestamp server, and hash algorithm reach the signing cmdlet rather than
being accepted and dropped, the defaults are the documented ones, and a signature comes back per
file. One Windows-only test signs for real against a self-signed certificate created for the run
and removed from the store in AfterAll however the tests end; an empty timestamp server keeps it
off the network. Platform guards use the #197 shape, and its mirror for the non-Windows test.

Coverage and verification

File Before After
Get-PSBuildCertificate.ps1 65 of 95 93 of 95
Invoke-PSBuildModuleSigning.ps1 5 of 17 17 of 17
Import-PSBuildX509Certificate.ps1 3 of 3

The five instructions covered before in Invoke-PSBuildModuleSigning were its ValidateScript,
which runs at parameter binding; its body was at zero. The two still missed are the non-Windows
throw, covered on the Linux and macOS legs.

./build.ps1 -Task Test is green on PowerShell 7.6.5 / Windows: 614 passed, 0 failed, 4 skipped
(the fourth is the new non-Windows test), up from 591, and 27.6% to 31.2% overall. No new
PSScriptAnalyzer findings, Cert:\CurrentUser\My holds nothing the tests created, rebased on
main. No CHANGELOG.md entry: this is the test suite, and the module change is an internal seam
with no user-facing behaviour change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MxiVZmXp31BREi3aNDop5y

Copilot AI lite review requested due to automatic review settings August 28, 2026 22:15

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Aug 28, 2026

Copy link
Copy Markdown

Test Results

    4 files  ± 0    901 suites  +12   2m 30s ⏱️ +3s
  620 tests +24    617 ✅ +24   3 💤 ± 0  0 ❌ ±0 
2 463 runs  +96  2 369 ✅ +74  94 💤 +22  0 ❌ ±0 

Results for commit 8e55873. ± Comparison against base commit 3ea9e82.

This pull request removes 2 and adds 26 tests. Note that renamed tests count towards both.
Code Signing Functions.Invoke-PSBuildModuleSigning.Searches for files matching Include patterns
Code Signing Functions.Invoke-PSBuildModuleSigning.Uses custom Include patterns when specified
Code Signing Functions.Get-PSBuildCertificate.EnvVar mode.Loads a certificate from a PFX that carries no password
Code Signing Functions.Get-PSBuildCertificate.EnvVar mode.Loads a password-protected PFX with the password from the environment
Code Signing Functions.Get-PSBuildCertificate.EnvVar mode.Throws when the environment variable holds nothing
Code Signing Functions.Get-PSBuildCertificate.EnvVar mode.Throws when the password from the environment is the wrong one
Code Signing Functions.Get-PSBuildCertificate.Store mode.Returns a valid certificate that the store does hold
Code Signing Functions.Get-PSBuildCertificate.Store mode.Throws where there is no certificate store to search
Code Signing Functions.Get-PSBuildCertificate.Thumbprint mode.Returns the certificate whose thumbprint was asked for
Code Signing Functions.Get-PSBuildCertificate.Thumbprint mode.Throws when the thumbprint is empty
Code Signing Functions.Get-PSBuildCertificate.Validation of a certificate loaded from EnvVar or PfxFile.Applies the same validation to a certificate loaded from a PFX file
Code Signing Functions.Get-PSBuildCertificate.Validation of a certificate loaded from EnvVar or PfxFile.Hands the decoded payload and the password from the environment to the loader
…

♻️ This comment has been updated with latest results.

tablackburn and others added 4 commits September 3, 2026 13:57
The EnvVar and PfxFile sources construct an X509Certificate2 directly. A
constructor is a .NET call rather than a command, so nothing could stand in
for it, and the validation Get-PSBuildCertificate applies afterwards -- private
key, expiry, and the Code Signing extended key usage -- could only ever run
against a certificate the machine happened to have. It never ran at all.

Import-PSBuildX509Certificate names that load as a command and changes nothing
else: the same two constructor overloads, the same exceptions left to
propagate, and the password parameter supplied only when the environment
variable held something, because no password and an empty one are not the same
thing to the PFX loader.

Refs #216

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011GYJrhbrDzqufaeMqD9QjT
Six Mock Get-ChildItem statements were declared without -ModuleName, so they
never reached the call Get-PSBuildCertificate makes from inside the module.
Three of the tests they backed asserted the real certificate store was empty,
which is true on CI and on any workstation without a code-signing certificate,
and false on a maintainer's machine -- the one place signing is worked on.

The mocks are now module-scoped, and the three store tests return an expired
certificate, a certificate without a private key, and a certificate whose
thumbprint was not the one asked for, so each one exercises the filter it is
named after. Two tests are added for the case none of them covered: a store
that does hold a usable certificate.

The post-load validation block is covered for the first time, through objects
carrying the five properties the function reads. Generated certificates cannot
do this job: EnhancedKeyUsageList comes from PowerShell's own type data, and
one and the same generated code-signing certificate reports the Code Signing
usage on Windows PowerShell 5.1 and an empty list on PowerShell 7.

Three guard clauses that had never run are covered too: the empty thumbprint,
the unset environment variable, and the Store source's refusal to run where
there is no certificate store, which runs on the Linux and macOS legs.

Closes #216

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011GYJrhbrDzqufaeMqD9QjT
…ldItem

Eleven tests passed and none of them called the function. The two named for
file discovery called Get-ChildItem in the test body and counted the results,
which asserts a fact about PowerShell rather than about this module, and one of
them declared a Mock Set-AuthenticodeSignature it never used and could not have
reached, because it too was missing -ModuleName.

Both are replaced by tests that call the function with Set-AuthenticodeSignature
mocked inside the module. They cover what nothing covered: that the search
recurses, that the Include patterns filter, that the certificate, timestamp
server, and hash algorithm reach the signing cmdlet rather than being accepted
and dropped, that the defaults are the documented ones, and that a signature
comes back for every file.

One Windows-only test signs for real, against a self-signed certificate created
for the run and removed from the store in AfterAll however the tests end. It
passes an empty timestamp server so the suite stays off the network.

Closes #217

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011GYJrhbrDzqufaeMqD9QjT
…ot exist

The guard omitted the Password parameter when the password environment variable
was unset, on the stated premise that no password and an empty one are not the
same thing to the PFX loader. The premise is false, and the guard could not have
acted on it anyway: an unbound [string] parameter is an empty string, not null,
so the function had no way to express "no password" in the first place.

Measured on Windows PowerShell 5.1 and PowerShell 7, loading a password-less
PFX, a password-protected one, and one exported with an explicitly empty
password, each with a genuine null (NullString::Value), with an empty string,
and through the single-argument overload. Null and empty behave identically in
every cell: both load the two loadable payloads and both fail the protected one
with the same message. Only the single-argument overload differs, and only in
the text of the failure it produces on a protected PFX.

Three tests cover the load end to end with the real constructor rather than a
stand-in: a password-less PFX with the password variable unset, a protected one
with the password supplied, and a protected one with the wrong password, which
is what proves the password is used at all. They pass -SkipValidation because
EnhancedKeyUsageList is edition-dependent; the validation itself is covered by
the fake certificates.

Refs #216

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MxiVZmXp31BREi3aNDop5y
@tablackburn
tablackburn force-pushed the test/216-217-signing-coverage branch from 1a597ec to 8e55873 Compare September 3, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants