test: Fix the certificate mock scoping and cover the signing surface - #228
Open
tablackburn wants to merge 3 commits into
Open
test: Fix the certificate mock scoping and cover the signing surface#228tablackburn wants to merge 3 commits into
tablackburn wants to merge 3 commits into
Conversation
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
Test Results 4 files ± 0 901 suites +12 2m 28s ⏱️ -1s Results for commit 1a597ec. ± Comparison against base commit 3ea9e82. This pull request removes 2 and adds 23 tests. Note that renamed tests count towards both. |
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 from inside themodule. Three of the tests they backed asserted that the real certificate store was empty: green
on CI and on any workstation without a code-signing certificate, red on a maintainer's machine.
Reproduced first:
Should -Invoke Get-ChildItem -Times 0 -Exactlypassed while a module-scopedspy recorded the one real call, and a certificate stood into the store turned the test red.
Invoke-PSBuildModuleSigninghad eleven passing tests and none of them called it. Its twofile-discovery tests called
Get-ChildItemin the test body and counted the results, whichasserts a fact about PowerShell rather than 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, a certificate with no private
key, and a certificate whose thumbprint is not the one asked for, so each exercises the filter it
is named after; two tests cover what none of them did, a store that does hold a usable
certificate.
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 generated 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 off Windows.
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 two overloads, same exceptions left to propagate, and the password passed
only when the environment variable held one, since no password and an empty one are not the same
thing to the PFX loader. No behaviour change for consumers.
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; it passes an empty timestampserver so the suite stays off the network. Platform guards use
-Skip:($null -ne $IsWindows -and -not $IsWindows)from #197, and its mirror for the onenon-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 in
Get-PSBuildCertificateare the non-Windowsthrow, covered on the Linux and macOS legs../build.ps1 -Task Testis green on PowerShell 7.6.5 / Windows: 611 passed, 0 failed, 4 skipped(the fourth is the new non-Windows test), up from 591, and 27.6% to 31.4% overall. No new
PSScriptAnalyzer findings.
Cert:\CurrentUser\Mywas checked after every run and holds nothingthe tests created. The real-signing path was run under Windows PowerShell 5.1 directly and behaves
the same; Pester 6 is not installed for 5.1 here, so that leg is left to CI.
No
CHANGELOG.mdentry: this is the test suite, and the module change is an internal seam with nouser-facing behaviour change.