From 9665ea5af6a30c8daf50cbba05580ea9803b52ec Mon Sep 17 00:00:00 2001 From: Matthias Wolf <78562192+mawosoft@users.noreply.github.com> Date: Tue, 21 Nov 2023 19:56:24 +0100 Subject: [PATCH 1/4] Fix regression in #20608. Fixes regression from #20608 which mixed up use of `StringBuilder.AppendFormat` and interpolated strings, thereby passing the actual value to be formatted as the format string. Fixes #20711. --- .../commands/utility/Group-Object.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs index 0f0a9951967..1f527258939 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs @@ -153,7 +153,7 @@ private static string BuildName(List propValues) foreach (object item in propertyValueItems) { - sb.AppendFormat(CultureInfo.CurrentCulture, $"{item}, "); + sb.Append(CultureInfo.CurrentCulture, $"{item}, "); } sb = sb.Length > length ? sb.Remove(sb.Length - 2, 2) : sb; @@ -161,7 +161,7 @@ private static string BuildName(List propValues) } else { - sb.AppendFormat(CultureInfo.CurrentCulture, $"{propValuePropertyValue}, "); + sb.Append(CultureInfo.CurrentCulture, $"{propValuePropertyValue}, "); } } } From 6ee5a25c11851ae14849f62b8a5c107c2c563342 Mon Sep 17 00:00:00 2001 From: Matthias Wolf <78562192+mawosoft@users.noreply.github.com> Date: Wed, 22 Nov 2023 10:03:33 +0100 Subject: [PATCH 2/4] Test added. --- .../Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 index b1787bddad2..ee71f07df2e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -130,6 +130,10 @@ Describe "Group-Object" -Tags "CI" { $result[0].Name | Should -Be "" $result[0].Group | Should -Be '@{X=}' } + + It "Should not throw with format-like strings containing curly braces (issue #20711)" { + { '{', '}', '{0}' | Group-Object } | Should -Not -Throw + } } Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort-Object, Compare-Object)" -Tags "CI" { From 6ce2d247979c69696e7db9c4caf48927d9671ade Mon Sep 17 00:00:00 2001 From: Matthias Wolf <78562192+mawosoft@users.noreply.github.com> Date: Wed, 22 Nov 2023 13:07:48 +0100 Subject: [PATCH 3/4] Apply feedback from code review. --- .../Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 index ee71f07df2e..f3d39d8046c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -131,8 +131,12 @@ Describe "Group-Object" -Tags "CI" { $result[0].Group | Should -Be '@{X=}' } - It "Should not throw with format-like strings containing curly braces (issue #20711)" { - { '{', '}', '{0}' | Group-Object } | Should -Not -Throw + It "Should handle format-like strings with curly braces like normal strings" { + $result = '{', '}', '{0}' | Group-Object + $result.Count | Should -Be 3 + $result[0].Name | Should -Be '{' + $result[1].Name | Should -Be '{0}' + $result[2].Name | Should -Be '}' } } From be5727d7a5bafb6aa41040999d5c7a54a3044725 Mon Sep 17 00:00:00 2001 From: Matthias Wolf <78562192+mawosoft@users.noreply.github.com> Date: Wed, 22 Nov 2023 17:58:24 +0100 Subject: [PATCH 4/4] Apply suggestion from code review Co-authored-by: Ilya --- .../Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 index f3d39d8046c..5811db60cfc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -134,9 +134,9 @@ Describe "Group-Object" -Tags "CI" { It "Should handle format-like strings with curly braces like normal strings" { $result = '{', '}', '{0}' | Group-Object $result.Count | Should -Be 3 - $result[0].Name | Should -Be '{' - $result[1].Name | Should -Be '{0}' - $result[2].Name | Should -Be '}' + $result[0].Name | Should -BeExactly '{' + $result[1].Name | Should -BeExactly '{0}' + $result[2].Name | Should -BeExactly '}' } }