Skip to content

Commit 329ca92

Browse files
tablackburnclaude
andauthored
fix(tests): parse changelog version with Select-String instead of break in ForEach-Object (#37)
* fix(tests): parse changelog version with Select-String instead of break in ForEach-Object The BeforeAll changelog-version parse in tests/Manifest.tests.ps1 used `Get-Content $changelogPath | ForEach-Object { ... break }`. `break` inside ForEach-Object is unreliable: a pipeline is not a loop, so `break` targets an enclosing loop if one exists, or otherwise terminates the block unexpectedly, rather than cleanly stopping at the first match. Replace it with Select-String, which returns the first matching line's named capture group directly — no loop and no break. Behavior is unchanged (still resolves to the topmost `## [Version]` entry). Surfaced while aligning a downstream module's Manifest test (tablackburn/ReScenePS#22). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(tests): guard changelog version parse against no match Capture the Select-String MatchInfo before indexing so a missing or malformed changelog heading leaves $changelogVersion as $null and fails the assertion cleanly, instead of throwing "Cannot index into a null array" in BeforeAll. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 01cca97 commit 329ca92

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

tests/Manifest.tests.ps1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,15 @@ BeforeAll {
140140
# Parse the version from the changelog
141141
$changelogPath = Join-Path -Path $Env:BHProjectPath -ChildPath 'CHANGELOG.md'
142142
$changelogVersionPattern = '^##\s\\?\[(?<Version>(\d+\.){1,3}\d+)\\?\]' # Matches on a line that starts with '## [Version]' or '## \[Version\]'
143-
$changelogVersion = Get-Content $changelogPath | ForEach-Object {
144-
if ($_ -match $changelogVersionPattern) {
145-
$changelogVersion = $matches.Version
146-
break
147-
}
143+
# Select-String returns the first matching line's named capture directly — no loop and no
144+
# 'break' (which is unreliable inside ForEach-Object, since a pipeline is not a loop).
145+
# Capture the MatchInfo first and guard for no match, so a missing or malformed changelog
146+
# heading leaves $changelogVersion as $null (failing the assertion below cleanly) instead of
147+
# throwing "Cannot index into a null array" here in BeforeAll.
148+
$changelogMatch = Select-String -Path $changelogPath -Pattern $changelogVersionPattern |
149+
Select-Object -First 1
150+
$changelogVersion = if ($changelogMatch) {
151+
$changelogMatch.Matches[0].Groups['Version'].Value
148152
}
149153
}
150154
Describe 'Module manifest' {

0 commit comments

Comments
 (0)