Skip to content

Commit

Permalink
Merge pull request #59 from leojonathanoh/fix/errors-show-any-errors-…
Browse files Browse the repository at this point in the history
…in-definition-files

Fix (errors): Show any errors in definition files
  • Loading branch information
leojonathanoh authored Nov 19, 2021
2 parents 8b9eefa + 590db82 commit 0cc53d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/Generate-DockerImageVariants/private/Get-Definition.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ Describe "Get-Definition" -Tag 'Unit' {
BeforeEach {
$drive = Convert-Path 'TestDrive:\'
$definitionFile = Join-Path $drive 'foo.ps1'
$definitionFileContent = '$VARIANTS = @()'
$definitionFileContent | Out-File $definitionFile -Encoding utf8 -Force
}

It 'Returns definition variable' {
$definitionFileContent = '$VARIANTS = @()'
$definitionFileContent | Out-File $definitionFile -Encoding utf8 -Force

$result = Get-Definition -Path $definitionFile -VariableName VARIANTS

$result | Should -Be @()
}

It 'throws exception on errors in definition file' {
$definitionFileContent = 'zzz'
$definitionFileContent | Out-File $definitionFile -Encoding utf8 -Force

{ Get-Definition -Path $definitionFile -VariableName VARIANTS 2>&1 } | Should -Throw 'zzz'
}

}

}
18 changes: 15 additions & 3 deletions src/Generate-DockerImageVariants/private/Get-Definition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ function Get-Definition {
[string]
$VariableName
)
. $Path > $null
try {
$definition = & {
. $Path > $null

# Send the variable down the pipeline
Get-Variable -Name $VariableName -ValueOnly -ErrorAction SilentlyContinue
# Send the variable down the pipeline
Get-Variable -Name $VariableName -ValueOnly -ErrorAction Stop
}
if ($definition -is [array]) {
,$definition
}else {
$definition
}
}catch {
Write-Error "There was an error in definition file $Path. Exception: " -ErrorAction Continue
throw
}
}

0 comments on commit 0cc53d6

Please sign in to comment.