Skip to content

Commit b71d983

Browse files
vexx32iSazonov
authored andcommitted
Format-Hex - Fix Grouping Behaviour with Boolean values (#11587)
* Resolve issue with grouping bools & ints We were not flushing the input buffer immediately when a different type is encountered. This caused some odd behaviour when piping in a mix of bools and ints. Fix is to immediately flush the input buffer when the incoming object is a different type than anything we have buffered.
1 parent caa5051 commit b71d983

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

  • src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex
  • test/powershell/Modules/Microsoft.PowerShell.Utility

src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ private byte[] ConvertToBytes(object inputObject)
403403
if (_lastInputType != null && baseType != _lastInputType)
404404
{
405405
_groupInput = false;
406+
FlushInputBuffer();
406407
}
407408

408409
_lastInputType = baseType;

test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Hex.Tests.ps1

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public enum TestSByteEnum : sbyte {
6363
}
6464

6565
$testCases = @(
66+
@{
67+
Name = "Can process bool type 'fhx -InputObject `$true'"
68+
InputObject = $true
69+
Count = 1
70+
ExpectedResult = "00000000 01 00 00 00"
71+
}
6672
@{
6773
Name = "Can process byte type 'fhx -InputObject [byte]5'"
6874
InputObject = [byte]5
@@ -159,6 +165,12 @@ public enum TestSByteEnum : sbyte {
159165
}
160166

161167
$testCases = @(
168+
@{
169+
Name = "Can process bool type '`$true | fhx'"
170+
InputObject = $true
171+
Count = 1
172+
ExpectedResult = "0000000000000000 01"
173+
}
162174
@{
163175
Name = "Can process byte type '[byte]5 | fhx'"
164176
InputObject = [byte]5
@@ -227,6 +239,13 @@ public enum TestSByteEnum : sbyte {
227239
ExpectedResult = "0000000000000000 F1 12 15 FB ñ��û"
228240
ExpectedSecondResult = "0000000000000000 01 02 03 04 05 06 ������"
229241
}
242+
@{
243+
Name = "Can process jagged array type '[bool[]](`$true, `$false), [int[]](1, 2, 3, 4) | fhx'"
244+
InputObject = [bool[]]($true, $false), [int[]](1, 2, 3, 4)
245+
Count = 2
246+
ExpectedResult = "0000000000000000 01 00 00 00 00 00 00 00 �"
247+
ExpectedSecondResult = "0000000000000000 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 � � � �"
248+
}
230249
@{
231250
Name = "Can process PS-native enum array '[TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour') | fhx'"
232251
InputObject = [TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour')
@@ -291,6 +310,19 @@ public enum TestSByteEnum : sbyte {
291310
"System.UInt16[]"
292311
).ForEach{ [regex]::Escape($_) } -join '|'
293312
}
313+
@{
314+
InputScript = { $true, $false, $true, 123, 100, 76, $true, $false }
315+
Count = 3
316+
ExpectedResults = @(
317+
"0000000000000000 01 00 00 00 00 00 00 00 01 00 00 00 � �"
318+
"0000000000000000 7B 00 00 00 64 00 00 00 4C 00 00 00 { d L"
319+
"0000000000000000 01 00 00 00 00 00 00 00 �"
320+
)
321+
ExpectedLabels = @(
322+
"System.Boolean"
323+
"System.Int32"
324+
).ForEach{ [regex]::Escape($_) } -join '|'
325+
}
294326
)
295327

296328
It 'can process jagged input: <InputScript>' -TestCases $heterogenousInputCases {
@@ -356,8 +388,7 @@ public enum TestSByteEnum : sbyte {
356388

357389
if ($PathCase) {
358390
$result = Format-Hex -Path $Path
359-
}
360-
else {
391+
} else {
361392
# LiteralPath
362393
$result = Format-Hex -LiteralPath $Path
363394
}
@@ -380,8 +411,7 @@ public enum TestSByteEnum : sbyte {
380411
$Result.Bytes[-1] | Should -Be 0x0A
381412
$Result.Bytes[-2] | Should -Be 0x0D
382413
$Result.Bytes.Length | Should -Be 14
383-
}
384-
else {
414+
} else {
385415
$Result.Bytes[-1] | Should -Be 0x0A
386416
$Result.Bytes.Length | Should -Be 13
387417
}
@@ -507,8 +537,7 @@ public enum TestSByteEnum : sbyte {
507537

508538
if ($PathCase) {
509539
$output = Format-Hex -Path $InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue
510-
}
511-
else {
540+
} else {
512541
# LiteralPath
513542
$output = Format-Hex -LiteralPath $InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue
514543
}

0 commit comments

Comments
 (0)