Skip to content

Commit f5c5efb

Browse files
author
Kapil Borle
authored
Fix PSUseShouldProcessForStateChangingFunctions rule (#634)
* Add "start" to state changing verbs UseShouldProcessForStateChanging did not check for start-* in a function name. This commit adds "start-" to the list of state changing verbs. Also, updates and makes the rule documentation consistent with the implmentation. * Add tests for each state changing verb
1 parent 0104451 commit f5c5efb

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Engine/Helper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,13 +1555,14 @@ public bool IsStateChangingFunctionName(string functionName)
15551555
// Array of verbs that can potentially change the state of a system
15561556
string[] stateChangingVerbs =
15571557
{
1558-
"Restart-",
1559-
"Stop-",
15601558
"New-",
15611559
"Set-",
1562-
"Update-",
1560+
"Remove-",
1561+
"Start-",
1562+
"Stop-",
1563+
"Restart-",
15631564
"Reset-",
1564-
"Remove-"
1565+
"Update-"
15651566
};
15661567
foreach (var verb in stateChangingVerbs)
15671568
{

RuleDocumentation/UseShouldProcessForStateChangingFunctions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ Functions whose verbs change system state should support `ShouldProcess`.
66

77
Verbs that should support `ShouldProcess`:
88
* `New`
9-
* `Reset`
10-
* `Restart`
119
* `Set`
10+
* `Remove`
1211
* `Start`
1312
* `Stop`
13+
* `Restart`
14+
* `Reset`
15+
* `Update`
1416

1517
##How to Fix
1618
Include the attribute `SupportsShouldProcess`, in the `CmdletBindingBinding`.

Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ $violations = Invoke-ScriptAnalyzer $directory\UseShouldProcessForStateChangingF
66
$noViolations = Invoke-ScriptAnalyzer $directory\UseShouldProcessForStateChangingFunctionsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
77

88
Describe "It checks UseShouldProcess is enabled when there are state changing verbs in the function names" {
9+
Context "When function name has state changing verb" {
10+
11+
Function Test-Verb([string] $verb)
12+
{
13+
$scriptDefinitionGeneric = @'
14+
Function New-{0} () {{ }}
15+
'@
16+
$scriptDef = $scriptDefinitionGeneric -f $verb
17+
It ('finds "{0}" verb in function name' -f $verb) {
18+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule $violationName
19+
$violations.Count | Should Be 1
20+
}
21+
}
22+
23+
$verbs = @('New', 'Set', 'Remove', 'Start', 'Stop', 'Restart', 'Reset', 'Update')
24+
$verbs | ForEach-Object {Test-Verb $_}
25+
}
926
Context "When there are violations" {
1027
$numViolations = 5
1128
It ("has {0} violations where ShouldProcess is not supported" -f $numViolations) {
@@ -16,9 +33,9 @@ Describe "It checks UseShouldProcess is enabled when there are state changing ve
1633
$violations[0].Message | Should Match $violationMessage
1734
}
1835

19-
It "has the correct extent" {
20-
$violations[0].Extent.Text | Should Be "Set-MyObject"
21-
}
36+
It "has the correct extent" {
37+
$violations[0].Extent.Text | Should Be "Set-MyObject"
38+
}
2239
}
2340

2441
Context "When there are no violations" {

0 commit comments

Comments
 (0)