Skip to content

Commit

Permalink
Improved requires on invalid null/empty arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Apr 12, 2024
1 parent 1327aa9 commit c732a82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Invoke-Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,30 @@ function Remove-BuildItem([Parameter(Mandatory=1)][string[]]$Path) {
}

#.ExternalHelp Help.xml
function Test-BuildAsset([Parameter(Position=0)][string[]]$Variable, [string[]]$Environment, [string[]]$Path, [string[]]$Property) {
Remove-Variable Variable, Environment, Path, Property
if ($_ = $PSBoundParameters['Variable']) {foreach($_ in $_) {
function Test-BuildAsset(
[ValidateNotNull()][string[]][Parameter(Position=0)]$Variable,
[ValidateNotNull()][string[]]$Environment,
[ValidateNotNull()][string[]]$Property,
[ValidateNotNull()][string[]]$Path
) {
Remove-Variable Variable, Environment, Property, Path
function *get($p, $n) {
if ($_ = $p[$n]) {
$_ | .{process{if ($_) {$_} else {*Die "Invalid empty '$n'."}}}
}
}
foreach($_ in *get $PSBoundParameters Variable) {
if ($null -eq ($$ = $PSCmdlet.GetVariableValue($_)) -or '' -eq $$) {*Die "Missing variable '$_'." 13}
}}
if ($_ = $PSBoundParameters['Environment']) {foreach($_ in $_) {
}
foreach($_ in *get $PSBoundParameters Environment) {
if (!([Environment]::GetEnvironmentVariable($_))) {*Die "Missing environment variable '$_'." 13}
}}
if ($_ = $PSBoundParameters['Property']) {foreach($_ in $_) {
}
foreach($_ in *get $PSBoundParameters Property) {
if ('' -eq (Get-BuildProperty $_ '')) {*Die "Missing property '$_'." 13}
}}
if ($_ = $PSBoundParameters['Path']) {foreach($_ in $_) {
}
foreach($_ in *get $PSBoundParameters Path) {
if (!(Test-Path -LiteralPath $_)) {*Die "Missing path '$_'." 13}
}}
}
}

#.ExternalHelp Help.xml
Expand Down
5 changes: 5 additions & 0 deletions Release-Notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Invoke-Build Release Notes

## v5.11.1

`requires` / `Test-BuildAsset`
- improved errors on invalid null/empty arguments

## v5.11.0

`exec` / `Invoke-BuildExec`
Expand Down
28 changes: 28 additions & 0 deletions Tests/Requires.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,31 @@ task PropertyEnvironment {

$env:miss1 = $null
}

task NullArgument1 {
try { throw requires -Variable $null }
catch { assert ($_ -like '*The argument is null.*') }

try { throw requires -Environment $null }
catch { assert ($_ -like '*The argument is null.*') }

try { throw requires -Property $null }
catch { assert ($_ -like '*The argument is null.*') }

try { throw requires -Path $null }
catch { assert ($_ -like '*The argument is null.*') }
}

task NullArgument2 {
try { throw requires -Variable host, $null }
catch { assert ("Invalid empty 'Variable'." -eq $_) }

try { throw requires -Environment path, $null }
catch { assert ("Invalid empty 'Environment'." -eq $_) }

try { throw requires -Property host, $null }
catch { assert ("Invalid empty 'Property'." -eq $_) }

try { throw requires -Path ., $null }
catch { assert ("Invalid empty 'Path'." -eq $_) }
}

0 comments on commit c732a82

Please sign in to comment.