Skip to content

Commit 0e46667

Browse files
authored
Set exit code of Invoke-ScriptAnalyzer -EnableExit to total number of diagnostics (#2055)
1 parent 9dacfa8 commit 0e46667

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
3434

3535
#region Private variables
3636
List<string> processedPaths;
37+
private int totalDiagnosticCount = 0;
3738
#endregion // Private variables
3839

3940
#region Parameters
@@ -412,6 +413,10 @@ protected override void EndProcessing()
412413
{
413414
ScriptAnalyzer.Instance.CleanUp();
414415
base.EndProcessing();
416+
417+
if (EnableExit) {
418+
this.Host.SetShouldExit(totalDiagnosticCount);
419+
}
415420
}
416421

417422
protected override void StopProcessing()
@@ -426,10 +431,12 @@ protected override void StopProcessing()
426431

427432
private void ProcessInput()
428433
{
429-
WriteToOutput(RunAnalysis());
434+
var diagnosticRecords = RunAnalysis();
435+
WriteToOutput(diagnosticRecords);
436+
totalDiagnosticCount += diagnosticRecords.Count;
430437
}
431438

432-
private IEnumerable<DiagnosticRecord> RunAnalysis()
439+
private List<DiagnosticRecord> RunAnalysis()
433440
{
434441
if (!IsFileParameterSet())
435442
{
@@ -454,7 +461,7 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
454461
return diagnostics;
455462
}
456463

457-
private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
464+
private void WriteToOutput(List<DiagnosticRecord> diagnosticRecords)
458465
{
459466
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
460467
{
@@ -507,11 +514,6 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
507514
}
508515
}
509516
}
510-
511-
if (EnableExit.IsPresent)
512-
{
513-
this.Host.SetShouldExit(diagnosticRecords.Count());
514-
}
515517
}
516518

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

536538
#endregion // Private Methods
537539
}
538-
}
540+
}

Engine/ScriptAnalyzer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, Func<string,
14881488
/// <param name="scriptTokens">Parsed tokens of <paramref name="scriptDefinition"/.></param>
14891489
/// <param name="skipVariableAnalysis">Whether variable analysis can be skipped (applicable if rules do not use variable analysis APIs).</param>
14901490
/// <returns></returns>
1491-
public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition, out ScriptBlockAst scriptAst, out Token[] scriptTokens, bool skipVariableAnalysis = false)
1491+
public List<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefinition, out ScriptBlockAst scriptAst, out Token[] scriptTokens, bool skipVariableAnalysis = false)
14921492
{
14931493
scriptAst = null;
14941494
scriptTokens = null;
@@ -1503,7 +1503,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
15031503
catch (Exception e)
15041504
{
15051505
this.outputWriter.WriteWarning(e.ToString());
1506-
return null;
1506+
return new();
15071507
}
15081508

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

15301530
// now, analyze the script definition
1531-
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, String.Empty, skipVariableAnalysis));
1531+
diagnosticRecords.AddRange(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, null, skipVariableAnalysis));
1532+
return diagnosticRecords;
15321533
}
15331534

15341535
/// <summary>

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,29 @@ Describe "Test -EnableExit Switch" {
561561

562562
$pssaPath = (Get-Module PSScriptAnalyzer).Path
563563

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

566-
$LASTEXITCODE | Should -Be 1
566+
$LASTEXITCODE | Should -Be 1
567+
}
568+
569+
It "Returns exit code equivalent to number of warnings for multiple piped files" {
570+
if ($IsCoreCLR)
571+
{
572+
$pwshExe = (Get-Process -Id $PID).Path
573+
}
574+
else
575+
{
576+
$pwshExe = 'powershell'
577+
}
578+
579+
$pssaPath = (Get-Module PSScriptAnalyzer).Path
580+
581+
& $pwshExe -NoProfile {
582+
Import-Module $Args[0]
583+
Get-ChildItem $Args[1] | Invoke-ScriptAnalyzer -EnableExit
584+
} -Args $pssaPath, "$PSScriptRoot\RecursionDirectoryTest"
585+
586+
$LASTEXITCODE | Should -Be 2
567587
}
568588

569589
Describe "-ReportSummary switch" {

0 commit comments

Comments
 (0)