Skip to content

Commit 767a438

Browse files
committed
Merge branch 'NewRule' into BugFixes
2 parents ac7fd00 + 8ef6b7d commit 767a438

7 files changed

+90
-14
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#UseShouldProcessForStateChangingFunctions
2+
**Severity Level: Warning**
3+
4+
##Description
5+
6+
Functions that have verbs like New, Start, Stop, Set, Reset that change system state should support 'ShouldProcess'
7+
8+
##How to Fix
9+
10+
To fix a violation of this rule, please add attribute SupportsShouldProcess. eg: [CmdletBinding(SupportsShouldProcess = $true)] to the function.
11+
12+
##Example
13+
14+
Wrong:
15+
```
16+
function Set-ServiceObject
17+
{
18+
[CmdletBinding()]
19+
param ([string]$c)
20+
}
21+
```
22+
23+
Correct:
24+
```
25+
function Set-ServiceObject
26+
{
27+
[CmdletBinding(SupportsShouldProcess = $true)]
28+
param ([string]$c)
29+
}
30+
```

Rules/Strings.Designer.cs

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@
658658
<value>Use ShouldProcess For State Changing Functions</value>
659659
</data>
660660
<data name="UseShouldProcessForStateChangingFunctionsDescrption" xml:space="preserve">
661-
<value>Functions that have verbs like New, Start, Stop, Set, Reset that change system state should support 'ShouldProcess'.</value>
661+
<value>Functions that have verbs like New, Start, Stop, Set, Reset, Restart that change system state should support 'ShouldProcess'.</value>
662662
</data>
663663
<data name="UseShouldProcessForStateChangingFunctionsError" xml:space="preserve">
664664
<value>Function ’{0}’ has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.</value>

Rules/UseShouldProcessForStateChangingFunctions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4444
string funcName = funcDefAst.Name;
4545
bool hasShouldProcessAttribute = false;
4646

47-
if (funcName.StartsWith("Get-", StringComparison.OrdinalIgnoreCase) ||
47+
if (funcName.StartsWith("Restart-", StringComparison.OrdinalIgnoreCase) ||
4848
funcName.StartsWith("Stop-", StringComparison.OrdinalIgnoreCase)||
4949
funcName.StartsWith("New-", StringComparison.OrdinalIgnoreCase) ||
5050
funcName.StartsWith("Set-", StringComparison.OrdinalIgnoreCase) ||
@@ -76,7 +76,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7676
if (!hasShouldProcessAttribute)
7777
{
7878
yield return
79-
new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture,Strings.UseShouldProcessForStateChangingFunctionsError, funcName),funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
79+
new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture,Strings.UseShouldProcessForStateChangingFunctionsError, funcName),funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
8080
}
8181
}
8282
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Get-Service
2+
{
3+
param ([string]$c)
4+
}
5+
6+
function Get-MyObject{
7+
[CmdletBinding(SupportsShouldProcess = $false)]
8+
param([string]$c, [int]$d)
9+
10+
}
11+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Import-Module PSScriptAnalyzer
2+
$violationMessage = "Function ’Get-Service’ has verb that could change system state. Therefore, the function has to support 'ShouldProcess'"
3+
$violationName = "PSUseShouldProcessForStateChangingFunctions"
4+
$violationName = "PS.UseShouldProcessForStateChangingFunctions"
5+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
6+
$violations = Invoke-ScriptAnalyzer $directory\UseShouldProcessForStateChangingFunctions.ps1 | Where-Object {$_.RuleName -eq $violationName}
7+
$noViolations = Invoke-ScriptAnalyzer $directory\UseShouldProcessForStateChangingFunctionsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
8+
9+
Describe "" {
10+
Context "When there are violations" {
11+
It "has 2 violations where ShouldProcess is not supported" {
12+
$violations.Count | Should Be 3
13+
}
14+
15+
It "has the correct description message" {
16+
$violations[0].Message | Should Match $violationMessage
17+
}
18+
}
19+
20+
Context "When there are no violations" {
21+
It "returns no violations" {
22+
$noViolations.Count | Should Be 0
23+
}
24+
}
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Get-Service
2+
{
3+
[CmdletBinding(SupportShouldSuppress= $false)]
4+
param ([string]$c)
5+
}
6+
7+
function Test-GetMyObject{
8+
9+
param([string]$c, [int]$d)
10+
11+
}
12+

0 commit comments

Comments
 (0)