-
Notifications
You must be signed in to change notification settings - Fork 0
/
psakeFile.ps1
104 lines (81 loc) · 3.51 KB
/
psakeFile.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
# We are leveraging concepts from an open-source project on GitHub - /psake/PowerShellBuild (https://github.com/psake/PowerShellBuild)
# All kudos go to the author/contributors of that project. In some cases, we are flat out copying logic
properties {
# BH* env variables are prefilled by the BuildHelpers module - step executed in build.ps1 script which invokes this script.
$settings = . ([IO.Path]::Combine($PSScriptRoot, 'build.settings.ps1'))
}
task default -depends Init
task Init {
Write-Host "`nEnvironment variables:" -ForegroundColor Yellow
(Get-Item ENV:BH*).Foreach({
'{0,-20}{1}' -f $_.name, $_.value
})
Write-Host "`nBuild-Time parameters:" -ForegroundColor Yellow
$settings.getenumerator().foreach({
'{0,-20}{1}' -f $_.key, $_.value
})
} -description 'Initialise Build Environment'
task Clean -depends Init {
if (Test-Path $settings.buildOutput) {
Remove-Item -Path $settings.buildOutput -Recurse -Force -Verbose:$false
}
} -description 'Clear the Output folder'
task Stage -depends Clean {
$stageParams = @{
ModuleSource = $settings.moduleSourcePath
OutputPath = $settings.buildOutput
}
. ([IO.Path]::Combine($projectRoot, '.helpers\localPublishModule.ps1')) @stageParams
# if running in the context of a pipeline, get the GitVer value, otherwise, leave as is (local run)
Write-Host ('Setting module version. Detecting run context...')
Write-Host ('Pipeline Context: [{0}]. Module version will be set to [{1}].' -f $pipelineRun,$settings.moduleVersion)
$versionParams = @{
manifestPath = ('{0}\{1}\{2}.psd1' -f $settings.buildOutput, $settings.moduleName, $settings.moduleName)
version = $settings.moduleVersion
}
# set the version
. ([IO.Path]::Combine($projectRoot, '.helpers\setModuleVersion.ps1')) @versionParams
} -description 'Stage the module content before building'
$pesterPreReqs = {
$result = $true
if (-not $settings.runPesterTests) {
Write-Warning 'Pester testing is not enabled.'
$result = $false
}
if (-not (Get-Module -Name Pester -ListAvailable)) {
Write-Warning 'Pester module is not installed'
$result = $false
}
if (-not (Test-Path -Path $settings.testsDir)) {
Write-Warning "Test directory [$($settings.testsDir)] not found"
$result = $false
}
return $result
}
task Pester -depends Init -precondition $pesterPreReqs{
try{
Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
}catch{
$ex = $_
Write-Error "Could not load Pester module:`n"
throw $ex
}
$configuration = [PesterConfiguration]::Default
$configuration.Output.Verbosity = 'Detailed'
$configuration.Run.Path = $settings.testsDir
$configuration.Run.PassThru = $true
$configuration.TestResult.Enabled = -not [string]::IsNullOrEmpty($settings.testsOutputFile)
$configuration.TestResult.OutputPath = $settings.testsOutputFile
$configuration.TestResult.OutputFormat = $settings.testsOutputFormat
# execute tests
$testResults = Invoke-Pester -Configuration $configuration
# check for failures
if ($testResults.FailedCount -gt 0){
if ($pipelineRun){
Write-Host "##[error]One or more Pester Tests failed"
}else{
Write-Error "One or more Pester tests failed"
}
throw '##[error]Testing Failed'
}
} -description 'Execute Pester tests'