Skip to content

Commit b5d4932

Browse files
committed
Set exit code of Invoke-ScriptAnalyzer -EnableExit to total number of diagnostics
1 parent 9dacfa8 commit b5d4932

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 11 additions & 8 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,7 +431,10 @@ protected override void StopProcessing()
426431

427432
private void ProcessInput()
428433
{
429-
WriteToOutput(RunAnalysis());
434+
// collect to array to avoid double enumeration
435+
var diagnosticRecords = RunAnalysis().ToArray();
436+
WriteToOutput(diagnosticRecords);
437+
totalDiagnosticCount += diagnosticRecords.Length;
430438
}
431439

432440
private IEnumerable<DiagnosticRecord> RunAnalysis()
@@ -454,7 +462,7 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
454462
return diagnostics;
455463
}
456464

457-
private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
465+
private void WriteToOutput(DiagnosticRecord[] diagnosticRecords)
458466
{
459467
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
460468
{
@@ -507,11 +515,6 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
507515
}
508516
}
509517
}
510-
511-
if (EnableExit.IsPresent)
512-
{
513-
this.Host.SetShouldExit(diagnosticRecords.Count());
514-
}
515518
}
516519

517520
private void ProcessPath()
@@ -535,4 +538,4 @@ private bool OverrideSwitchParam(bool paramValue, string paramName)
535538

536539
#endregion // Private Methods
537540
}
538-
}
541+
}

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)