diff --git a/src/Generate-DockerImageVariants/private/Get-Definition.Tests.ps1 b/src/Generate-DockerImageVariants/private/Get-Definition.Tests.ps1 index 696c9a2..9ab3780 100644 --- a/src/Generate-DockerImageVariants/private/Get-Definition.Tests.ps1 +++ b/src/Generate-DockerImageVariants/private/Get-Definition.Tests.ps1 @@ -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' + } + } } diff --git a/src/Generate-DockerImageVariants/private/Get-Definition.ps1 b/src/Generate-DockerImageVariants/private/Get-Definition.ps1 index 20305e4..0ee7b04 100644 --- a/src/Generate-DockerImageVariants/private/Get-Definition.ps1 +++ b/src/Generate-DockerImageVariants/private/Get-Definition.ps1 @@ -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 + } }