From 27e4ddcddb40172a70b9990bcbef0cfc4ca905bc Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 9 Dec 2017 00:37:05 +0000 Subject: [PATCH 1/8] Add -EnableExit switch --- .../Commands/InvokeScriptAnalyzerCommand.cs | 16 ++++++++++++ Tests/Engine/LibraryUsage.tests.ps1 | 25 ++++++++++++++----- docs/markdown/Invoke-ScriptAnalyzer.md | 19 ++++++++++++-- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Engine/Commands/InvokeScriptAnalyzerCommand.cs b/Engine/Commands/InvokeScriptAnalyzerCommand.cs index 40b8d5cff..4719a51a1 100644 --- a/Engine/Commands/InvokeScriptAnalyzerCommand.cs +++ b/Engine/Commands/InvokeScriptAnalyzerCommand.cs @@ -192,6 +192,17 @@ public SwitchParameter Fix } private bool fix; + /// + /// Sets the exit code to the number of warnings for usage in CI. + /// + [Parameter(Mandatory = false)] + public SwitchParameter EnableExit + { + get { return enableExit; } + set { enableExit = value; } + } + private bool enableExit; + /// /// Returns path to the file that contains user profile or hash table for ScriptAnalyzer /// @@ -418,6 +429,11 @@ private void WriteToOutput(IEnumerable diagnosticRecords) logger.LogObject(diagnostic, this); } } + + if (EnableExit.IsPresent) + { + this.Host.SetShouldExit(diagnosticRecords.Count()); + } } private void ProcessPath() diff --git a/Tests/Engine/LibraryUsage.tests.ps1 b/Tests/Engine/LibraryUsage.tests.ps1 index a945fdd02..1c3061163 100644 --- a/Tests/Engine/LibraryUsage.tests.ps1 +++ b/Tests/Engine/LibraryUsage.tests.ps1 @@ -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) @@ -77,17 +80,27 @@ function Invoke-ScriptAnalyzer { $SuppressedOnly.IsPresent ); - if ($PSCmdlet.ParameterSetName -eq "File") { + if ($PSCmdlet.ParameterSetName -eq "File") + { $supportsShouldProcessFunc = [Func[string, string, bool]]{ return $PSCmdlet.Shouldprocess } if ($Fix.IsPresent) { - return $scriptAnalyzer.AnalyzeAndFixPath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent); + $results = $scriptAnalyzer.AnalyzeAndFixPath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent); + } + else + { + $results = $scriptAnalyzer.AnalyzePath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent); } - return $scriptAnalyzer.AnalyzePath($Path, $supportsShouldProcessFunc, $Recurse.IsPresent); } - else { - return $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition); + else + { + $results = $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition); } + + if ($null -ne $results) + { + exit $results.Count + } } # Define an implementation of the IOutputWriter interface diff --git a/docs/markdown/Invoke-ScriptAnalyzer.md b/docs/markdown/Invoke-ScriptAnalyzer.md index b421ea076..4c68b4231 100644 --- a/docs/markdown/Invoke-ScriptAnalyzer.md +++ b/docs/markdown/Invoke-ScriptAnalyzer.md @@ -12,14 +12,14 @@ Evaluates a script or module based on selected best practice rules ### UNNAMED_PARAMETER_SET_1 ``` Invoke-ScriptAnalyzer [-Path] [-CustomRulePath ] [-RecurseCustomRulePath] - [-ExcludeRule ] [-IncludeRule ] [-Severity ] [-Recurse] [-SuppressedOnly] [-Fix] + [-ExcludeRule ] [-IncludeRule ] [-Severity ] [-Recurse] [-SuppressedOnly] [-Fix] [-EnableExit] [-Settings ] ``` ### UNNAMED_PARAMETER_SET_2 ``` Invoke-ScriptAnalyzer [-ScriptDefinition] [-CustomRulePath ] [-RecurseCustomRulePath] - [-ExcludeRule ] [-IncludeRule ] [-Severity ] [-Recurse] [-SuppressedOnly] + [-ExcludeRule ] [-IncludeRule ] [-Severity ] [-Recurse] [-SuppressedOnly] [-EnableExit] [-Settings ] ``` @@ -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. @@ -409,6 +410,20 @@ Type: SwitchParameter Parameter Sets: UNNAMED_PARAMETER_SET_1 Aliases: +Required: False +Position: Named +Default value: False +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 From 9bae52c54ded28167ad4dc868709fda35cb7c186 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 9 Dec 2017 00:50:05 +0000 Subject: [PATCH 2/8] fix yaml in markdown file --- docs/markdown/Invoke-ScriptAnalyzer.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/markdown/Invoke-ScriptAnalyzer.md b/docs/markdown/Invoke-ScriptAnalyzer.md index 4c68b4231..a83634afd 100644 --- a/docs/markdown/Invoke-ScriptAnalyzer.md +++ b/docs/markdown/Invoke-ScriptAnalyzer.md @@ -415,6 +415,7 @@ Position: Named Default value: False 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. From 9421b2caa4857ef5fbae72bf9d5ef5b9cd24000d Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 9 Dec 2017 01:10:17 +0000 Subject: [PATCH 3/8] fix LibraryUsage.tests.ps1 for -EnableExit switch --- Tests/Engine/LibraryUsage.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Engine/LibraryUsage.tests.ps1 b/Tests/Engine/LibraryUsage.tests.ps1 index 1c3061163..687d49a02 100644 --- a/Tests/Engine/LibraryUsage.tests.ps1 +++ b/Tests/Engine/LibraryUsage.tests.ps1 @@ -53,7 +53,7 @@ function Invoke-ScriptAnalyzer { [Parameter(Mandatory = $false)] [switch] $Fix, - [Parameter(Mandatory = $false)] + [Parameter(Mandatory = $false)] [switch] $EnableExit ) @@ -97,7 +97,7 @@ function Invoke-ScriptAnalyzer { $results = $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition); } - if ($null -ne $results) + if ($EnableExit.IsPresent -and $null -ne $results) { exit $results.Count } From 349be0b74cbdcfe22912731ef1571f992fdebe04 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 9 Dec 2017 01:23:03 +0000 Subject: [PATCH 4/8] fix LibraryUsage.tests.ps1 to return results --- Tests/Engine/LibraryUsage.tests.ps1 | 34 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Tests/Engine/LibraryUsage.tests.ps1 b/Tests/Engine/LibraryUsage.tests.ps1 index 687d49a02..3bf7890bd 100644 --- a/Tests/Engine/LibraryUsage.tests.ps1 +++ b/Tests/Engine/LibraryUsage.tests.ps1 @@ -81,26 +81,28 @@ function Invoke-ScriptAnalyzer { ); 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); - } + { + $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 - { + { $results = $scriptAnalyzer.AnalyzeScriptDefinition($ScriptDefinition); - } - - if ($EnableExit.IsPresent -and $null -ne $results) - { - exit $results.Count } + + $results + + if ($EnableExit.IsPresent -and $null -ne $results) + { + exit $results.Count + } } # Define an implementation of the IOutputWriter interface From a26ecd2763f7b43cd61f6c6e88db2c7166a2310f Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 20 Jan 2018 20:39:36 +0000 Subject: [PATCH 5/8] add test for -EnableExit switch --- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index a915bed85..abe9d370f 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -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" { + $process = Start-Process powershell -ArgumentList 'Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit' -PassThru + $process.ExitCode | Should Be 1 + } +} From fcfe48c439ce37e2e77cf84a30bda3370aee872a Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 20 Jan 2018 21:11:07 +0000 Subject: [PATCH 6/8] Fix test by waiting for the powershell process to finish and improve it to hide the window --- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index abe9d370f..d85a1960b 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -500,7 +500,7 @@ Describe "Test -Fix Switch" { Describe "Test -EnableExit Switch" { It "Returns exit code equivalent to number of warnings" { - $process = Start-Process powershell -ArgumentList 'Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit' -PassThru + $process = Start-Process powershell -ArgumentList '-WindowStyle Hidden -Command Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit' -PassThru -Wait $process.ExitCode | Should Be 1 } } From 21295ed52fe729bac224139c017c9f4d21ef4fec Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 20 Jan 2018 21:20:54 +0000 Subject: [PATCH 7/8] Add -EnableExit switch to Invoke-ScriptAnalyzer cmdlet documentation in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b532a5c7b..43924b9ba 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Usage ``` PowerShell Get-ScriptAnalyzerRule [-CustomizedRulePath ] [-Name ] [] [-Severity ] -Invoke-ScriptAnalyzer [-Path] [-CustomizedRulePath ] [-ExcludeRule ] [-IncludeRule ] [-Severity ] [-Recurse] [] +Invoke-ScriptAnalyzer [-Path] [-CustomizedRulePath ] [-ExcludeRule ] [-IncludeRule ] [-Severity ] [-Recurse] [-EnableExit] [] ``` [Back to ToC](#table-of-contents) From 91d1c4f7354fcc9df3fa8b0fe62c227707107dd7 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 20 Jan 2018 23:51:39 +0000 Subject: [PATCH 8/8] Call powerShell directly to avoid using unsupported (in pwsh) WindowStyle parameter --- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index d85a1960b..50f2e8a2f 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -500,7 +500,7 @@ Describe "Test -Fix Switch" { Describe "Test -EnableExit Switch" { It "Returns exit code equivalent to number of warnings" { - $process = Start-Process powershell -ArgumentList '-WindowStyle Hidden -Command Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit' -PassThru -Wait - $process.ExitCode | Should Be 1 + powershell -Command 'Import-Module PSScriptAnalyzer; Invoke-ScriptAnalyzer -ScriptDefinition gci -EnableExit' + $LASTEXITCODE | Should Be 1 } }