mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add -Path and -LiteralPath parameters to Test-Json cmdlet (#19042)
This commit is contained in:
@@ -3,11 +3,11 @@
|
||||
|
||||
Describe "Test-Json" -Tags "CI" {
|
||||
BeforeAll {
|
||||
$validSchemaJsonPath = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath valid_schema_reference.json
|
||||
|
||||
$invalidSchemaJsonPath = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath invalid_schema_reference.json
|
||||
|
||||
$missingSchemaJsonPath = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath no_such_file.json
|
||||
$assetsPath = Join-Path $PSScriptRoot -ChildPath assets
|
||||
$validSchemaJsonPath = Join-Path -Path $assetsPath -ChildPath valid_schema_reference.json
|
||||
$invalidSchemaJsonPath = Join-Path -Path $assetsPath -ChildPath invalid_schema_reference.json
|
||||
$missingSchemaJsonPath = Join-Path -Path $assetsPath -ChildPath no_such_file.json
|
||||
$missingJsonPath = Join-Path -Path $assetsPath -ChildPath no_such_file.json
|
||||
|
||||
$validSchemaJson = @"
|
||||
{
|
||||
@@ -65,48 +65,139 @@ Describe "Test-Json" -Tags "CI" {
|
||||
errorNode
|
||||
}
|
||||
"@
|
||||
|
||||
$validJsonPath = Join-Path -Path $TestDrive -ChildPath 'validJson.json'
|
||||
$validLiteralJsonPath = Join-Path -Path $TestDrive -ChildPath "[valid]Json.json"
|
||||
$invalidNodeInJsonPath = Join-Path -Path $TestDrive -ChildPath 'invalidNodeInJson.json'
|
||||
$invalidTypeInJsonPath = Join-Path -Path $TestDrive -ChildPath 'invalidTypeInJson.json'
|
||||
$invalidTypeInJson2Path = Join-Path -Path $TestDrive -ChildPath 'invalidTypeInJson2.json'
|
||||
$invalidEmptyJsonPath = Join-Path -Path $TestDrive -ChildPath 'emptyJson.json'
|
||||
|
||||
Set-Content -Path $validJsonPath -Value $validJson
|
||||
Set-Content -LiteralPath $validLiteralJsonPath -Value $validJson
|
||||
Set-Content -Path $invalidNodeInJsonPath -Value $invalidNodeInJson
|
||||
Set-Content -Path $invalidTypeInJsonPath -Value $invalidTypeInJson
|
||||
Set-Content -Path $invalidTypeInJson2Path -Value $invalidTypeInJson2
|
||||
New-Item -Path $invalidEmptyJsonPath -ItemType File
|
||||
}
|
||||
|
||||
It "Missing JSON schema file doesn't exist" {
|
||||
Test-Path -LiteralPath $missingSchemaJsonPath | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Missing JSON file doesn't exist" {
|
||||
Test-Path -LiteralPath $missingJsonPath | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json is valid" {
|
||||
Test-Json -Json $validJson | Should -BeTrue
|
||||
($validJson | Test-Json) | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json is valid against a valid schema from string" {
|
||||
Test-Json -Json $validJson -Schema $validSchemaJson | Should -BeTrue
|
||||
($validJson | Test-Json -Schema $validSchemaJson) | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json is valid against a valid schema from file" {
|
||||
Test-Json -Json $validJson -SchemaFile $validSchemaJsonPath | Should -BeTrue
|
||||
($validJson | Test-Json -SchemaFile $validSchemaJsonPath) | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file specified using -Path is valid" {
|
||||
Test-Json -Path $validJsonPath | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file specified using -LiteralPath is valid" {
|
||||
Test-Json -LiteralPath $validLiteralJsonPath | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file specified using LiteralPath aliases -PSPath and -LP is valid" {
|
||||
Test-Json -PSPath $validLiteralJsonPath | Should -BeTrue
|
||||
Test-Json -LP $validLiteralJsonPath | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file specified using -Path from pipeline is valid" {
|
||||
(Get-ChildItem -Path $validJsonPath -File | Test-Json) | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file specified using -LiteralPath from pipeline is valid" {
|
||||
(Get-ChildItem -LiteralPath $validLiteralJsonPath -File | Test-Json) | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file is valid against a valid schema from string" {
|
||||
Test-Json -Path $validJsonPath -Schema $validSchemaJson | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json file is valid against a valid schema from file" {
|
||||
Test-Json -Path $validJsonPath -SchemaFile $validSchemaJsonPath | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Json is invalid" {
|
||||
Test-Json -Json $invalidNodeInJson -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
($invalidNodeInJson | Test-Json -ErrorAction SilentlyContinue) | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json is invalid against a valid schema from string" {
|
||||
Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
($invalidTypeInJson2 | Test-Json -Schema $validSchemaJson -ErrorAction SilentlyContinue) | Should -BeFalse
|
||||
|
||||
Test-Json -Json $invalidNodeInJson -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
($invalidNodeInJson | Test-Json -Schema $validSchemaJson -ErrorAction SilentlyContinue) | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json is invalid against a valid schema from file" {
|
||||
Test-Json -Json $invalidTypeInJson2 -SchemaFile $validSchemaJsonPath -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
($invalidTypeInJson2 | Test-Json -SchemaFile $validSchemaJsonPath -ErrorAction SilentlyContinue) | Should -BeFalse
|
||||
|
||||
Test-Json -Json $invalidNodeInJson -SchemaFile $validSchemaJsonPath -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
($invalidNodeInJson | Test-Json -SchemaFile $validSchemaJsonPath -ErrorAction SilentlyContinue) | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json file is invalid against a valid schema from file" {
|
||||
Test-Json -Path $invalidTypeInJson2Path -SchemaFile $validSchemaJsonPath -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
Test-Json -Path $invalidNodeInJsonPath -SchemaFile $validSchemaJsonPath -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json file is invalid" {
|
||||
Test-Json -Path $invalidNodeInJsonPath -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json file is invalid against a valid schema from string" {
|
||||
Test-Json -Path $invalidTypeInJson2Path -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
Test-Json -Path $invalidNodeInJsonPath -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Json file is invalid against an empty file" {
|
||||
Test-Json -Path $invalidEmptyJsonPath -ErrorAction SilentlyContinue | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Test-Json throw if a schema from string is invalid" {
|
||||
{ Test-Json -Json $validJson -Schema $invalidSchemaJson -ErrorAction Stop } | Should -Throw -ErrorId "InvalidJsonSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
{ Test-Json -Path $validJsonPath -Schema $invalidSchemaJson -ErrorAction Stop } | Should -Throw -ErrorId "InvalidJsonSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json throw if a schema from file is invalid" {
|
||||
{ Test-Json -Json $validJson -SchemaFile $invalidSchemaJsonPath -ErrorAction Stop } | Should -Throw -ErrorId "InvalidJsonSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
{ Test-Json -Path $validJsonPath -SchemaFile $invalidSchemaJsonPath -ErrorAction Stop } | Should -Throw -ErrorId "InvalidJsonSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json throw if a path to a schema from file is invalid" {
|
||||
{ Test-Json -Json $validJson -SchemaFile $missingSchemaJsonPath -ErrorAction Stop } | Should -Throw -ErrorId "JsonSchemaFileOpenFailure,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
{ Test-Json -Path $validJsonPath -SchemaFile $missingSchemaJsonPath -ErrorAction Stop } | Should -Throw -ErrorId "JsonSchemaFileOpenFailure,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json throw if a path from file is invalid" {
|
||||
{ Test-Json -Path $missingJsonPath -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json throw if a path from file using -Path is a literal path" {
|
||||
{ Test-Json -Path $validLiteralJsonPath -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Json file throw if a path from file using -LiteralPath is a wildcard or regular expression" {
|
||||
{ Test-Json -LiteralPath (Join-Path -Path $TestDrive -ChildPath "*Json.json") -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
{ Test-Json -LiteralPath (Join-Path -Path $TestDrive -ChildPath "[a-z]Json.json") -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json write an error on invalid (<name>) Json against a valid schema from string" -TestCases @(
|
||||
@@ -133,6 +224,30 @@ Describe "Test-Json" -Tags "CI" {
|
||||
$errorVar.FullyQualifiedErrorId | Should -BeExactly $errorId
|
||||
}
|
||||
|
||||
It "Test-Json write an error on invalid (<name>) Json file against a valid schema from string" -TestCases @(
|
||||
@{ name = "type"; json = $invalidTypeInJsonPath; errorId = "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" }
|
||||
@{ name = "node"; json = $invalidNodeInJsonPath; errorId = "InvalidJson,Microsoft.PowerShell.Commands.TestJsonCommand" }
|
||||
) {
|
||||
param ($json, $errorId)
|
||||
|
||||
$errorVar = $null
|
||||
Test-Json -Path $json -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue
|
||||
|
||||
$errorVar.FullyQualifiedErrorId | Should -BeExactly $errorId
|
||||
}
|
||||
|
||||
It "Test-Json write an error on invalid (<name>) Json file against a valid schema from file" -TestCases @(
|
||||
@{ name = "type"; json = $invalidTypeInJsonPath; errorId = "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" }
|
||||
@{ name = "node"; json = $invalidNodeInJsonPath; errorId = "InvalidJson,Microsoft.PowerShell.Commands.TestJsonCommand" }
|
||||
) {
|
||||
param ($json, $errorId)
|
||||
|
||||
$errorVar = $null
|
||||
Test-Json -Path $json -SchemaFile $validSchemaJsonPath -ErrorVariable errorVar -ErrorAction SilentlyContinue
|
||||
|
||||
$errorVar.FullyQualifiedErrorId | Should -BeExactly $errorId
|
||||
}
|
||||
|
||||
It "Test-Json return all errors when check invalid Json against a valid schema from string" {
|
||||
$errorVar = $null
|
||||
Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue
|
||||
@@ -153,6 +268,26 @@ Describe "Test-Json" -Tags "CI" {
|
||||
$errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json return all errors when check invalid Json file against a valid schema from string" {
|
||||
$errorVar = $null
|
||||
Test-Json -Path $invalidTypeInJson2Path -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue
|
||||
|
||||
# '$invalidTypeInJson2Path' contains two errors in property types.
|
||||
$errorVar.Count | Should -Be 2
|
||||
$errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
$errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It "Test-Json return all errors when check invalid Json file against a valid schema from file" {
|
||||
$errorVar = $null
|
||||
Test-Json -Path $invalidTypeInJson2Path -SchemaFile $validSchemaJsonPath -ErrorVariable errorVar -ErrorAction SilentlyContinue
|
||||
|
||||
# '$invalidTypeInJson2Path' contains two errors in property types.
|
||||
$errorVar.Count | Should -Be 2
|
||||
$errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
$errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
|
||||
}
|
||||
|
||||
It 'Test-Json recognizes non-object types: <name>' -TestCases @(
|
||||
@{ name = 'number'; value = 1; expected = 'number' }
|
||||
@{ name = '"true"'; value = '"true"'; expected = 'string' }
|
||||
|
||||
Reference in New Issue
Block a user