-
Notifications
You must be signed in to change notification settings - Fork 62
/
ConvertTo-MtMaesterResults.ps1
126 lines (108 loc) · 4.47 KB
/
ConvertTo-MtMaesterResults.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<#
.SYNOPSIS
Converts Pester results to the Maester test results format which includes additional information.
#>
function ConvertTo-MtMaesterResult {
[CmdletBinding()]
param(
# The Pester test results returned from Invoke-Pester -PassThru
[Parameter(Mandatory = $true)]
[psobject] $PesterResults
)
function GetTenantName() {
$org = Invoke-MtGraphRequest -RelativeUri 'organization'
return $org.DisplayName
}
function GetTestsSorted() {
# Show passed and failed tests first by name then show not run tests
$activeTests = $PesterResults.Tests | Where-Object { $_.Result -eq 'Passed' -or $_.Result -eq 'Failed' } | Sort-Object -Property Name
$inactiveTests = $PesterResults.Tests | Where-Object { $_.Result -ne 'Passed' -and $_.Result -ne 'Failed' } | Sort-Object -Property Name
# Convert to array and add, if not when only one object is returned it doesn't create an array with all items.
return @($activeTests) + @($inactiveTests)
}
function GetFormattedDate($date) {
if(!$IsCoreCLR) { # Prevent 5.1 date format to json issue
return $date.ToString("o")
}
else {
return $date
}
}
function GetMaesterLatestVersion() {
if (Get-Command 'Find-Module' -ErrorAction SilentlyContinue) {
return (Find-Module -Name Maester).Version
}
return 'Unknown'
}
$mgContext = Get-MgContext
$tenantId = $mgContext.TenantId
$tenantName = GetTenantName
$account = $mgContext.Account
$currentVersion = ((Get-Module -Name Maester).Version | Select-Object -Last 1).ToString()
$latestVersion = GetMaesterLatestVersion
$mtTests = @()
$sortedTests = GetTestsSorted
foreach ($test in $sortedTests) {
$name = $test.ExpandedName
$helpUrl = ''
$start = $name.IndexOf("See https")
# Get the Help Url from the message and the ID
if ($start -gt 0) {
$helpUrl = $name.Substring($start + 4).Trim() #Strip away the "See https://maester.dev" part
$name = $name.Substring(0, $start).Trim() #Strip away the "See https://maester.dev" part
}
$mtTestInfo = [PSCustomObject]@{
Name = $name
HelpUrl = $helpUrl
Tag = $test.Block.Tag
Result = $test.Result
ScriptBlock = $test.ScriptBlock.ToString()
ScriptBlockFile = $test.ScriptBlock.File
ErrorRecord = $test.ErrorRecord
Block = $test.Block.ExpandedName
ResultDetail = $__MtSession.TestResultDetail[$test.ExpandedName]
}
$mtTests += $mtTestInfo
}
$mtBlocks = @()
foreach ($container in $PesterResults.Containers) {
foreach ($block in $container.Blocks) {
$mtBlockInfo = $mtBlocks | Where-Object { $_.Name -eq $block.Name }
if ($null -eq $mtBlockInfo) {
$mtBlockInfo = [PSCustomObject]@{
Name = $block.Name
Result = $block.Result
FailedCount = $block.FailedCount
PassedCount = $block.PassedCount
SkippedCount = $block.SkippedCount
NotRunCount = $block.NotRunCount
TotalCount = $block.TotalCount
Tag = $block.Tag
}
$mtBlocks += $mtBlockInfo
} else {
$mtBlockInfo.FailedCount += $block.FailedCount
$mtBlockInfo.PassedCount += $block.PassedCount
$mtBlockInfo.SkippedCount += $block.SkippedCount
$mtBlockInfo.NotRunCount += $block.NotRunCount
$mtBlockInfo.TotalCount += $block.TotalCount
}
}
}
$mtTestResults = [PSCustomObject]@{
Result = $PesterResults.Result
FailedCount = $PesterResults.FailedCount
PassedCount = $PesterResults.PassedCount
SkippedCount = $PesterResults.SkippedCount
TotalCount = $PesterResults.TotalCount
ExecutedAt = GetFormattedDate($PesterResults.ExecutedAt)
TenantId = $tenantId
TenantName = $tenantName
Account = $account
CurrentVersion = $currentVersion
LatestVersion = $latestVersion
Tests = $mtTests
Blocks = $mtBlocks
}
return $mtTestResults
}