-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBuildPreparation.ps1
101 lines (84 loc) · 3.43 KB
/
BuildPreparation.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
#parts of the script are taken from the VSTeam module https://github.com/RazorSPoint/vsteam
[CmdletBinding(DefaultParameterSetName = "All")]
param(
#output path of the build module
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[string]$outputDir = './dist',
# run the scripts with the PS script analyzer
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$analyzeScript,
# runs the unit tests
[Parameter(ParameterSetName = "UnitTest", Mandatory = $true)]
[Parameter(ParameterSetName = "All")]
[switch]$runTests,
# does all the preparation for the pre-build
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$prepareExtension,
# can be used to filter the unit test parts that should be run
# see also: https://github.com/pester/Pester/wiki/Invoke%E2%80%90Pester#testname-alias-name
[Parameter(ParameterSetName = "UnitTest")]
[string]$testName,
# outputs the code coverage
[Parameter(ParameterSetName = "UnitTest")]
[switch]$codeCoverage
)
$sourcePath = "./src"
if ([System.IO.Path]::IsPathRooted($outputDir)) {
$output = $outputDir
}
else {
$output = Join-Path (Get-Location) $outputDir
}
$output = [System.IO.Path]::GetFullPath($output)
if ($prepareExtension) {
. ./tools/PrepareExtension.ps1 -sourcePath $sourcePath -outputDir $outputDir
Write-Output "Publish complete to $outputDir"
}
# run the unit tests with Pester
if ($runTests.IsPresent) {
Write-Output "Starting unit tests..."
if ($null -eq $(Get-Module -Name Pester)) {
Install-Module -Name Pester -Repository PSGallery -Force -Scope CurrentUser -AllowClobber -SkipPublisherCheck
}
$pesterArgs = @{
Script = '.\.Tests\unit'
OutputFile = 'test-results.xml'
OutputFormat = 'NUnitXml'
Show = 'Fails'
Strict = $true
}
if ($codeCoverage.IsPresent) {
$pesterArgs.CodeCoverage = "$outputDir\*.ps1"
$pesterArgs.CodeCoverageOutputFile = "coverage.xml"
$pesterArgs.CodeCoverageOutputFileFormat = 'JaCoCo'
}
else {
$pesterArgs.PassThru = $true
}
if ($testName) {
$pesterArgs.TestName = $testName
#passthru must be activated according to Pester docs
$pesterArgs.PassThru = $true
}
Invoke-Pester @pesterArgs
}
# Run this last so the results can be seen even if tests were also run
# if not the results scroll off and my not be in the buffer.
# run PSScriptAnalyzer
if ($analyzeScript.IsPresent) {
Write-Output "Starting static code analysis..."
Write-Output "-----------"
if ($null -eq $(Get-Module -Name PSScriptAnalyzer)) {
Install-Module -Name PSScriptAnalyzer -Repository PSGallery -Force -Scope CurrentUser
}
$r = Invoke-ScriptAnalyzer -Path "$output/AzureInitiative/AzureInitiativeV1/ps_modules/CommonScripts" -Recurse
$r += Invoke-ScriptAnalyzer -Path "$output/AzureInitiative/AzureInitiativeV1"
$r += Invoke-ScriptAnalyzer -Path "$output/AzurePolicy/AzurePolicyV1"
$r += Invoke-ScriptAnalyzer -Path "$output/AzurePolicy/AzurePolicyV2"
$r | ForEach-Object { Write-Host "##vso[task.logissue type=$($_.Severity);sourcepath=$($_.ScriptPath);linenumber=$($_.Line);columnnumber=$($_.Column);]$($_.Message)" }
Write-Output "-----------"
Write-Output "Static code analysis complete."
}