@@ -9,10 +9,18 @@ BeforeAll {
99}
1010
1111Describe " MissingTryBlock" {
12+
13+ BeforeAll {
14+ $Settings = @ {
15+ IncludeRules = @ ($ruleName )
16+ Rules = @ { $ruleName = @ { Enable = $true } }
17+ }
18+ }
19+
1220 Context " Violates" {
1321 It " Catch is missing a try block" {
1422 $scriptDefinition = { catch { " An error occurred." } }.ToString()
15- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
23+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
1624 $violations.Count | Should - Be 1
1725 $violations.Severity | Should - Be Warning
1826 $violations.Extent.Text | Should - Be catch
@@ -22,7 +30,7 @@ Describe "MissingTryBlock" {
2230
2331 It " Finally is missing a try block" {
2432 $scriptDefinition = { finally { " Finalizing..." } }.ToString()
25- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
33+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
2634 $violations.Count | Should - Be 1
2735 $violations.Severity | Should - Be Warning
2836 $violations.Extent.Text | Should - Be finally
@@ -34,7 +42,7 @@ Describe "MissingTryBlock" {
3442 $scriptDefinition = {
3543 catch { " An error occurred." } finally { " Finalizing..." }
3644 }.ToString()
37- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
45+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
3846 $violations.Count | Should - Be 1
3947 $violations.Severity | Should - Be Warning
4048 $violations.Extent.Text | Should - Be catch
@@ -47,7 +55,7 @@ Describe "MissingTryBlock" {
4755 catch { " An error occurred." }
4856 finally { " Finalizing..." }
4957 }.ToString()
50- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
58+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
5159 $violations.Count | Should - Be 2
5260 $violations [0 ].Severity | Should - Be Warning
5361 $violations [0 ].Extent.Text | Should - Be catch
@@ -66,7 +74,7 @@ Describe "MissingTryBlock" {
6674 try { NonsenseString }
6775 catch { " An error occurred." }
6876 }.ToString()
69- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
77+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
7078 $violations | Should - BeNullOrEmpty
7179 }
7280
@@ -76,33 +84,33 @@ Describe "MissingTryBlock" {
7684 catch { " An error occurred." }
7785 finally { " Finalizing..." }
7886 }.ToString()
79- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
87+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
8088 $violations | Should - BeNullOrEmpty
8189 }
8290
8391 It " Single line try statement" {
8492 $scriptDefinition = {
8593 try { NonsenseString } catch { " An error occurred." } finally { " Finalizing..." }
8694 }.ToString()
87- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
95+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
8896 $violations | Should - BeNullOrEmpty
8997 }
9098
9199 It " Catch as parameter" {
92100 $scriptDefinition = { Write-Host Catch }.ToString()
93- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
101+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
94102 $violations | Should - BeNullOrEmpty
95103 }
96104
97105 It " Catch as double quoted string" {
98106 $scriptDefinition = { " Catch" }.ToString()
99- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
107+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
100108 $violations | Should - BeNullOrEmpty
101109 }
102110
103111 It " Catch as single quoted string" {
104112 $scriptDefinition = { ' Catch' }.ToString()
105- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
113+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
106114 $violations | Should - BeNullOrEmpty
107115 }
108116 }
@@ -115,7 +123,7 @@ Describe "MissingTryBlock" {
115123 catch { " An error occurred." }
116124 finally { " Finalizing..." }
117125 }.ToString()
118- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
126+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
119127 $violations | Should - BeNullOrEmpty
120128 }
121129
@@ -126,8 +134,25 @@ Describe "MissingTryBlock" {
126134 catch { " An error occurred." }
127135 finally { " Finalizing..." }
128136 }.ToString()
129- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ( $ruleName )
137+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
130138 $violations.Count | Should - Be 1
131139 }
132140 }
141+
142+ Context " Disabled" {
143+
144+ BeforeAll {
145+ $Settings = @ {
146+ IncludeRules = @ ($ruleName )
147+ Rules = @ { $ruleName = @ { Enable = $false } }
148+ }
149+ }
150+
151+ It " ConvertFrom-SecureString -AsPlainText" {
152+ $scriptDefinition = { catch { " An error occurred." } }.ToString()
153+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - Settings $Settings
154+ $violations | Should - BeNullOrEmpty
155+ }
156+ }
157+
133158}
0 commit comments