Skip to content

Commit

Permalink
[Bug] Backward compatibility: Test-JSONChecksum
Browse files Browse the repository at this point in the history
    - To ensure backward compatibility, we use the older Test-JSON -JSON syntax instead of the Test-JSON -Path command, which was introduced in later versions of PowerShell 7.
  • Loading branch information
TinaMor committed Aug 29, 2024
1 parent aeaf32e commit c55a494
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ You can find documentation for these functions here: [Containers-Toolkit Documen

## Prerequisites

This module requires `ThreadJob` and `HNS` modules.
1. PowerShell Version 7

1. `ThreadJob` module

Expand Down
20 changes: 6 additions & 14 deletions Tests/CommonToolUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,10 @@ Describe "CommonToolUtilities.psm1" {
}

# Validate Checksum file content
Should -Invoke Get-Content -ModuleName "CommonToolUtilities" -ParameterFilter {
$Path -eq $Script:ChecksumFile
}
Should -Invoke Get-Content -ModuleName "CommonToolUtilities" -ParameterFilter { $Path -eq $Script:ChecksumFile }

# Validate JSON file
# Should -Invoke Get-Content -ModuleName "CommonToolUtilities" -ParameterFilter {
# $Path -eq "$Script:SchemaFile"
# } # FIXME: This test is failing
Should -Invoke Test-Json -Times 1 -Scope It -ModuleName "CommonToolUtilities" -ParameterFilter {
$Path -eq $Script:ChecksumFile -and
$SchemaFile -eq $Script:SchemaFile
}
Should -Invoke Test-Json -Times 1 -Scope It -ModuleName "CommonToolUtilities"

# Assert success
Should -Invoke Get-FileHash -Times 1 -Scope It -ModuleName "CommonToolUtilities" -ParameterFilter {
Expand Down Expand Up @@ -467,12 +459,12 @@ Describe "CommonToolUtilities.psm1" {

# Validate JSON file
Should -Invoke Get-Content -Times 1 -Scope It -ModuleName "CommonToolUtilities" -ParameterFilter {
$Path -eq $Script:SchemaFile
$Path -eq $Script:ChecksumFile
}
Should -Invoke Test-Json -Times 1 -Scope It -ModuleName "CommonToolUtilities" -ParameterFilter {
$Path -eq $Script:ChecksumFile -and
$SchemaFile -eq $Script:SchemaFile
Should -Invoke Get-Content -Times 1 -Scope It -ModuleName "CommonToolUtilities" -ParameterFilter {
$Path -eq $Script:SchemaFile
}
Should -Invoke Test-Json -Times 1 -Scope It -ModuleName "CommonToolUtilities"

# Assert success
Should -Invoke Get-FileHash -Times 1 -Scope It -ModuleName "CommonToolUtilities" -ParameterFilter {
Expand Down
8 changes: 6 additions & 2 deletions containers-toolkit/Private/CommonToolUtilities.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
###########################################################################
###########################################################################
# #
# Copyright (c) Microsoft Corporation. All rights reserved. #
# #
Expand Down Expand Up @@ -339,6 +339,7 @@ function DownloadCheckSumFile {
Throw "Checksum file download failed: $checksumUri.`n$($_.Exception.Message)"
}

Write-Debug "Checksum file downloaded to `"$OutFile`""
return $OutFile
}

Expand All @@ -350,6 +351,8 @@ function ValidateJSONChecksumFile {
[String]$SchemaFile
)

Write-Debug "Validating JSON checksum file...`n`t Checksum file path:$ChecksumFilePath`n`tSchema file: $SchemaFile"

# Check if the schema file exists
if (-not (Test-Path -Path $SchemaFile)) {
Throw "Couldn't find the provided schema file: $SchemaFile"
Expand All @@ -363,7 +366,8 @@ function ValidateJSONChecksumFile {
# Test JSON checksum file is valid
try {
Write-Debug "Validating checksum JSON file $checksumFilePath"
return (Test-Json -Path "$checksumFilePath" -SchemaFile $SchemaFile)
$isValidJSON = Test-Json -Json (Get-Content -Path $checksumFilePath -Raw) -Schema (Get-Content -Path $schemafile -Raw)
return $isValidJSON
}
catch {
Throw "Invalid JSON format in checksum file. $_"
Expand Down

0 comments on commit c55a494

Please sign in to comment.