Skip to content

Commit c880736

Browse files
authored
Merge pull request #773 from luis-gasparschroeder/json-format-piping
Implemented functionality to convert the test result format into JSON
2 parents 8861e25 + 4427fda commit c880736

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

arm-ttk/Format-Json.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Function Format-Json {
2+
<#
3+
.SYNOPSIS
4+
Takes results from ARMTTK and exports them as a JSON blob.
5+
.DESCRIPTION
6+
Takes results from ARMTTK and exports them as JSON. The test cases include the filename, name of the test,
7+
whether the test was successful, and help text for how to resolve the error if the test failed.
8+
#>
9+
[CmdletBinding()]
10+
Param (
11+
# Object containing a single test result or an array of test results
12+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
13+
[psobject]$TestResult
14+
)
15+
16+
Begin {
17+
# Initialize the array to collect processed test cases
18+
$TestCases = @()
19+
}
20+
21+
Process {
22+
# Process each TestResult item one by one as they come from the pipeline
23+
$TestCase = @{
24+
filepath = $TestResult.file.fullpath
25+
name = $TestResult.name
26+
success = $TestResult.Passed
27+
}
28+
29+
if ($TestResult.Passed) {
30+
$TestCase.optional = $false
31+
}
32+
elseif ($null -ne $($TestResult.Warnings)) {
33+
$TestCase.optional = $true
34+
$TestCase.message = "$($TestResult.Warnings.Message.Replace('"', '\"')) in template file $($TestResult.file.name)"
35+
}
36+
elseif ($null -ne $($TestResult.Errors)) {
37+
$TestCase.optional = $false
38+
$TestCase.message = "$($TestResult.Errors.Exception.Message.Replace('"', '\"')) in template file $($TestResult.file.name)"
39+
}
40+
else {
41+
$TestCase.optional = $true
42+
$TestCase.message = "Unknown error in template file " + $TestResult.file.name
43+
}
44+
45+
$TestCases += $TestCase
46+
}
47+
48+
End {
49+
# Convert the array of hashtables to JSON
50+
$JSON = $TestCases | ConvertTo-Json
51+
52+
# Print the JSON string to the console
53+
Write-Output $JSON
54+
}
55+
}

arm-ttk/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ This will run the full suite of applicable tests on your template. To run a spe
2424
You can also skip tests any tests:
2525

2626
Test-AzTemplate -TemplatePath $thePathToYourTemplate -Skip apiVersions-Should-Be-Recent
27-
# This will exclude the tests indicated by the -Skip parameter from the test run and results
27+
# This will exclude the tests indicated by the -Skip parameter from the test run and results
28+
29+
You can also change the output format to get detailed test result descriptions in JSON:
30+
31+
Test-AzTemplate -TemplatePath $thePathToYourTemplate | Format-Json
32+
# This will output the test results from Test-AzTemplate to the console in JSON format
33+
Test-AzMarketplacePackage -TemplatePath $thePathToYourTemplate | Format-Json
34+
# This will output the test results from AzMarketplacePackage to the console in JSON format
2835

2936
## Running Tests on Linux
3037

0 commit comments

Comments
 (0)