-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExport-NUnitXml.psm1
More file actions
113 lines (98 loc) · 4.79 KB
/
Copy pathExport-NUnitXml.psm1
File metadata and controls
113 lines (98 loc) · 4.79 KB
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
Function Export-NUnitXml {
<#
.SYNOPSIS
Takes results from PSScriptAnalyzer and exports them as a Pester test results file (NUnitXml format).
.DESCRIPTION
Takes results from PSScriptAnalyzer and exports them as a Pester test results file (NUnit XML schema).
Because the generated file in NUnit-compatible, it can be consumed and published by most continuous integration tools.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory, Position=0)]
[AllowNull()]
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]]$ScriptAnalyzerResult,
[Parameter(Mandatory, Position=1)]
[string]$Path
)
$TotalNumber = If ($ScriptAnalyzerResult) { $ScriptAnalyzerResult.Count -as [string] } Else { '1' }
$FailedNumber = If ($ScriptAnalyzerResult) { $ScriptAnalyzerResult.Count -as [string] } Else { '0' }
$Now = Get-Date
$FormattedDate = Get-Date $Now -Format 'yyyy-MM-dd'
$FormattedTime = Get-Date $Now -Format 'T'
$User = $env:USERNAME
$MachineName = $env:COMPUTERNAME
$Cwd = $pwd.Path
$UserDomain = $env:USERDOMAIN
$OS = Get-CimInstance -ClassName Win32_OperatingSystem
$Platform = $OS.Caption
$OSVersion = $OS.Version
$ClrVersion = 'Unknown'
$CurrentCulture = (Get-Culture).Name
$UICulture = (Get-UICulture).Name
# This has been deprecated with PS Core and is only kept for compatbility
# See https://github.com/PowerShell/PowerShell/issues/1395
if ($PSVersionTable.CLRVersion) {
$ClrVersion = $PSVersionTable.CLRVersion.ToString()
}
Switch ($ScriptAnalyzerResult) {
$Null { $TestResult = 'Success'; $TestSuccess = 'True'; Break}
Default { $TestResult = 'Failure'; $TestSuccess = 'False'}
}
$Header = @"
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<test-results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nunit_schema_2.5.xsd" name="PSScriptAnalyzer" total="$TotalNumber" errors="0" failures="$FailedNumber" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="$FormattedDate" time="$FormattedTime">
<environment user="$User" machine-name="$MachineName" cwd="$Cwd" user-domain="$UserDomain" platform="$Platform" nunit-version="2.5.8.0" os-version="$OSVersion" clr-version="$ClrVersion" />
<culture-info current-culture="$CurrentCulture" current-uiculture="$UICulture" />
<test-suite type="Powershell" name="PSScriptAnalyzer" executed="True" result="$TestResult" success="$TestSuccess" time="0.0" asserts="0">
<results>
<test-suite type="TestFixture" name="PSScriptAnalyzer" executed="True" result="$TestResult" success="$TestSuccess" time="0.0" asserts="0" description="PSScriptAnalyzer">
<results>`n
"@
$Footer = @"
</results>
</test-suite>
</results>
</test-suite>
</test-results>
"@
If ( -not($ScriptAnalyzerResult) ) {
$TestDescription = 'All PowerShell files pass the specified PSScriptAnalyzer rules'
$TestName = "PSScriptAnalyzer.{0}" -f $TestDescription
$Body = @"
<test-case description="$TestDescription" name="$TestName" time="0.0" asserts="0" success="True" result="Success" executed="True" />`n
"@
}
Else { # $ScriptAnalyzerResult is not null
$Body = [string]::Empty
Foreach ( $Result in $ScriptAnalyzerResult ) {
$TestDescription = "Rule name : $($Result.RuleName)"
$TestName = "PSScriptAnalyzer.{0} - {1} - Line {2}" -f $TestDescription, $($Result.ScriptName), $($Result.Line.ToString())
# Need to Escape these otherwise we can end up with an invalid XML if the Stacktrace has non XML friendly chars like &, etc
$Line = [System.Security.SecurityElement]::Escape($Result.Line)
$ScriptPath = [System.Security.SecurityElement]::Escape($Result.ScriptPath)
$Text = [System.Security.SecurityElement]::Escape($Result.Extent.Text)
$Severity = [System.Security.SecurityElement]::Escape($Result.Severity)
$TestCase = @"
<test-case description="$TestDescription" name="$TestName" time="0.0" asserts="0" success="False" result="Failure" executed="True">
<failure>
<message>$($Result.Message)</message>
<stack-trace>at line: $($Line) in $($ScriptPath)
$($Line): $($Text)
Rule severity : $($Severity)
</stack-trace>
</failure>
</test-case>`n
"@
$Body += $TestCase
}
}
$OutputXml = $Header + $Body + $Footer
# Checking our output is a well formed XML document
Try {
$XmlCheck = [xml]$OutputXml
}
Catch {
Throw "There was an problem when attempting to cast the output to XML : $($_.Exception.Message)"
}
$OutputXml | Out-File -FilePath $Path -Encoding utf8 -Force
}