Skip to content

Commit 7bfcbe9

Browse files
committed
Throw an error is JSON validation fails
1 parent 2f4cf34 commit 7bfcbe9

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

Tests/CommonToolUtilities.Tests.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ Describe "CommonToolUtilities.psm1" {
585585
{ & $Script:FunctionToCall } | Should -Throw "Invalid schema file: $Script:SchemaFile. Schema file is empty."
586586
}
587587

588-
It "should throw an error if the JSON file is not valid" {
588+
It "should throw an error if Test-Json fails" {
589589
Mock Get-Content -ModuleName "CommonToolUtilities" -MockWith { return "Test data" }
590590

591591
# Test-Json returns true if the JSON is valid, otherwise it throws an error
@@ -595,6 +595,16 @@ Describe "CommonToolUtilities.psm1" {
595595
{ & $Script:FunctionToCall } | Should -Throw "Error validating JSON checksum file. Error"
596596
}
597597

598+
It "should throw an error if the JSON file is not valid" {
599+
Mock Get-Content -ModuleName "CommonToolUtilities" -MockWith { return "Test data" }
600+
601+
# Test-Json returns true if the JSON is valid, otherwise it throws an error
602+
Mock Test-Json -ModuleName "CommonToolUtilities" -MockWith { return $false }
603+
604+
# Test the function
605+
{ & $Script:FunctionToCall } | Should -Throw "Checksum file validation failed for $Script:ChecksumFile. Please check the file format and schema."
606+
}
607+
598608
It "should throw an error if script block throws an error" {
599609
# ParentContainsErrorRecordException: Exception calling "Invoke" with "1" argument(s): "Error message"
600610
{ Test-CheckSum -DownloadedFile $Script:DownloadedFile -ChecksumFile $Script:ChecksumFile -JSON -SchemaFile $Script:SchemaFile -ExtractDigestScriptBlock { (Throw "Error message") } | `

containers-toolkit/Private/CommonToolUtilities.psm1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@ function Test-JSONChecksum {
579579
# Validate the checksum file
580580
$isJsonValid = ValidateJSONChecksumFile -ChecksumFilePath $checksumFile -SchemaFile $SchemaFile
581581
Write-Debug "Checksum JSON file validation status. {success: $isJsonValid}"
582+
if ($isJsonValid -eq $false) {
583+
Throw "Checksum file validation failed for $checksumFile. Please check the file format and schema."
584+
}
582585

583586
if ($null -eq $ExtractDigestScriptBlock) {
584587
Write-Debug "Using default JSON checksum extraction script block"
@@ -653,7 +656,7 @@ function ValidateJSONChecksumFile {
653656
# Check if Test-Json cmdlet is available
654657
if (-not (Get-Command -Name Test-Json -ErrorAction SilentlyContinue)) {
655658
Write-Warning "Couldn't validate JSON checksum file. Test-Json cmdlet is not available. Upgrade to PowerShell 6.1 or later to use this feature."
656-
return $false
659+
return $null
657660
}
658661
$isValidJSON = Test-Json -Json "$(Get-Content -Path $ChecksumFilePath -Raw)" -Schema "$schemaFileContent"
659662
return $isValidJSON

0 commit comments

Comments
 (0)