Skip to content

Add -EnableExit switch to Invoke-ScriptAnalyzer for exit and return exit code for CI purposes #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 21, 2018
16 changes: 16 additions & 0 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ public SwitchParameter Fix
}
private bool fix;

/// <summary>
/// Sets the exit code to the number of warnings for usage in CI.
/// </summary>
[Parameter(Mandatory = false)]
public SwitchParameter EnableExit
{
get { return enableExit; }
set { enableExit = value; }
}
private bool enableExit;

/// <summary>
/// Returns path to the file that contains user profile or hash table for ScriptAnalyzer
/// </summary>
Expand Down Expand Up @@ -418,6 +429,11 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
logger.LogObject(diagnostic, this);
}
}

if (EnableExit.IsPresent)
{
this.Host.SetShouldExit(diagnosticRecords.Count());
}
}

private void ProcessPath()
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Usage
``` PowerShell
Get-ScriptAnalyzerRule [-CustomizedRulePath <string[]>] [-Name <string[]>] [<CommonParameters>] [-Severity <string[]>]

Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule <string[]>] [-Severity <string[]>] [-Recurse] [-Fix] [<CommonParameters>]
Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule <string[]>] [-Severity <string[]>] [-Recurse] [-EnableExit] [-Fix] [<CommonParameters>]
```

[Back to ToC](#table-of-contents)
Expand Down
7 changes: 7 additions & 0 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,10 @@ Describe "Test -Fix Switch" {
$actualScriptContentAfterFix | Should Be $expectedScriptContentAfterFix
}
}

Describe "Test -EnableExit Switch" {
It "Returns exit code equivalent to number of warnings" {
powershell -Command 'Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit'
$LASTEXITCODE | Should Be 1
}
}
35 changes: 25 additions & 10 deletions Tests/Engine/LibraryUsage.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ function Invoke-ScriptAnalyzer {
[switch] $SuppressedOnly,

[Parameter(Mandatory = $false)]
[switch] $Fix
[switch] $Fix,

[Parameter(Mandatory = $false)]
[switch] $EnableExit
)

if ($null -eq $CustomRulePath)
Expand All @@ -77,16 +80,28 @@ function Invoke-ScriptAnalyzer {
$SuppressedOnly.IsPresent
);

if ($PSCmdlet.ParameterSetName -eq "File") {
$supportsShouldProcessFunc = [Func[string, string, bool]]{ return $PSCmdlet.Shouldprocess }
if ($Fix.IsPresent)
{
return $scriptAnalyzer.AnalyzeAndFixPath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
}
return $scriptAnalyzer.AnalyzePath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
if ($PSCmdlet.ParameterSetName -eq "File")
{
$supportsShouldProcessFunc = [Func[string, string, bool]] { return $PSCmdlet.Shouldprocess }
if ($Fix.IsPresent)
{
$results = $scriptAnalyzer.AnalyzeAndFixPath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
}
else
{
$results = $scriptAnalyzer.AnalyzePath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent);
}
}
else {
return $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition);
else
{
$results = $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition);
}

$results

if ($EnableExit.IsPresent -and $null -ne $results)
{
exit $results.Count
}
}

Expand Down
20 changes: 18 additions & 2 deletions docs/markdown/Invoke-ScriptAnalyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Evaluates a script or module based on selected best practice rules
### UNNAMED_PARAMETER_SET_1
```
Invoke-ScriptAnalyzer [-Path] <String> [-CustomRulePath <String>] [-RecurseCustomRulePath]
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-Fix]
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-Fix] [-EnableExit]
[-Settings <String>]
```

### UNNAMED_PARAMETER_SET_2
```
Invoke-ScriptAnalyzer [-ScriptDefinition] <String> [-CustomRulePath <String>] [-RecurseCustomRulePath]
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly]
[-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse] [-SuppressedOnly] [-EnableExit]
[-Settings <String>]
```

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

The PSScriptAnalyzer module tests the Windows PowerShell code in a script, module, or DSC resource to determine
whether, and to what extent, it fulfils best practice standards.
Expand Down Expand Up @@ -416,6 +417,21 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -EnableExit
Exits PowerShell and returns an exit code equal to the number of error records. This can be useful in CI systems.

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```

### -Settings
File path that contains user profile or hash table for ScriptAnalyzer

Expand Down