-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
267 lines (214 loc) · 9.38 KB
/
Copy pathbuild.ps1
File metadata and controls
267 lines (214 loc) · 9.38 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#Requires -Version 7.4
<#
.SYNOPSIS
Build, test and static-analysis entry point for the AppxBackup module.
.DESCRIPTION
A single command that a contributor and CI both run, so "it passes locally"
and "it passes in CI" mean the same thing.
Tasks:
Test - run the Pester suite under tests/
Analyze - run PSScriptAnalyzer using PSScriptAnalyzerSettings.psd1
Build - verify the manifest and that the module imports cleanly
All - Build, then Analyze, then Test (default)
.PARAMETER Task
Which task to run. Defaults to All.
.PARAMETER CI
Emit NUnit test results and fail the process on any test failure or on any
analyzer finding at or above -FailOn. Sets the exit code for the build system.
.PARAMETER FailOn
Lowest analyzer severity that should fail the build. Defaults to Warning.
Findings below this level are reported but do not fail.
.PARAMETER OutputPath
Directory for test and analysis artefacts. Defaults to ./out.
.PARAMETER IncludeE2E
Also run the end-to-end tests tagged 'E2E'. These exercise the real Windows
SDK MakeAppx and are excluded from the default run because they require the
SDK on the machine. Without this switch those tests are skipped entirely.
.EXAMPLE
.\build.ps1
Runs the full pipeline locally with human-readable output.
.EXAMPLE
.\build.ps1 -Task Test -CI
Runs the tests and writes out/TestResults.xml, exiting non-zero on failure.
.EXAMPLE
.\build.ps1 -Task Test -IncludeE2E
Runs the unit suite plus the end-to-end packaging tests (needs the Windows SDK).
#>
[CmdletBinding()]
param(
[ValidateSet('All', 'Build', 'Test', 'Analyze')]
[string]$Task = 'All',
[switch]$CI,
[switch]$IncludeE2E,
[ValidateSet('Error', 'Warning', 'Information', 'None')]
[string]$FailOn = 'Warning',
[string]$OutputPath = (Join-Path $PSScriptRoot 'out')
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$script:ModuleRoot = $PSScriptRoot
$script:ManifestPath = Join-Path $script:ModuleRoot 'AppxBackup.psd1'
$script:Failures = [System.Collections.Generic.List[string]]::new()
# Promote the parameters the task functions read to explicit script scope. They would
# resolve dynamically anyway, but naming the scope makes the data flow obvious and
# keeps static analysis able to see the usage.
$script:IsCI = [bool]$CI
$script:FailOnSeverity = $FailOn
$script:ArtifactPath = $OutputPath
$script:IncludeE2ETests = [bool]$IncludeE2E
# Minimum tool versions. Pester 5.5 is the floor for the test syntax used in tests/,
# which is written to also run unchanged on Pester 6.
$script:MinimumPester = [version]'5.5.0'
$script:MinimumAnalyzer = [version]'1.21.0'
function Write-Section {
param([Parameter(Mandatory)][string]$Name)
Write-Host ''
Write-Host "=== $Name ===" -ForegroundColor Cyan
}
function Import-BuildDependency {
<#
Loads a tool at or above the required version, and explains how to get it
rather than failing with a bare "module not found".
#>
param(
[Parameter(Mandatory)][string]$Name,
[Parameter(Mandatory)][version]$MinimumVersion
)
$candidate = Get-Module -ListAvailable -Name $Name |
Where-Object { $_.Version -ge $MinimumVersion } |
Sort-Object Version -Descending |
Select-Object -First 1
if (-not $candidate) {
throw "$Name $MinimumVersion or later is required. Install it with: Install-Module $Name -MinimumVersion $MinimumVersion -Scope CurrentUser -Force"
}
Import-Module $candidate.Path -Force -ErrorAction Stop
Write-Host "Using $Name $($candidate.Version)" -ForegroundColor DarkGray
}
function Invoke-BuildTask {
Write-Section 'Build'
Write-Host 'Validating module manifest...' -ForegroundColor Gray
$manifest = Test-ModuleManifest -Path $script:ManifestPath -ErrorAction Stop
Write-Host " $($manifest.Name) $($manifest.Version)" -ForegroundColor Green
Write-Host 'Importing module...' -ForegroundColor Gray
try {
Import-Module $script:ManifestPath -Force -ErrorAction Stop
$commands = @(Get-Command -Module AppxBackup)
Write-Host " Imported $($commands.Count) commands" -ForegroundColor Green
}
finally {
Remove-Module AppxBackup -Force -ErrorAction SilentlyContinue
}
Write-Host 'Checking manifest FileList against disk...' -ForegroundColor Gray
$data = Import-PowerShellDataFile -LiteralPath $script:ManifestPath
$missing = @($data.FileList | Where-Object { -not (Test-Path -LiteralPath (Join-Path $script:ModuleRoot $_)) })
if ($missing.Count -gt 0) {
$missing | ForEach-Object { Write-Host " Missing: $_" -ForegroundColor Red }
$script:Failures.Add("Manifest FileList references $($missing.Count) file(s) that do not exist")
}
else {
Write-Host ' FileList is consistent' -ForegroundColor Green
}
}
function Invoke-AnalyzeTask {
Write-Section 'Analyze'
Import-BuildDependency -Name 'PSScriptAnalyzer' -MinimumVersion $script:MinimumAnalyzer
$settings = Join-Path $script:ModuleRoot 'PSScriptAnalyzerSettings.psd1'
# PSScriptAnalyzer 1.25 raises a non-terminating NullReferenceException while
# inspecting AppxBackup.psm1 when it is reached through an absolute path. The
# analysis still completes and returns the identical finding set, but this script
# runs with $ErrorActionPreference = 'Stop', which would promote that internal
# error into a build failure. Collect analyzer errors instead and report them
# separately from the findings so a genuine tool failure is still visible.
$analyzerErrors = @()
$findings = @(Invoke-ScriptAnalyzer -Path $script:ModuleRoot -Recurse -Settings $settings `
-ErrorVariable analyzerErrors -ErrorAction SilentlyContinue)
foreach ($analyzerError in $analyzerErrors) {
Write-Host " Analyzer could not fully inspect '$($analyzerError.TargetObject)': $($analyzerError.Exception.Message)" -ForegroundColor DarkGray
}
if ($findings.Count -eq 0) {
Write-Host 'No findings' -ForegroundColor Green
return
}
$findings |
Group-Object Severity |
Sort-Object Name |
ForEach-Object { Write-Host (" {0,-12} {1}" -f $_.Name, $_.Count) -ForegroundColor Yellow }
$findings |
Sort-Object Severity, ScriptName, Line |
ForEach-Object {
Write-Host (" [{0}] {1}:{2} {3}" -f $_.Severity, $_.ScriptName, $_.Line, $_.RuleName) -ForegroundColor DarkYellow
}
if ($script:IsCI) {
$null = New-Item -ItemType Directory -Path $script:ArtifactPath -Force
$findings | Export-Csv -Path (Join-Path $script:ArtifactPath 'AnalyzerResults.csv') -NoTypeInformation
}
if ($script:FailOnSeverity -ne 'None') {
# Severity ascends Information -> Warning -> Error, so anything at or above
# the requested level is blocking.
$threshold = switch ($script:FailOnSeverity) {
'Information' { 0 }
'Warning' { 1 }
'Error' { 2 }
}
$blocking = @($findings | Where-Object { [int]$_.Severity -ge $threshold })
if ($blocking.Count -gt 0) {
$script:Failures.Add("PSScriptAnalyzer reported $($blocking.Count) finding(s) at or above $script:FailOnSeverity")
}
}
}
function Invoke-TestTask {
Write-Section 'Test'
Import-BuildDependency -Name 'Pester' -MinimumVersion $script:MinimumPester
$testPath = Join-Path $script:ModuleRoot 'tests'
if (-not (Test-Path -LiteralPath $testPath)) {
throw "Test directory not found: $testPath"
}
$config = New-PesterConfiguration
$config.Run.Path = $testPath
$config.Run.PassThru = $true
$config.Output.Verbosity = if ($script:IsCI) { 'Detailed' } else { 'Normal' }
# End-to-end tests (real MakeAppx) run only when explicitly requested.
if (-not $script:IncludeE2ETests) {
$config.Filter.ExcludeTag = 'E2E'
}
if ($script:IsCI) {
$null = New-Item -ItemType Directory -Path $script:ArtifactPath -Force
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = 'NUnitXml'
$config.TestResult.OutputPath = Join-Path $script:ArtifactPath 'TestResults.xml'
}
$result = Invoke-Pester -Configuration $config
Write-Host ''
Write-Host ("Passed {0} | Failed {1} | Skipped {2} | {3:N1}s" -f
$result.PassedCount, $result.FailedCount, $result.SkippedCount, $result.Duration.TotalSeconds) -ForegroundColor Gray
if ($result.FailedCount -gt 0) {
$script:Failures.Add("$($result.FailedCount) test(s) failed")
}
}
try {
Write-Host "AppxBackup build - task '$Task' on PowerShell $($PSVersionTable.PSVersion)" -ForegroundColor White
switch ($Task) {
'Build' { Invoke-BuildTask }
'Analyze' { Invoke-AnalyzeTask }
'Test' { Invoke-TestTask }
'All' {
Invoke-BuildTask
Invoke-AnalyzeTask
Invoke-TestTask
}
}
Write-Section 'Summary'
if ($script:Failures.Count -gt 0) {
$script:Failures | ForEach-Object { Write-Host "FAIL: $_" -ForegroundColor Red }
if ($script:IsCI) { exit 1 }
throw "Build failed with $($script:Failures.Count) problem(s)"
}
Write-Host 'Success' -ForegroundColor Green
if ($script:IsCI) { exit 0 }
}
catch {
Write-Host ''
Write-Host "Build error: $($_.Exception.Message)" -ForegroundColor Red
if ($script:IsCI) { exit 1 }
throw
}