Skip to content

Commit 92558b1

Browse files
authored
Backward compatibility for validating JSON files using Test-JSON command (#78)
* Backward compatibility for validating JSON files using Test-JSON command. - Skip JSON validation if `Test-Json` command is not available, for backward compatibility (PowerShell 5.1). - Update README.md recommending PowerShell 7.0+ for best experience. - Throw an error is JSON validation fails
1 parent 216fb37 commit 92558b1

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ You can find documentation for these functions here: [Containers-Toolkit Documen
3232

3333
1. PowerShell: Minimum Version 7
3434

35+
> NOTE: While this module is developed and tested with PowerShell 7, it may work with PowerShell 5.1, but it is not guaranteed. PowerShell 7 is recommended for the best experience.
36+
3537
1. `HNS` module
3638

3739
To install the HNS module, follow the [instructions here](./docs/FAQs.md#2-new-hnsnetwork-command-does-not-exist)

Tests/CommonToolUtilities.Tests.ps1

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,15 @@ Describe "CommonToolUtilities.psm1" {
534534
-ParameterFilter { $Path -eq $Script:ChecksumFile }
535535
}
536536

537+
It "should skip JSON validation when Test-Json is not available" {
538+
Mock Get-Command -ModuleName "CommonToolUtilities" -MockWith { return $null } -ParameterFilter {
539+
$Name -eq 'Test-Json'
540+
}
541+
542+
{ & $Script:FunctionToCall } | Should -Not -Throw
543+
Should -Invoke Test-Json -Times 0 -Scope It -ModuleName "CommonToolUtilities"
544+
}
545+
537546
It "should throw an error if checksum file does not exist" {
538547
Mock Test-Path -ModuleName "CommonToolUtilities" -MockWith { return $false } -ParameterFilter { $Path -eq $Script:ChecksumFile }
539548

@@ -576,14 +585,24 @@ Describe "CommonToolUtilities.psm1" {
576585
{ & $Script:FunctionToCall } | Should -Throw "Invalid schema file: $Script:SchemaFile. Schema file is empty."
577586
}
578587

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

582591
# Test-Json returns true if the JSON is valid, otherwise it throws an error
583592
Mock Test-Json -ModuleName "CommonToolUtilities" -MockWith { Throw "Error" }
584593

585594
# Test the function
586-
{ & $Script:FunctionToCall } | Should -Throw "Invalid JSON format in checksum file. Error"
595+
{ & $Script:FunctionToCall } | Should -Throw "Error validating JSON checksum file. Error"
596+
}
597+
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."
587606
}
588607

589608
It "should throw an error if script block throws an error" {

containers-toolkit/Private/CommonToolUtilities.psm1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public enum ActionConsent {
6767
}
6868
'@
6969

70-
$HASH_FUNCTIONS = @("SHA1", "SHA256", "SHA384", "SHA512", "MD5")
71-
$HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA1|SHA256|SHA384|SHA512|MD5
70+
$HASH_FUNCTIONS = @("SHA256", "SHA384", "SHA512")
71+
$HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA256|SHA384|SHA512
7272
$NERDCTL_CHECKSUM_FILE_PATTERN = "(?<hashfunction>(?:^({0})))" -f ($HASH_FUNCTIONS -join '|')
7373
$NERDCTL_FILTER_SCRIPTBLOCK_STR = { (("{0}" -match "$NERDCTL_CHECKSUM_FILE_PATTERN") -and "{0}" -notmatch ".*.asc$") }.ToString()
7474

@@ -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"
@@ -650,11 +653,16 @@ function ValidateJSONChecksumFile {
650653

651654
# Test JSON checksum file is valid
652655
try {
656+
# Check if Test-Json cmdlet is available
657+
if (-not (Get-Command -Name Test-Json -ErrorAction SilentlyContinue)) {
658+
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."
659+
return $null
660+
}
653661
$isValidJSON = Test-Json -Json "$(Get-Content -Path $ChecksumFilePath -Raw)" -Schema "$schemaFileContent"
654662
return $isValidJSON
655663
}
656664
catch {
657-
Throw "Invalid JSON format in checksum file. $_"
665+
Throw "Error validating JSON checksum file. $($_.Exception.Message)"
658666
}
659667
}
660668

0 commit comments

Comments
 (0)