Skip to content

Commit e973047

Browse files
bergmeisterJamesWTruher
authored andcommitted
Add -EnableExit switch to Invoke-ScriptAnalyzer for exit and return exit code for CI purposes (#842)
* Add -EnableExit switch * fix yaml in markdown file * fix LibraryUsage.tests.ps1 for -EnableExit switch * fix LibraryUsage.tests.ps1 to return results * add test for -EnableExit switch * Fix test by waiting for the powershell process to finish and improve it to hide the window * Add -EnableExit switch to Invoke-ScriptAnalyzer cmdlet documentation in README.md * Call powerShell directly to avoid using unsupported (in pwsh) WindowStyle parameter
1 parent cfea14f commit e973047

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ public SwitchParameter Fix
192192
}
193193
private bool fix;
194194

195+
/// <summary>
196+
/// Sets the exit code to the number of warnings for usage in CI.
197+
/// </summary>
198+
[Parameter(Mandatory = false)]
199+
public SwitchParameter EnableExit
200+
{
201+
get { return enableExit; }
202+
set { enableExit = value; }
203+
}
204+
private bool enableExit;
205+
195206
/// <summary>
196207
/// Returns path to the file that contains user profile or hash table for ScriptAnalyzer
197208
/// </summary>
@@ -418,6 +429,11 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
418429
logger.LogObject(diagnostic, this);
419430
}
420431
}
432+
433+
if (EnableExit.IsPresent)
434+
{
435+
this.Host.SetShouldExit(diagnosticRecords.Count());
436+
}
421437
}
422438

423439
private void ProcessPath()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Usage
5151
``` PowerShell
5252
Get-ScriptAnalyzerRule [-CustomizedRulePath <string[]>] [-Name <string[]>] [<CommonParameters>] [-Severity <string[]>]
5353
54-
Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule <string[]>] [-Severity <string[]>] [-Recurse] [-Fix] [<CommonParameters>]
54+
Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule <string[]>] [-Severity <string[]>] [-Recurse] [-EnableExit] [-Fix] [<CommonParameters>]
5555
```
5656

5757
[Back to ToC](#table-of-contents)

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,10 @@ Describe "Test -Fix Switch" {
497497
$actualScriptContentAfterFix | Should Be $expectedScriptContentAfterFix
498498
}
499499
}
500+
501+
Describe "Test -EnableExit Switch" {
502+
It "Returns exit code equivalent to number of warnings" {
503+
powershell -Command 'Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit'
504+
$LASTEXITCODE | Should Be 1
505+
}
506+
}

Tests/Engine/LibraryUsage.tests.ps1

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ function Invoke-ScriptAnalyzer {
5151
[switch] $SuppressedOnly,
5252

5353
[Parameter(Mandatory = $false)]
54-
[switch] $Fix
54+
[switch] $Fix,
55+
56+
[Parameter(Mandatory = $false)]
57+
[switch] $EnableExit
5558
)
5659

5760
if ($null -eq $CustomRulePath)
@@ -77,16 +80,28 @@ function Invoke-ScriptAnalyzer {
7780
$SuppressedOnly.IsPresent
7881
);
7982

80-
if ($PSCmdlet.ParameterSetName -eq "File") {
81-
$supportsShouldProcessFunc = [Func[string, string, bool]]{ return $PSCmdlet.Shouldprocess }
82-
if ($Fix.IsPresent)
83-
{
84-
return $scriptAnalyzer.AnalyzeAndFixPath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
85-
}
86-
return $scriptAnalyzer.AnalyzePath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
83+
if ($PSCmdlet.ParameterSetName -eq "File")
84+
{
85+
$supportsShouldProcessFunc = [Func[string, string, bool]] { return $PSCmdlet.Shouldprocess }
86+
if ($Fix.IsPresent)
87+
{
88+
$results = $scriptAnalyzer.AnalyzeAndFixPath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
89+
}
90+
else
91+
{
92+
$results = $scriptAnalyzer.AnalyzePath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
93+
}
8794
}
88-
else {
89-
return $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition);
95+
else
96+
{
97+
$results = $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition);
98+
}
99+
100+
$results
101+
102+
if ($EnableExit.IsPresent -and $null -ne $results)
103+
{
104+
exit $results.Count
90105
}
91106
}
92107

docs/markdown/Invoke-ScriptAnalyzer.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Evaluates a script or module based on selected best practice rules
1212
### UNNAMED_PARAMETER_SET_1
1313
```
1414
Invoke-ScriptAnalyzer [-Path] <String> [-CustomRulePath <String>] [-RecurseCustomRulePath]
15-
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-Fix]
15+
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-Fix] [-EnableExit]
1616
[-Settings <String>]
1717
```
1818

1919
### UNNAMED_PARAMETER_SET_2
2020
```
2121
Invoke-ScriptAnalyzer [-ScriptDefinition] <String> [-CustomRulePath <String>] [-RecurseCustomRulePath]
22-
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly]
22+
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-EnableExit]
2323
[-Settings <String>]
2424
```
2525

@@ -48,6 +48,7 @@ To get rules that were suppressed, run
4848
Invoke-ScriptAnalyzer with the SuppressedOnly parameter.
4949
For instructions on suppressing a rule, see the description of
5050
the SuppressedOnly parameter.
51+
For usage in CI systems, the -EnableExit exits the shell with an exit code equal to the number of error records.
5152

5253
The PSScriptAnalyzer module tests the Windows PowerShell code in a script, module, or DSC resource to determine
5354
whether, and to what extent, it fulfils best practice standards.
@@ -416,6 +417,21 @@ Accept pipeline input: False
416417
Accept wildcard characters: False
417418
```
418419
420+
### -EnableExit
421+
Exits PowerShell and returns an exit code equal to the number of error records. This can be useful in CI systems.
422+
423+
```yaml
424+
Type: SwitchParameter
425+
Parameter Sets: (All)
426+
Aliases:
427+
428+
Required: False
429+
Position: Named
430+
Default value: False
431+
Accept pipeline input: False
432+
Accept wildcard characters: False
433+
```
434+
419435
### -Settings
420436
File path that contains user profile or hash table for ScriptAnalyzer
421437

0 commit comments

Comments
 (0)