test: Fix the certificate mock scoping and cover the signing surface - #228
Open
tablackburn wants to merge 4 commits into
Open
test: Fix the certificate mock scoping and cover the signing surface#228tablackburn wants to merge 4 commits into
tablackburn wants to merge 4 commits into
Conversation
Test Results 4 files ± 0 901 suites +12 2m 30s ⏱️ +3s 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.♻️ This comment has been updated with latest results. |
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
force-pushed
the
test/216-217-signing-coverage
branch
from
September 3, 2026 17:59
1a597ec to
8e55873
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #216. Closes #217.
What was wrong
Six
Mock Get-ChildItemstatements intests/Get-PSBuildCertificate.tests.ps1had no-ModuleName, so they never reached the callGet-PSBuildCertificatemakes 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 -Exactlypassed, and a certificate stood into the storeturned the test red.
Invoke-PSBuildModuleSigninghad eleven passing tests and none called it; its two file-discoverytests counted
Get-ChildItemresults in the test body, asserting a fact about PowerShell ratherthan about this module.
What changed
Certificate tests. The six mocks are module-scoped, following the
SkipValidationcontextfrom #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:
EnhancedKeyUsageListcomes from PowerShell's own type data, andone and the same certificate reports the usage on Windows PowerShell 5.1 and an empty list on
PowerShell 7.
One module change. The
EnvVarandPfxFilesources constructed anX509Certificate2directly, 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.ps1names thatload 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
$nullfrom an unset variable would not become"". That distinction does not exist, so the guard and its comment are gone. Measured on botheditions 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 thesingle-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]::Valuecollapses to""when bound to one.Signing tests. The two misnamed tests are replaced by tests that call the function with
Set-AuthenticodeSignaturemocked inside the module: the search recurses, theIncludepatternsfilter, 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
AfterAllhowever the tests end; an empty timestamp server keeps itoff the network. Platform guards use the #197 shape, and its mirror for the non-Windows test.
Coverage and verification
Get-PSBuildCertificate.ps1Invoke-PSBuildModuleSigning.ps1Import-PSBuildX509Certificate.ps1The five instructions covered before in
Invoke-PSBuildModuleSigningwere itsValidateScript,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 Testis 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\Myholds nothing the tests created, rebased onmain. NoCHANGELOG.mdentry: this is the test suite, and the module change is an internal seamwith no user-facing behaviour change.
🤖 Generated with Claude Code
https://claude.ai/code/session_01MxiVZmXp31BREi3aNDop5y