Skip to content

Commit

Permalink
Merge pull request #93 from leojonathanoh/enhancement/print-errors-wh…
Browse files Browse the repository at this point in the history
…en-errors-in-definitions-or-templates-are-found

Enhancement: Print errors when errors in definitions or templates are found
  • Loading branch information
leojonathanoh authored Dec 1, 2023
2 parents 601736a + 4e596c7 commit 3aef7de
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Describe "Get-ContentFromTemplate" -Tag 'Unit' {

Context 'Behavior' {

It 'Should throw on errors' {
'{' | Out-File $templateFile -Encoding utf8 -Force -Append

{
Get-ContentFromTemplate -Path $templateFile 2>$null
} | Should -Throw
}

It 'Gets content from a template' {
$content = Get-ContentFromTemplate -Path $templateFile
$content | Should -Be $templateFileContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ function Get-ContentFromTemplate {
[ValidateRange(1,100)]
[int]$PrependNewLines
)
if (! (Test-Path $Path -PathType Leaf) ) {
throw "No such file: $Path"
}

$content = & $Path
if ($PrependNewLines -gt 0) {
$content = "$( "`n" * $PrependNewLines )$content"
try {
if (! (Test-Path $Path -PathType Leaf) ) {
throw "No such file: $Path"
}

$content = & $Path
if ($PrependNewLines -gt 0) {
$content = "$( "`n" * $PrependNewLines )$content"
}
$content
}catch {
throw
}
$content
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,26 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
Context 'Behavior' {

BeforeEach {
function Get-ContentFromTemplate {
function Get-ContentFromTemplate {}
Mock Get-ContentFromTemplate {
param (
$Path
)
"Some content from $Path"
}
function Test-Path {}
Mock Test-Path { $true }
}

It 'Should throw on errors' {
$template = @{}
Mock Get-ContentFromTemplate {
throw "some error"
}

{
Get-ContextFileContent -Template $template 2>$null
} | Should -Throw
}

It 'Returns header content' {
Expand All @@ -45,7 +58,6 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
templateDirectory = 'bar'
includeHeader = $true
}
Mock Test-Path { $true }

$content = Get-ContextFileContent -Template $template
$content[0].Replace('\', '/') | Should -Match "Some content from bar/foo.header.ps1"
Expand All @@ -57,7 +69,6 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
file = 'foo'
templateDirectory = 'bar'
}
Mock Test-Path { $true }

$content = Get-ContextFileContent -Template $template
$content.Replace('\', '/') | Should -Match "Some content from bar/foo.ps1"
Expand All @@ -72,7 +83,6 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
'doe'
)
}
Mock Test-Path { $true }

$content = Get-ContextFileContent -Template $template
$content[0].Replace('\', '/') | Should -Match "Some content from bar/john/john.ps1"
Expand All @@ -85,7 +95,6 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
templateDirectory = 'bar'
includeFooter = $true
}
Mock Test-Path { $true }

$content = Get-ContextFileContent -Template $template
$content[0].Replace('\', '/') | Should -Match "Some content from bar/foo.ps1"
Expand All @@ -99,7 +108,6 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
includeHeader = $true
includeFooter = $true
}
Mock Test-Path { $true }

$content = Get-ContextFileContent -Template $template
$content[0].Replace('\', '/') | Should -Match "Some content from bar/foo.header.ps1"
Expand All @@ -118,7 +126,6 @@ Describe "Get-ContextFileContent" -Tag 'Unit' {
'doe'
)
}
Mock Test-Path { $true }

$content = Get-ContextFileContent -Template $template
$content[0].Replace('\', '/') | Should -Match "Some content from bar/foo.header.ps1"
Expand Down
55 changes: 30 additions & 25 deletions src/Generate-DockerImageVariants/private/Get-ContextFileContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,43 @@ function Get-ContextFileContent {
[hashtable]$TemplatePassVariables
)

if (! (Test-Path $Template['templateDirectory'] -PathType Container) ) {
throw "No such template directory: $( $Template['templateDirectory'] )"
}
try {
if (! (Test-Path $Template['templateDirectory'] -PathType Container) ) {
throw "No such template directory: $( $Template['templateDirectory'] )"
}

# Make PASS_VARIABLES global variable available to the template script
$global:PASS_VARIABLES = if ($TemplatePassVariables) { $TemplatePassVariables } else { @{} }
# Make PASS_VARIABLES global variable available to the template script
$global:PASS_VARIABLES = if ($TemplatePassVariables) { $TemplatePassVariables } else { @{} }

$params = @{}
if ( $Template['includeHeader'] ) {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], "$( $Template['file'] ).header.ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath
$params = @{}
if ( $Template['includeHeader'] ) {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], "$( $Template['file'] ).header.ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath

# Spaces our header from body
$params['PrependNewLines'] = 2
}
# Spaces our header from body
$params['PrependNewLines'] = 2
}

if ($Template.Contains('subTemplates') -and $Template['subTemplates'] -is [array] -and $Template['subTemplates'].Count -gt 0) {
$Template['subTemplates'] | % {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], $_, "$_.ps1")
if ($Template.Contains('subTemplates') -and $Template['subTemplates'] -is [array] -and $Template['subTemplates'].Count -gt 0) {
$Template['subTemplates'] | % {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], $_, "$_.ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath @params
}
}else {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], "$( $Template['file'] ).ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath @params
}
}else {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], "$( $Template['file'] ).ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath @params
}

if ( $Template['includeFooter'] ) {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], "$( $Template['file'] ).footer.ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath @params
if ( $Template['includeFooter'] ) {
$templateFileAbsolutePath = [IO.Path]::Combine($Template['templateDirectory'], "$( $Template['file'] ).footer.ps1")
"Processing template file: $templateFileAbsolutePath" | Write-Verbose
Get-ContentFromTemplate -Path $templateFileAbsolutePath @params
}
}catch {
Write-Error "There was an error getting content from template. Exception: $( $_.Exception.Message )" -ErrorAction Continue
throw
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Describe "Get-Definition" -Tag 'Unit' {

Context 'Behavior' {

It 'Should throw on errors' {
'{' | Out-File $definitionFile -Encoding utf8 -Force -Append

{
Get-Definition -Path $template 2>$null
} | Should -Throw
}

It 'Returns variable in definition file' {
$definitionFileContent = '$foo = @()'
$definitionFileContent | Out-File $definitionFile -Encoding utf8 -Force
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Get-Definition {
,$v
}
}catch {
Write-Error "There was an error in definition file $Path. Exception: " -ErrorAction Continue
Write-Error "There was an error in definition file $Path. Exception: $( $_.Exception.Message )" -ErrorAction Continue
throw
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function Generate-DockerImageVariants {
$GenerateConfig['FILES'] | New-RepositoryFile
}
}catch {
"Ended with errors. Please review." | Write-Host -ForegroundColor Yellow
if ($ErrorActionPreference -eq 'Stop') {
throw
}else {
Expand Down

0 comments on commit 3aef7de

Please sign in to comment.