Skip to content

Commit f29829d

Browse files
committed
update so json is does not care about whitespace
1 parent 4ed2332 commit f29829d

4 files changed

Lines changed: 33 additions & 36 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ step-templates/*.py
1515
diff-output/
1616
/.vs
1717
!.vscode
18+
*copy.ps1

tools/StepTemplatePacker/tests/ConvertTo-OctopusJson.Tests.ps1

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
$ErrorActionPreference = "Stop";
22
Set-StrictMode -Version "Latest";
3-
4-
function Assert-JsonEquivalent {
5-
param(
6-
[Parameter(Mandatory = $true)]
7-
[string] $ActualJson,
8-
[Parameter(Mandatory = $true)]
9-
[string] $ExpectedJson
10-
)
11-
12-
$actualValue = ConvertFrom-Json -InputObject $ActualJson | ConvertTo-Json -Depth 10 -Compress
13-
$expectedValue = ConvertFrom-Json -InputObject $ExpectedJson | ConvertTo-Json -Depth 10 -Compress
14-
$actualValue | Should Be $expectedValue
15-
}
3+
. (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "Test-JsonAssertions.ps1")
164

175
Describe "ConvertTo-OctopusDeploy" {
186

@@ -68,7 +56,7 @@ Describe "ConvertTo-OctopusDeploy" {
6856
It "InputObject is a populated array" {
6957
$input = @( $null, 100, "my string" );
7058
$expected = "[`r`n null,`r`n 100,`r`n `"my string`"`r`n]";
71-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $input) -ExpectedJson $expected
59+
ConvertTo-OctopusJson -InputObject $input | Should BeJsonEquivalent $expected
7260
}
7361

7462
It "InputObject is an empty PSCustomObject" {
@@ -102,10 +90,10 @@ Describe "ConvertTo-OctopusDeploy" {
10290
"myPsObject": {
10391
"childProperty": "childValue"
10492
}
105-
}
93+
}
10694
"@
10795
$expected = $expected.Trim()
108-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $input) -ExpectedJson $expected
96+
ConvertTo-OctopusJson -InputObject $input | Should BeJsonEquivalent $expected
10997
}
11098

11199
It "InputObject is an unhandled type" {

tools/StepTemplatePacker/tests/Set-OctopusStepTemplateProperty.Tests.ps1

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
$ErrorActionPreference = "Stop";
22
Set-StrictMode -Version "Latest";
3-
4-
function Assert-JsonEquivalent {
5-
param(
6-
[Parameter(Mandatory = $true)]
7-
[string] $ActualJson,
8-
[Parameter(Mandatory = $true)]
9-
[string] $ExpectedJson
10-
)
11-
12-
$actualValue = ConvertFrom-Json -InputObject $ActualJson | ConvertTo-Json -Depth 10 -Compress
13-
$expectedValue = ConvertFrom-Json -InputObject $ExpectedJson | ConvertTo-Json -Depth 10 -Compress
14-
$actualValue | Should Be $expectedValue
15-
}
3+
. (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "Test-JsonAssertions.ps1")
164

175
Describe "Set-OctopusStepTemplateProperty" {
186

@@ -22,7 +10,7 @@ Describe "Set-OctopusStepTemplateProperty" {
2210
-PropertyName "Octopus.Action.Script.Syntax" `
2311
-Value "PowerShell";
2412
$expected = "{`r`n `"Properties`": {`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
25-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
13+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
2614
}
2715

2816
It "No properties exist" {
@@ -31,7 +19,7 @@ Describe "Set-OctopusStepTemplateProperty" {
3119
-PropertyName "Octopus.Action.Script.Syntax" `
3220
-Value "PowerShell";
3321
$expected = "{`r`n `"Properties`": {`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
34-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
22+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
3523
}
3624

3725
It "Specified property does not exist" {
@@ -40,7 +28,7 @@ Describe "Set-OctopusStepTemplateProperty" {
4028
-PropertyName "Octopus.Action.Script.Syntax" `
4129
-Value "PowerShell";
4230
$expected = "{`r`n `"Properties`": {`r`n `"otherProperty`": `"`",`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
43-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
31+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
4432
}
4533

4634
It "Property does not exist" {
@@ -49,7 +37,7 @@ Describe "Set-OctopusStepTemplateProperty" {
4937
-PropertyName "Octopus.Action.Script.Syntax" `
5038
-Value "PowerShell";
5139
$expected = "{`r`n `"Properties`": {`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
52-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
40+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
5341
}
5442

5543
It "Property exists with a null value" {
@@ -58,7 +46,7 @@ Describe "Set-OctopusStepTemplateProperty" {
5846
-PropertyName "Octopus.Action.Script.Syntax" `
5947
-Value "PowerShell";
6048
$expected = "{`r`n `"Properties`": {`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
61-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
49+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
6250
}
6351

6452
It "Property exists with an empty string value" {
@@ -67,7 +55,7 @@ Describe "Set-OctopusStepTemplateProperty" {
6755
-PropertyName "Octopus.Action.Script.Syntax" `
6856
-Value "PowerShell";
6957
$expected = "{`r`n `"Properties`": {`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
70-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
58+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
7159
}
7260

7361
It "Property exists with a string value" {
@@ -76,7 +64,7 @@ Describe "Set-OctopusStepTemplateProperty" {
7664
-PropertyName "Octopus.Action.Script.Syntax" `
7765
-Value "PowerShell";
7866
$expected = "{`r`n `"Properties`": {`r`n `"Octopus.Action.Script.Syntax`": `"PowerShell`"`r`n }`r`n}";
79-
Assert-JsonEquivalent -ActualJson (ConvertTo-OctopusJson -InputObject $stepJson) -ExpectedJson $expected
67+
ConvertTo-OctopusJson -InputObject $stepJson | Should BeJsonEquivalent $expected
8068
}
8169

8270

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function global:ConvertTo-CompressedJsonForAssertion {
2+
param(
3+
[Parameter(Mandatory = $true)]
4+
[string] $Json
5+
)
6+
7+
return (ConvertFrom-Json -InputObject $Json | ConvertTo-Json -Depth 10 -Compress)
8+
}
9+
10+
function global:PesterBeJsonEquivalent($value, $expected) {
11+
return (ConvertTo-CompressedJsonForAssertion -Json $value) -eq (ConvertTo-CompressedJsonForAssertion -Json $expected)
12+
}
13+
14+
function global:PesterBeJsonEquivalentFailureMessage($value, $expected) {
15+
return "Expected JSON equivalent to: {$expected}`nBut was: {$value}"
16+
}
17+
18+
function global:NotPesterBeJsonEquivalentFailureMessage($value, $expected) {
19+
return "Expected JSON not equivalent to: {$expected}`nBut was: {$value}"
20+
}

0 commit comments

Comments
 (0)