From 446ab85ef0f3136efc7fc86bb44bd65d2b08b6c5 Mon Sep 17 00:00:00 2001 From: Yuting Chen Date: Thu, 16 Apr 2015 16:20:28 -0700 Subject: [PATCH 1/2] Add Tests for UseShouldProcessForStateChangingFunctions --- ...ShouldProcessForStateChangingFunctions.ps1 | 11 +++++++++ ...ProcessForStateChangingFunctions.tests.ps1 | 24 +++++++++++++++++++ ...sForStateChangingFunctionsNoViolations.ps1 | 12 ++++++++++ 3 files changed, 47 insertions(+) create mode 100644 Tests/Rules/UseShouldProcessForStateChangingFunctions.ps1 create mode 100644 Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 create mode 100644 Tests/Rules/UseShouldProcessForStateChangingFunctionsNoViolations.ps1 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..e037eaa6f --- /dev/null +++ b/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 @@ -0,0 +1,24 @@ +Import-Module PSScriptAnalyzer +$violationMessage = "File AvoidConvertToSecureStringWithPlainText.ps1 uses ConvertTo-SecureString with plaintext. This will expose secure information. Encrypted standard strings should be used instead." +$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 + } + } +} \ No newline at end of file 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) + +} + From 763844fbe91f12722f91ec0b18ee0f98f302de48 Mon Sep 17 00:00:00 2001 From: "Yuting Chen[MSFT]" Date: Thu, 16 Apr 2015 16:27:00 -0700 Subject: [PATCH 2/2] Update UseShouldProcessForStateChangingFunctions.tests.ps1 Sorry. Pushed the wrong file. Corrected the message. --- .../UseShouldProcessForStateChangingFunctions.tests.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 b/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 index e037eaa6f..e5998e00e 100644 --- a/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 +++ b/Tests/Rules/UseShouldProcessForStateChangingFunctions.tests.ps1 @@ -1,5 +1,6 @@ Import-Module PSScriptAnalyzer -$violationMessage = "File AvoidConvertToSecureStringWithPlainText.ps1 uses ConvertTo-SecureString with plaintext. This will expose secure information. Encrypted standard strings should be used instead." +$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} @@ -21,4 +22,4 @@ Describe "" { $noViolations.Count | Should Be 0 } } -} \ No newline at end of file +}