Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ protected static object BaseConvertFrom(object sourceValue, Type destinationType
if (!multipleValues && foundOne)
{
object firstValue = Enum.ToObject(destinationType, returnUInt64);
object secondValue = Enum.ToObject(destinationType, Convert.ToUInt64(values.GetValue(i), CultureInfo.CurrentCulture));
object secondValue = Enum.ToObject(destinationType, Convert.ToUInt64(values.GetValue(j), CultureInfo.CurrentCulture));
throw new PSInvalidCastException("InvalidCastEnumTwoStringsFoundAndNoFlags", null,
ExtendedTypeSystem.InvalidCastExceptionEnumerationMoreThanOneValue,
sourceValue, destinationType, firstValue, secondValue);
Expand Down
33 changes: 33 additions & 0 deletions test/powershell/engine/Api/LanguagePrimitive.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,37 @@ Describe "Language Primitive Tests" -Tags "CI" {
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
$convertedValue | Should -Be 1
}

It 'Wildcard enum string matching two members lists both distinct values in the cast error' {
# Regression: the second conflicting match used values.GetValue(i) (outer source index) instead of
# values.GetValue(j) (enum member index), so the message repeated the first member as the second.
# LanguagePrimitives.ConvertTo(string, enum) often resolves via Enum.Parse / EnumMinimumDisambiguation
# first; EnumSingleTypeConverter.ConvertFrom hits the wildcard branch that contains the fix.
$enumTypeName = 'TestLanguagePrimitivesEnumDup.TwoMemberNoFlags'
if (-not ($enumTypeName -as [type])) {
Add-Type -TypeDefinition @'
namespace TestLanguagePrimitivesEnumDup {
public enum TwoMemberNoFlags {
Alpha = 0,
Beta = 1
}
}
'@
}

$enumType = [TestLanguagePrimitivesEnumDup.TwoMemberNoFlags]
$asm = [System.Management.Automation.LanguagePrimitives].Assembly
$converterType = $asm.GetType('System.Management.Automation.LanguagePrimitives+EnumSingleTypeConverter')
$inst = [Activator]::CreateInstance($converterType, $true)
Comment on lines +245 to +247
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$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.

Copilot uses AI. Check for mistakes.
$ex = $null
try {
$null = $inst.ConvertFrom('*', $enumType, [cultureinfo]::InvariantCulture, $true)
} catch {
$ex = $_.Exception.GetBaseException()
}

$ex | Should -BeOfType [System.Management.Automation.PSInvalidCastException]
$ex.Message | Should -Match '\(Alpha, Beta\)'
$ex.Message | Should -Not -Match '\(Alpha, Alpha\)'
Comment on lines +255 to +257
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
}
}
Loading