-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathScriptAnalyzer-job.yml
103 lines (89 loc) · 3.85 KB
/
ScriptAnalyzer-job.yml
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
parameters:
scriptAnalyzerVersion: ''
dependsOn: []
artifactName: ''
artifactDownloadDirectory: $(Build.ArtifactStagingDirectory)
codeDirectory: $(Build.ArtifactStagingDirectory)
requiredModulesPath: RequiredModules.psd1
modulesToImport: []
customRulePath: ''
excludeRules: ["PSShouldProcess", "PSUseShouldProcessForStateChangingFunctions", "PSAvoidUsingWriteHost"]
pool:
vmImage: 'windows-2019'
jobs:
- job: ScriptAnalyzer
dependsOn: ${{ parameters.dependsOn }}
workspace:
clean: outputs
pool: ${{ parameters.pool }}
steps:
- checkout: self # re-checkout, because we need the RequiredModules - what can we do?
fetchDepth: 1 # we only need the current copy
lfs: true # this must always stay true or it will fail if it's ever true
- template: Install-RequiredModule-step.yml
parameters:
path: $(Build.SourcesDirectory)/${{ parameters.requiredModulesPath }}
# If we depends on a build step, then we should download the artifact
- task: DownloadPipelineArtifact@2
displayName: 'Download Build Artifacts'
condition: ${{ ne('', parameters.artifactName)}}
inputs:
artifactName: ${{ parameters.artifactName }}
downloadPath: ${{ parameters.artifactDownloadDirectory }}/$(Build.DefinitionName)
- powershell: |
$Path = (New-Item $Env:TEMP\$(New-Guid)\PSScriptAnalyzer.Tests.ps1 -Type File -Force).FullName
"##vso[task.setvariable variable=TestPath]$Path"
Import-Module PsScriptAnalyzer -Passthru
Set-Content $Path '
$QMExcludeRules = @("${{ join('", "', parameters.excludeRules) }}")
$QMCustomRulePath = "${{ parameters.customRulePath }}"
$Path = "${{ parameters.codeDirectory }}"
if ($Modules = @("${{ join('", "', parameters.modulesToImport) }}")) {
Write-Verbose "Import-Module $Modules" -Verbose
Import-Module $Modules
}
Describe "PSScriptAnalyzer" {
$ScriptAnalyzer = @{
Config = @{ Path = $Path; Recurse = $true; IncludeDefaultRules = $true; ExcludeRule = $QMExcludeRules }
Rules = Get-ScriptAnalyzerRule | Where-Object RuleName -notin $QMExcludeRules
}
It "Can have custom rules" {
if ($QMCustomRulePath) {
$QMCustomRulePath | Should Exist
if ($CustomRules = Get-ScriptAnalyzerRule -CustomRulePath $QMCustomRulePath -RecurseCustomRulePath) {
$ScriptAnalyzer.Rules += $CustomRules
$ScriptAnalyzer.Config += @{
CustomRulePath = $QMCustomRulePath
RecurseCustomRulePath = $true
}
}
}
}
It "Does not throw while running Script Analyzer" {
$Config = $ScriptAnalyzer.Config
try {
$ScriptAnalyzer.Results = Invoke-ScriptAnalyzer @Config
} catch {
Write-Warning "Exception running script analyzer on $($_.TargetObject)"
Write-Warning $($_.Exception.StackTrace)
throw
}
}
forEach ($Rule in $ScriptAnalyzer.Rules.RuleName) {
It "Passes $Rule" {
if ($Failures = $ScriptAnalyzer.Results.Where( {$_.RuleName -like "*$Rule"})) {
throw ([Management.Automation.ErrorRecord]::new(
([Exception]::new(($Failures.ForEach{$_.ScriptName + ":" + $_.Line + " " + $_.Message} -join "`n"))),
"ScriptAnalyzerViolation",
"SyntaxError",
$Failures))
}
}
}
}'
displayName: 'Generate ScriptAnalyzer Test'
- template: Pester-step.yml
parameters:
testsDirectory: $(TestPath)
additionalModulePaths: ${{ parameters.artifactDownloadDirectory }}
configurationName: 'Script Analyzer'