Skip to content

Commit

Permalink
Get-Content/OutVariable-and-ReadCount, #19
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Sep 1, 2024
1 parent 0067931 commit ca1508c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Cmdlets/Get-Content/OutVariable-and-ReadCount/.test.ps1
Original file line number Diff line number Diff line change
@@ -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[]'
}
16 changes: 16 additions & 0 deletions Cmdlets/Get-Content/OutVariable-and-ReadCount/README.md
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-1.ps1
Original file line number Diff line number Diff line change
@@ -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 ','
25 changes: 25 additions & 0 deletions Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-2-Raw.ps1
Original file line number Diff line number Diff line change
@@ -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 ','
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca1508c

Please sign in to comment.