Skip to content

Commit 08d24a3

Browse files
committed
Skip JSON validation if Test-Json command is not available, for backward compatibility (PowerShell 5.1).
1 parent 1dc3619 commit 08d24a3

File tree

7 files changed

+9
-251
lines changed

7 files changed

+9
-251
lines changed

.textlintrc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"/nat/gi",
3939
"NatNetwork",
4040
"nerdctl",
41-
"/nuget/gi",
4241
"PowerShell",
4342
"/PSCustomObject/gi",
4443
"subnet",

Tests/CommonToolUtilities.Tests.ps1

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

537-
It "should use custom Test-JSON if command not exists" {
538-
Mock Get-Command -ModuleName "CommonToolUtilities" -MockWith { return $null }
539-
Mock Get-Content -ModuleName "CommonToolUtilities" `
540-
-MockWith { return ($SbomJson -replace "__CHECKSUM__", "SampleHash") } `
541-
-ParameterFilter { $Path -eq $Script:ChecksumFile }
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+
}
542541

543-
# We rely on the absence of the required types (Newtonsoft.Json)
544-
# to confirm that your fallback logic is being exercised.
545-
{ & $Script:FunctionToCall } | Should -Throw "*Unable to find type*Newtonsoft.Json*"
542+
{& $Script:FunctionToCall} | Should -Not -Throw
543+
Should -Invoke Test-Json -Times 0 -Scope It -ModuleName "CommonToolUtilities"
546544
}
547545

548546
It "should throw an error if checksum file does not exist" {

build/hacks/Load-NewtonsoftDlls.ps1

Lines changed: 0 additions & 110 deletions
This file was deleted.

containers-toolkit/Private/CommonToolUtilities.psm1

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

70-
$HASH_FUNCTIONS = @("SHA1", "SHA256", "SHA384", "SHA512", "MD5")
70+
$HASH_FUNCTIONS = @("SHA256", "SHA384", "SHA512")
7171
$HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA1|SHA256|SHA384|SHA512|MD5
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()
@@ -652,10 +652,8 @@ function ValidateJSONChecksumFile {
652652
try {
653653
# Check if Test-Json cmdlet is available
654654
if (-not (Get-Command -Name Test-Json -ErrorAction SilentlyContinue)) {
655-
Write-Debug "Using custom JSON validation module."
656-
Import-Module (Join-Path $ModuleParentPath "Private\Json-Validator.psm1") -DisableNameChecking -Force -ErrorAction Stop
657-
} else {
658-
Write-Debug "Using built-in Test-Json cmdlet."
655+
Write-Warning "Couldn't validate JSON checksum file. Test-Json cmdlet is not available. Upgrade to PowerShell 7.0 or later to use this feature."
656+
return $false
659657
}
660658
$isValidJSON = Test-Json -Json "$(Get-Content -Path $ChecksumFilePath -Raw)" -Schema "$schemaFileContent"
661659
return $isValidJSON

containers-toolkit/Private/Json-Validator.psm1

Lines changed: 0 additions & 85 deletions
This file was deleted.

containers-toolkit/containers-toolkit.psd1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Note that the HostNetworkingService module is available only when the Hyper-V Wi
6969
NestedModules = @(
7070
'Private\CommonToolUtilities.psm1',
7171
'Private\UpdateEnvironmentPath.psm1',
72-
'Private\Json-Validator.psm1',
7372
'Public\AllToolsUtilities.psm1',
7473
'Public\BuildkitTools.psm1',
7574
'Public\ContainerdTools.psm1',
@@ -126,7 +125,6 @@ Note that the HostNetworkingService module is available only when the Hyper-V Wi
126125
FileList = @(
127126
'./Private/CommonToolUtilities.psm1',
128127
'./Private/UpdateEnvironmentPath.psm1',
129-
'./Private/Json-Validator.psm1',
130128
'./Public/AllToolsUtilities.psm1',
131129
'./Public/BuildkitTools.psm1',
132130
'./Public/ContainerdTools.psm1',

docs/FAQs.md

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
- [`Import-Module` issues](#import-module-issues)
66
- [Tool uninstall issues](#tool-uninstall-issues)
77
- [`Initialize-NatNetwork` issues](#initialize-natnetwork-issues)
8-
- [`'Test-Json' is not recognized as the name of a cmdlet`](#test-json-is-not-recognized-as-the-name-of-a-cmdlet)
98

109
## `Import-Module` issues
1110

@@ -108,42 +107,3 @@ Import-Module HNS
108107
> [!CAUTION]
109108
> **We do not recommend** using the [_third-party_ HNS module](https://www.powershellgallery.com/packages/HNS/0.2.4) available in the PowerShell Gallery. This module is **NOT** maintained and signed by Microsoft and may not be up-to-date with the latest changes in the HNS module.
110109
> _Microsoft does **NOT** take any responsibility for issues that may arise from installing and/or execution of commands in any third-party modules/scripts. Install the _third-party_ HNS module at your own risk!_
111-
112-
## `'Test-Json' is not recognized as the name of a cmdlet`
113-
114-
This error occurs because the `Test-Json` cmdlet was introduced in **PowerShell 6.1** and is not available in **Windows PowerShell 5.1** or earlier. To maintain compatibility, a custom implementation of `Test-Json` is provided for environments where the native cmdlet is unavailable.
115-
116-
To enable this compatibility, you must ensure that the `Newtonsoft.Json` and `Newtonsoft.Json.Schema` assemblies are loaded in your PowerShell session. You can do this in one of two ways:
117-
118-
**Option 1 – Use the loader script**
119-
120-
Use the `Load-NewtonsoftDlls` script to automatically download and load the required assemblies:
121-
122-
```powershell
123-
.\build\hacks\Load-NewtonsoftDlls.ps1
124-
```
125-
126-
**Option 2 – Load the assemblies manually**
127-
128-
1. **Download the NuGet package**:
129-
130-
```powershell
131-
$newtonsoftDir = \"$env:USERPROFILE\Documents\PowerShell\Modules\Newtonsoft.Json\"
132-
nuget.exe install Newtonsoft.Json.Schema -OutputDirectory $newtonsoftDir -Framework \"net45\" -ExcludeVersion
133-
```
134-
135-
2. **Load the assemblies**:
136-
137-
```powershell
138-
$jsondllPath = \"$newtonsoftDir\\Newtonsoft.Json\\lib\\net45\\Newtonsoft.Json.dll\"
139-
[Reflection.Assembly]::LoadFile($jsondllPath) | Out-Null
140-
141-
$schemadllPath = \"$newtonsoftDir\\Newtonsoft.Json.Schema\\lib\\net45\\Newtonsoft.Json.Schema.dll\"
142-
[Reflection.Assembly]::LoadFile($schemadllPath) | Out-Null
143-
```
144-
145-
### Reference
146-
147-
- [Test-Json](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/test-json?view=powershell-7.4)
148-
- [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json#supportedframeworks-body-tab)
149-
- [Differences between Windows PowerShell 5.1 and PowerShell 7.x](https://learn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell?view=powershell-7.5#new-test-json-cmdlet)

0 commit comments

Comments
 (0)