Skip to content

Commit

Permalink
Set exit code of Invoke-ScriptAnalyzer -EnableExit to total number …
Browse files Browse the repository at this point in the history
…of diagnostics (#2055)
  • Loading branch information
MatejKafka authored Feb 10, 2025
1 parent 9dacfa8 commit 0e46667
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
20 changes: 11 additions & 9 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter

#region Private variables
List<string> processedPaths;
private int totalDiagnosticCount = 0;
#endregion // Private variables

#region Parameters
Expand Down Expand Up @@ -412,6 +413,10 @@ protected override void EndProcessing()
{
ScriptAnalyzer.Instance.CleanUp();
base.EndProcessing();

if (EnableExit) {
this.Host.SetShouldExit(totalDiagnosticCount);
}
}

protected override void StopProcessing()
Expand All @@ -426,10 +431,12 @@ protected override void StopProcessing()

private void ProcessInput()
{
WriteToOutput(RunAnalysis());
var diagnosticRecords = RunAnalysis();
WriteToOutput(diagnosticRecords);
totalDiagnosticCount += diagnosticRecords.Count;
}

private IEnumerable<DiagnosticRecord> RunAnalysis()
private List<DiagnosticRecord> RunAnalysis()
{
if (!IsFileParameterSet())
{
Expand All @@ -454,7 +461,7 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
return diagnostics;
}

private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
private void WriteToOutput(List<DiagnosticRecord> diagnosticRecords)
{
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
{
Expand Down Expand Up @@ -507,11 +514,6 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
}
}
}

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

private void ProcessPath()
Expand All @@ -535,4 +537,4 @@ private bool OverrideSwitchParam(bool paramValue, string paramName)

#endregion // Private Methods
}
}
}
7 changes: 4 additions & 3 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, Func<string,
/// <param name="scriptTokens">Parsed tokens of <paramref name="scriptDefinition"/.></param>
/// <param name="skipVariableAnalysis">Whether variable analysis can be skipped (applicable if rules do not use variable analysis APIs).</param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition, out ScriptBlockAst scriptAst, out Token[] scriptTokens, bool skipVariableAnalysis = false)
public List<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition, out ScriptBlockAst scriptAst, out Token[] scriptTokens, bool skipVariableAnalysis = false)
{
scriptAst = null;
scriptTokens = null;
Expand All @@ -1503,7 +1503,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
catch (Exception e)
{
this.outputWriter.WriteWarning(e.ToString());
return null;
return new();
}

var relevantParseErrors = RemoveTypeNotFoundParseErrors(errors, out List<DiagnosticRecord> diagnosticRecords);
Expand All @@ -1528,7 +1528,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
}

// now, analyze the script definition
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, String.Empty, skipVariableAnalysis));
diagnosticRecords.AddRange(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, null, skipVariableAnalysis));
return diagnosticRecords;
}

/// <summary>
Expand Down
24 changes: 22 additions & 2 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,29 @@ Describe "Test -EnableExit Switch" {

$pssaPath = (Get-Module PSScriptAnalyzer).Path

& $pwshExe -Command "Import-Module '$pssaPath'; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit"
& $pwshExe -NoProfile -Command "Import-Module '$pssaPath'; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit"

$LASTEXITCODE | Should -Be 1
$LASTEXITCODE | Should -Be 1
}

It "Returns exit code equivalent to number of warnings for multiple piped files" {
if ($IsCoreCLR)
{
$pwshExe = (Get-Process -Id $PID).Path
}
else
{
$pwshExe = 'powershell'
}

$pssaPath = (Get-Module PSScriptAnalyzer).Path

& $pwshExe -NoProfile {
Import-Module $Args[0]
Get-ChildItem $Args[1] | Invoke-ScriptAnalyzer -EnableExit
} -Args $pssaPath, "$PSScriptRoot\RecursionDirectoryTest"

$LASTEXITCODE | Should -Be 2
}

Describe "-ReportSummary switch" {
Expand Down

0 comments on commit 0e46667

Please sign in to comment.