Skip to content

Commit f002c1c

Browse files
bergmeisterJamesWTruher
authored andcommitted
Do not trigger UseShouldProcessForStateChangingFunctions rule for workflows (#923)
* Workflows do not allow SupportsShouldProcess -> do not trigger UseShouldProcessForStateChangingFunctions * correct typo * Workflows are not supported in PowerShell 6.0 -> do not run test in this case
1 parent bc5b2dd commit f002c1c

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

Rules/UseShouldProcessForStateChangingFunctions.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
// Copyright (c) Microsoft Corporation.
2-
//
3-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
6-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9-
// THE SOFTWARE.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
103

114
using System;
125
using System.Collections.Generic;
136
#if !CORECLR
147
using System.ComponentModel.Composition;
158
#endif
16-
using System.Management.Automation;
179
using System.Management.Automation.Language;
1810
using System.Globalization;
19-
using System.Linq;
2011
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
2112

2213
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
23-
{
14+
{
2415
/// <summary>
2516
/// UseShouldProcessForStateChangingFunctions: Analyzes the ast to check if ShouldProcess is included in Advanced functions if the Verb of the function could change system state.
2617
/// </summary>
@@ -61,7 +52,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6152
private bool IsStateChangingFunctionWithNoShouldProcessAttribute(Ast ast)
6253
{
6354
var funcDefAst = ast as FunctionDefinitionAst;
64-
if (funcDefAst == null)
55+
// SupportsShouldProcess is not supported in workflows
56+
if (funcDefAst == null || funcDefAst.IsWorkflow)
6557
{
6658
return false;
6759
}

Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,11 @@ Function New-{0} () {{ }}
4242
It "returns no violations" {
4343
$noViolations.Count | Should -Be 0
4444
}
45+
46+
It "Workflows should not trigger a warning because they do not allow SupportsShouldProcess" -Skip:$IsCoreCLR {
47+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition 'workflow Set-Something {[CmdletBinding()]Param($Param1)}' | Where-Object {
48+
$_.RuleName -eq 'PSUseShouldProcessForStateChangingFunctions' }
49+
$violations.Count | Should -Be 0
50+
}
4551
}
4652
}

0 commit comments

Comments
 (0)