Skip to content

Commit 80d4c8c

Browse files
Honor false all-document removal switches
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent bf4dfbd commit 80d4c8c

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/functions/public/Remove-YamlEntry.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ function Remove-YamlEntry {
176176
-MaxScalarLength $MaxScalarLength -MaxTagLength $MaxTagLength `
177177
-MaxTotalTagLength $MaxTotalTagLength -MaxNumericLength $MaxNumericLength
178178
$sourceDocuments = [object[]] $documentBox.Value
179-
$selectAllDocuments = $AllDocuments.IsPresent -or (
180-
$PSCmdlet.ParameterSetName -ceq 'AllDocuments'
181-
)
179+
$selectAllDocuments = $AllDocuments.IsPresent
182180
if (-not $selectAllDocuments -and $DocumentIndex -ge $sourceDocuments.Count) {
183181
throw (New-YamlRemovalException `
184182
-ErrorId 'YamlRemovalDocumentIndexOutOfRange' -Message (

tests/Remove-YamlEntry.Tests.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,30 @@ root:
411411
$documents[1]['drop'] | Should -Be 'second'
412412
}
413413

414+
It 'treats an explicitly false AllDocuments switch as default nested selection' {
415+
$yaml = @'
416+
---
417+
root:
418+
drop: first
419+
keep: one
420+
---
421+
root:
422+
drop: second
423+
keep: two
424+
'@
425+
$defaultResult = Remove-YamlEntry $yaml '/root/drop'
426+
$falseResult = Remove-YamlEntry $yaml '/root/drop' -AllDocuments:$false
427+
$allResult = Remove-YamlEntry $yaml '/root/drop' -AllDocuments
428+
$defaultDocuments = @($defaultResult | ConvertFrom-Yaml -AsHashtable)
429+
$allDocuments = @($allResult | ConvertFrom-Yaml -AsHashtable)
430+
431+
$falseResult | Should -BeExactly $defaultResult
432+
$defaultDocuments[0]['root'].Contains('drop') | Should -BeFalse
433+
$defaultDocuments[1]['root']['drop'] | Should -Be 'second'
434+
$allDocuments[0]['root'].Contains('drop') | Should -BeFalse
435+
$allDocuments[1]['root'].Contains('drop') | Should -BeFalse
436+
}
437+
414438
It 'selects one explicit document index' {
415439
$yaml = "---`ndrop: first`n---`ndrop: second"
416440
$documents = @(
@@ -459,6 +483,19 @@ root:
459483
$documents[1]['third'] | Should -BeTrue
460484
}
461485

486+
It 'treats an explicitly false AllDocuments switch as default root selection' {
487+
$yaml = "---`nfirst: true`n---`nsecond: true"
488+
$defaultResult = Remove-YamlEntry $yaml ''
489+
$falseResult = Remove-YamlEntry $yaml '' -AllDocuments:$false
490+
$allResult = Remove-YamlEntry $yaml '' -AllDocuments
491+
$defaultDocuments = @($defaultResult | ConvertFrom-Yaml -AsHashtable)
492+
493+
$falseResult | Should -BeExactly $defaultResult
494+
$defaultDocuments.Count | Should -Be 1
495+
$defaultDocuments[0]['second'] | Should -BeTrue
496+
$allResult | Should -BeExactly ''
497+
}
498+
462499
It 'removes all documents with AllDocuments and an empty pointer' {
463500
$result = @(
464501
Remove-YamlEntry "---`nfirst: true`n---`nsecond: true" '' -AllDocuments

0 commit comments

Comments
 (0)