Skip to content

Commit f3e279e

Browse files
Fix null indentation in ConvertTo-YamlNode, reject tabs in indentation, strengthen empty-array and unsigned-enum test assertions
1 parent 73aa66f commit f3e279e

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/functions/private/ConvertFrom-YamlLineStream.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Lines that are empty or whitespace-only are skipped.
99
- Lines whose first non-whitespace character is `#` are skipped.
1010
- Inline comments (` #...` outside quotes) are stripped from the content.
11-
- Tabs in indentation are not allowed (YAML spec); they are treated as one space here.
11+
- Tabs in indentation are not allowed (YAML spec); a terminating error is thrown if one is found.
1212
#>
1313
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '',
1414
Justification = 'Comma-unary operator preserves List type; PSScriptAnalyzer misdetects as Object[].')]
@@ -31,9 +31,12 @@
3131
continue
3232
}
3333

34-
# Compute indent (spaces before first non-space).
34+
# Compute indent (spaces before first non-space). Tabs are invalid per YAML spec.
3535
$indent = 0
3636
while ($indent -lt $raw.Length -and ($raw[$indent] -eq ' ' -or $raw[$indent] -eq "`t")) {
37+
if ($raw[$indent] -eq "`t") {
38+
throw "YAML forbids tab characters in indentation (line $($i + 1)). Use spaces instead."
39+
}
3740
$indent++
3841
}
3942

src/functions/private/ConvertTo-YamlNode.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
}
3131

3232
if ($null -eq $Value) {
33-
$null = $Builder.Append('null').AppendLine()
33+
$indent = ' ' * ($Level * $Options.Indent)
34+
$null = $Builder.Append($indent).Append('null').AppendLine()
3435
return
3536
}
3637

tests/ConvertFrom-Yaml.Tests.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,9 @@ a:
636636
}
637637

638638
It 'Parses [] as an empty array' {
639-
$result = 'items: []' | ConvertFrom-Yaml
640-
$result.items | Should -BeNullOrEmpty
639+
$result = 'items: []' | ConvertFrom-Yaml -AsHashtable
640+
$result['items'].GetType().Name | Should -Be 'Object[]'
641+
$result['items'].Count | Should -Be 0
641642
# Verify via round-trip that ConvertTo-Yaml emits []
642643
$yaml = [ordered]@{ items = @() } | ConvertTo-Yaml
643644
$yaml | Should -Match '\[\]'
@@ -663,5 +664,10 @@ key: value
663664
'@
664665
{ $yaml | ConvertFrom-Yaml } | Should -Throw '*unexpected content*'
665666
}
667+
668+
It 'Throws on tab characters in indentation' {
669+
$yaml = "key:`n`tvalue: 1"
670+
{ $yaml | ConvertFrom-Yaml } | Should -Throw '*tab*'
671+
}
666672
}
667673
}

tests/ConvertTo-Yaml.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ Describe 'ConvertTo-Yaml' {
293293
$val = [System.Security.AccessControl.FileSystemRights]::FullControl
294294
$obj = [ordered]@{ rights = $val }
295295
$yaml = $obj | ConvertTo-Yaml
296-
$expected = [int]$val
296+
$underlyingType = [System.Enum]::GetUnderlyingType($val.GetType())
297+
$numeric = [System.Convert]::ChangeType($val, $underlyingType)
298+
$expected = ([System.IConvertible]$numeric).ToString([cultureinfo]::InvariantCulture)
297299
$yaml.Trim() | Should -Be "rights: $expected"
298300
}
299301
}

0 commit comments

Comments
 (0)