Skip to content

Commit

Permalink
Merge pull request #109 from leojonathanoh/fix/error-out-when-version…
Browse files Browse the repository at this point in the history
…s.json-is-empty-or-invalid-json

Fix: Error out when `versions.json` is empty or invalid JSON
  • Loading branch information
leojonathanoh authored Feb 17, 2024
2 parents c036275 + 6f4ff87 commit 1552a02
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,33 @@ Describe "Get-DockerImageVariantsVersions" -Tag 'Unit' {
} | Should -Throw
}

It "Gets version.json" {
It "Errors when versions.json is empty" {
Mock Get-Content {}

Get-DockerImageVariantsVersions -ErrorVariable err 2>$null

$err | Should -Not -Be $null
}

It "Gets when versions.json is empty" {
Mock Get-Content {
''
}

Get-DockerImageVariantsVersions -ErrorVariable err 2>$null

$err | Should -Not -Be $null
}

It "Gets versions.json" {
Mock Get-Content {
'[ "0.1.0", "0.2.0" ]'
'{ "somecoolpackage": { "versions": [ "0.0.1" ] } }'
}

$versions = Get-DockerImageVariantsVersions

$versions | Should -Be @( '0.1.0', '0.2.0' )
$versions -is [PSCustomObject]
$versions.psobject.Properties.Name | Should -Not -Be $null
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ function Get-DockerImageVariantsVersions {
$ErrorActionPreference = 'Stop'
try {
$VERSIONS_JSON_FILE = "./generate/definitions/versions.json"
Get-Content $VERSIONS_JSON_FILE -Encoding utf8 -Raw | ConvertFrom-Json
$content = Get-Content $VERSIONS_JSON_FILE -Encoding utf8 -Raw
if ($null -eq $content) {
throw "$VERSIONS_JSON_FILE is empty"
}else {
$o = ConvertFrom-Json -InputObject $content
if ($o -isnot [PSCustomObject]) {
throw "$VERSIONS_JSON_FILE does not contain valid JSON"
}
$o
}
}catch {
if ($callerEA -eq 'Stop') {
throw
Expand Down

0 comments on commit 1552a02

Please sign in to comment.