Skip to content

Commit 4f1ca19

Browse files
committed
Merge pull request #35 from PowerShell/NewRule
Add Tests for UseShouldProcessForStateChangingFunctions
2 parents ac7fd00 + 763844f commit 4f1ca19

3 files changed

+48
-0
lines changed
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)