Skip to content

Commit dae2d57

Browse files
committed
Add Collection-as-Boolean
1 parent e607b7b commit dae2d57

File tree

8 files changed

+97
-39
lines changed

8 files changed

+97
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
name: test7
2-
3-
on:
4-
workflow_dispatch:
5-
push:
6-
branches:
7-
- main
8-
9-
jobs:
10-
test7:
11-
runs-on: windows-latest
12-
steps:
13-
- uses: actions/checkout@v4
14-
- name: test
15-
env:
16-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17-
shell: pwsh
18-
run: ./1.build.ps1 test
1+
name: test-core
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
test-core:
11+
runs-on: windows-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: test-core
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
shell: pwsh
18+
run: ./1.build.ps1 test
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
name: test5
2-
3-
on:
4-
workflow_dispatch:
5-
push:
6-
branches:
7-
- main
8-
9-
jobs:
10-
test5:
11-
runs-on: windows-latest
12-
steps:
13-
- uses: actions/checkout@v4
14-
- name: test
15-
env:
16-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17-
shell: powershell
18-
run: ./1.build.ps1 test
1+
name: test-desktop
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
test-desktop:
11+
runs-on: windows-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: test-desktop
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
shell: powershell
18+
run: ./1.build.ps1 test

1.build.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ Set-StrictMode -Version Latest
1111
$Major = $PSVersionTable.PSVersion.Major
1212

1313
# bootstrap
14-
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
15-
$InvokeBuildVersion = '5.11.0'
14+
if (!$MyInvocation.ScriptName.EndsWith('Invoke-Build.ps1')) {
1615
$ErrorActionPreference = 1
17-
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Definition
16+
$InvokeBuildVersion = '5.14.14'
1817
$ib = "$PSScriptRoot/packages/InvokeBuild/$InvokeBuildVersion/Invoke-Build.ps1"
1918

2019
if (!(Test-Path -LiteralPath $ib)) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
task Test-1-expected {
3+
$1, $2, $3, $4 = & ./Test-1-expected.ps1
4+
equals $1 $true
5+
equals $2 $true
6+
equals $3 $true
7+
equals $4 $true
8+
}
9+
10+
task Test-2-may-be-unexpected {
11+
$1, $2, $3, $4 = & ./Test-2-may-be-unexpected.ps1
12+
equals $1 $false
13+
equals $2 $false
14+
equals $3 $false
15+
equals $4 $false
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Collection as Boolean
2+
3+
When a collection type (.NET array, generic list, `ArrayList`, etc.) is used
4+
in a Boolean context, in many cases PowerShell evaluates it as `$true` if it
5+
contains at least one item, and `$false` if it is empty.
6+
7+
This is not true and may be unexpected if a collection has a single item which
8+
is evaluated as `$false` in a Boolean context. Such not empty collections are
9+
evaluated as `$false`.
10+
11+
Scripts
12+
13+
- [Test-1-expected.ps1](Test-1-expected.ps1) shows expected results
14+
- [Test-2-may-be-unexpected.ps1](Test-2-may-be-unexpected.ps1) shows potentially unexpected results
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
### These not empty arrays are evaluated as $true
3+
4+
# number
5+
@(42) -as [bool]
6+
7+
# string
8+
@('abc') -as [bool]
9+
10+
# object
11+
@($Host) -as [bool]
12+
13+
# Boolean
14+
@($true) -as [bool]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
### These not empty arrays are evaluated as $false
3+
4+
# number
5+
@(0) -as [bool]
6+
7+
# string
8+
@('') -as [bool]
9+
10+
# object
11+
@($null) -as [bool]
12+
13+
# Boolean
14+
@($false) -as [bool]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ to their directory. See also [TESTS]. Some scripts require
2626
- [Automatic variables as parameters or local variables](Basic/Automatic-variables)
2727
- [Break and Continue with not matching label](Basic/Break-and-Continue-with-not-matching-label)
2828
- [Break and Continue without loop](Basic/Break-and-Continue-without-loop)
29+
- [Collection as Boolean](Basic/Collection-as-Boolean)
2930
- [Collection property enumeration](Basic/Collection-property-enumeration)
3031
- [Comparison operators work differently with scalars and collections](Basic/Comparison-operators-with-collections)
3132
- [Compound assignment operators](Basic/Compound-assignment-operators)

0 commit comments

Comments
 (0)