From ca1508cc5489962ac3661a34e27d8e2f3a3b2cd3 Mon Sep 17 00:00:00 2001 From: Roman Kuzmin Date: Sun, 1 Sep 2024 01:54:57 +0000 Subject: [PATCH] Get-Content/OutVariable-and-ReadCount, #19 --- .../OutVariable-and-ReadCount/.test.ps1 | 18 +++++++++++++ .../OutVariable-and-ReadCount/README.md | 16 ++++++++++++ .../OutVariable-and-ReadCount/Test-1.ps1 | 25 +++++++++++++++++++ .../OutVariable-and-ReadCount/Test-2-Raw.ps1 | 25 +++++++++++++++++++ README.md | 2 ++ 5 files changed, 86 insertions(+) create mode 100644 Cmdlets/Get-Content/OutVariable-and-ReadCount/.test.ps1 create mode 100644 Cmdlets/Get-Content/OutVariable-and-ReadCount/README.md create mode 100644 Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-1.ps1 create mode 100644 Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-2-Raw.ps1 diff --git a/Cmdlets/Get-Content/OutVariable-and-ReadCount/.test.ps1 b/Cmdlets/Get-Content/OutVariable-and-ReadCount/.test.ps1 new file mode 100644 index 0000000..836f8aa --- /dev/null +++ b/Cmdlets/Get-Content/OutVariable-and-ReadCount/.test.ps1 @@ -0,0 +1,18 @@ + +Exit-Build {remove z.txt} + +task Test-1.ps1 { + ($1, $2, $3, $4 = .\Test-1.ps1) + equals $1 'System.Collections.ArrayList 2 string' + equals $2 'hello,world' + equals $3 'System.Collections.ArrayList 1 System.Object[]' + equals $4 'System.Object[]' +} + +task Test-2-Raw.ps1 { + ($1, $2, $3, $4 = .\Test-2-Raw.ps1) + equals $1 'System.Collections.ArrayList 1 string' + equals $2 "hello`nworld" + equals $3 'System.Collections.ArrayList 1 System.Object[]' + equals $4 'System.Object[]' +} diff --git a/Cmdlets/Get-Content/OutVariable-and-ReadCount/README.md b/Cmdlets/Get-Content/OutVariable-and-ReadCount/README.md new file mode 100644 index 0000000..d2c28c0 --- /dev/null +++ b/Cmdlets/Get-Content/OutVariable-and-ReadCount/README.md @@ -0,0 +1,16 @@ +# `Get-Content` with `OutVariable` and `ReadCount` + +Using `Get-Content` with `OutVariable` and `ReadCount` may produce an +unexpected nested array in the result variable. Using or not using the +switch `Raw` makes no difference. + +The workaround is not using `ReadCount` together with `OutVariable`. + +**Scripts** + +- [Test-1.ps1](Test-1.ps1) shows the issue without `Raw` +- [Test-2-Raw.ps1](Test-2-Raw.ps1) shows the same issue with `Raw` + +--- + +- [PowerShellTraps/issues/19](https://github.com/nightroman/PowerShellTraps/issues/19) diff --git a/Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-1.ps1 b/Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-1.ps1 new file mode 100644 index 0000000..0627f9a --- /dev/null +++ b/Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-1.ps1 @@ -0,0 +1,25 @@ + +# Make a text file with 2 lines +$null = New-Item z.txt -Value "hello`nworld" -Force + +### Test 1 (expected result) + +# without -ReadCount +$null = Get-Content z.txt -OutVariable var1 + +# we get a list with 2 string items +"$($var1.GetType()) $($var1.Count) $($var1[0].GetType())" + +# and this gets expected `hello,world` +$var1 -join ',' + +### Test 2 ("unexpected" result) + +# with -ReadCount +$null = Get-Content z.txt -OutVariable var2 -ReadCount 0 + +# we get a list with 1 item, a nested array +"$($var2.GetType()) $($var2.Count) $($var2[0].GetType())" + +# and this gets "unexpected" `System.Object[]` +$var2 -join ',' diff --git a/Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-2-Raw.ps1 b/Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-2-Raw.ps1 new file mode 100644 index 0000000..3da4d07 --- /dev/null +++ b/Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-2-Raw.ps1 @@ -0,0 +1,25 @@ + +# Make a text file with 2 lines +$null = New-Item z.txt -Value "hello`nworld" -Force + +### Test 1 (expected result) + +# with -Raw without -ReadCount +$null = Get-Content z.txt -OutVariable var1 -Raw + +# we get a list with 1 string item +"$($var1.GetType()) $($var1.Count) $($var1[0].GetType())" + +# and this gets expected text "hello`nworld" +$var1 -join ',' + +### Test 2 ("unexpected" result) + +# with -Raw and -ReadCount +$null = Get-Content z.txt -OutVariable var2 -Raw -ReadCount 0 + +# we get a list with 1 item, a nested array +"$($var2.GetType()) $($var2.Count) $($var2[0].GetType())" + +# and this gets "unexpected" `System.Object[]` +$var2 -join ',' diff --git a/README.md b/README.md index f835380..4d412e0 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,8 @@ to their directory. See also [TESTS]. Some scripts require - [`Get-ChildItem -LiteralPath -Recurse` gets nothing for a directory with brackets](Cmdlets/Get-ChildItem/Directory-with-brackets) - [`Get-ChildItem -Recurse` and missing `-Path` unexpected search](Cmdlets/Get-ChildItem/Missing-path-and-Recurse) - [`Get-ChildItem -LiteralPath -Recurse` ignores `-Include`](Cmdlets/Get-ChildItem/v5-LiteralPath-Recurse-ignores-Include) +- Get-Content + - [`Get-Content` with `OutVariable` and `ReadCount`](Cmdlets/Get-Content/OutVariable-and-ReadCount) - Get-Event - [`Get-Event` erratic failures in v2-4](Cmdlets/Get-Event/v2-4-Erratic-failure) - Get-Item