LanguagePrimitives: fix second enum value in wildcard duplicate-match error#27104
LanguagePrimitives: fix second enum value in wildcard duplicate-match error#27104cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
Conversation
… error EnumSingleTypeConverter used values.GetValue(i) when two enumerator names matched a wildcard; the outer index i is wrong for the second match (use j). Add a regression test that invokes EnumSingleTypeConverter.ConvertFrom so the wildcard path is exercised (ConvertTo(string, enum) may resolve via Enum.Parse first).
There was a problem hiding this comment.
Pull request overview
Fixes an enum wildcard duplicate-match error so the “two matches found” cast exception reports the correct second enum value, and adds a regression test to exercise the wildcard matching path in LanguagePrimitives enum conversion.
Changes:
- Fix
EnumSingleTypeConverter.BaseConvertFromto use the correct enum member index when building the second conflicting match value. - Add a Pester regression test that invokes
EnumSingleTypeConverter.ConvertFromvia reflection to hit the wildcard branch and validate distinct matches are reported.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/System.Management.Automation/engine/LanguagePrimitives.cs |
Corrects the second conflicting enum value used in the duplicate-match error path. |
test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 |
Adds a regression test for wildcard enum-name matching that previously repeated the first match. |
| $ex | Should -BeOfType [System.Management.Automation.PSInvalidCastException] | ||
| $ex.Message | Should -Match '\(Alpha, Beta\)' | ||
| $ex.Message | Should -Not -Match '\(Alpha, Alpha\)' |
There was a problem hiding this comment.
The assertions on $ex.Message are tied to the exact punctuation/formatting of the localized resource string (e.g., parentheses and comma+space). This can make the test brittle if wording/punctuation changes or if the test runs under a different UI culture. Prefer asserting on the presence/uniqueness of the enum value tokens themselves (e.g., that Alpha and Beta each appear, and Alpha appears only once) rather than matching the full (Alpha, Beta) substring.
| $asm = [System.Management.Automation.LanguagePrimitives].Assembly | ||
| $converterType = $asm.GetType('System.Management.Automation.LanguagePrimitives+EnumSingleTypeConverter') | ||
| $inst = [Activator]::CreateInstance($converterType, $true) |
There was a problem hiding this comment.
$converterType = $asm.GetType(...) can return $null if the nested type name changes, which would make the test fail with a less-informative exception when creating the instance or calling ConvertFrom. Consider adding explicit assertions that $converterType (and $inst) are not $null so failures are clearer.
Summary
Ensures the
InvalidCastEnumTwoStringsFoundAndNoFlagserror lists the correct second enumerator when a wildcard string matches two names on a non-flags enumeration (the message previously repeated the first match).Changes
EnumSingleTypeConverter.BaseConvertFrom, usevalues.GetValue(j)for the second conflicting match instead ofvalues.GetValue(i).EnumSingleTypeConverter.ConvertFrom(via reflection) so the wildcard branch is exercised.Testing
Invoke-Pesterontest/powershell/engine/Api/LanguagePrimitive.Tests.ps1(newItblock).