diff --git a/Tests/Rules/UseShouldProcessForStateChangingFunctions.ps1 b/Tests/Rules/UseShouldProcessForStateChangingFunctions.ps1 new file mode 100644 index 000000000..925a1b809 --- /dev/null +++ b/Tests/Rules/UseShouldProcessForStateChangingFunctions.ps1 @@ -0,0 +1,11 @@ +function Get-Service +{ + param ([string]$c) +} + +function Get-MyObject{ + [CmdletBinding(SupportsShouldProcess = $false)] + param([string]$c, [int]$d) + +} + diff --git a/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 b/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 new file mode 100644 index 000000000..e5998e00e --- /dev/null +++ b/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 @@ -0,0 +1,25 @@ +Import-Module PSScriptAnalyzer +$violationMessage = "Function ’Get-Service’ has verb that could change system state. Therefore, the function has to support 'ShouldProcess'" +$violationName = "PSUseShouldProcessForStateChangingFunctions" +$violationName = "PS.UseShouldProcessForStateChangingFunctions" +$directory = Split-Path -Parent $MyInvocation.MyCommand.Path +$violations = Invoke-ScriptAnalyzer $directory\UseShouldProcessForStateChangingFunctions.ps1 | Where-Object {$_.RuleName -eq $violationName} +$noViolations = Invoke-ScriptAnalyzer $directory\UseShouldProcessForStateChangingFunctionsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName} + +Describe "" { + Context "When there are violations" { + It "has 2 violations where ShouldProcess is not supported" { + $violations.Count | Should Be 3 + } + + It "has the correct description message" { + $violations[0].Message | Should Match $violationMessage + } + } + + Context "When there are no violations" { + It "returns no violations" { + $noViolations.Count | Should Be 0 + } + } +} diff --git a/Tests/Rules/UseShouldProcessForStateChangingFunctionsNoViolations.ps1 b/Tests/Rules/UseShouldProcessForStateChangingFunctionsNoViolations.ps1 new file mode 100644 index 000000000..cb4b2cfc9 --- /dev/null +++ b/Tests/Rules/UseShouldProcessForStateChangingFunctionsNoViolations.ps1 @@ -0,0 +1,12 @@ +function Get-Service +{ + [CmdletBinding(SupportShouldSuppress= $false)] + param ([string]$c) +} + +function Test-GetMyObject{ + + param([string]$c, [int]$d) + +} +