Skip to content

Commit

Permalink
Merge pull request #773 from luis-gasparschroeder/json-format-piping
Browse files Browse the repository at this point in the history
Implemented functionality to convert the test result format into JSON
  • Loading branch information
psah434 authored Mar 22, 2024
2 parents 8861e25 + 4427fda commit c880736
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
55 changes: 55 additions & 0 deletions arm-ttk/Format-Json.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Function Format-Json {
<#
.SYNOPSIS
Takes results from ARMTTK and exports them as a JSON blob.
.DESCRIPTION
Takes results from ARMTTK and exports them as JSON. The test cases include the filename, name of the test,
whether the test was successful, and help text for how to resolve the error if the test failed.
#>
[CmdletBinding()]
Param (
# Object containing a single test result or an array of test results
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[psobject]$TestResult
)

Begin {
# Initialize the array to collect processed test cases
$TestCases = @()
}

Process {
# Process each TestResult item one by one as they come from the pipeline
$TestCase = @{
filepath = $TestResult.file.fullpath
name = $TestResult.name
success = $TestResult.Passed
}

if ($TestResult.Passed) {
$TestCase.optional = $false
}
elseif ($null -ne $($TestResult.Warnings)) {
$TestCase.optional = $true
$TestCase.message = "$($TestResult.Warnings.Message.Replace('"', '\"')) in template file $($TestResult.file.name)"
}
elseif ($null -ne $($TestResult.Errors)) {
$TestCase.optional = $false
$TestCase.message = "$($TestResult.Errors.Exception.Message.Replace('"', '\"')) in template file $($TestResult.file.name)"
}
else {
$TestCase.optional = $true
$TestCase.message = "Unknown error in template file " + $TestResult.file.name
}

$TestCases += $TestCase
}

End {
# Convert the array of hashtables to JSON
$JSON = $TestCases | ConvertTo-Json

# Print the JSON string to the console
Write-Output $JSON
}
}
9 changes: 8 additions & 1 deletion arm-ttk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ This will run the full suite of applicable tests on your template. To run a spe
You can also skip tests any tests:

Test-AzTemplate -TemplatePath $thePathToYourTemplate -Skip apiVersions-Should-Be-Recent
# This will exclude the tests indicated by the -Skip parameter from the test run and results
# This will exclude the tests indicated by the -Skip parameter from the test run and results

You can also change the output format to get detailed test result descriptions in JSON:

Test-AzTemplate -TemplatePath $thePathToYourTemplate | Format-Json
# This will output the test results from Test-AzTemplate to the console in JSON format
Test-AzMarketplacePackage -TemplatePath $thePathToYourTemplate | Format-Json
# This will output the test results from AzMarketplacePackage to the console in JSON format

## Running Tests on Linux

Expand Down

0 comments on commit c880736

Please sign in to comment.