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+ }
0 commit comments