DscResource.Tests/Tests/Unit/DscResource.AnalyzerRules.Helper.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
Describe 'DscResource.AnalyzerRules.Helper Unit Tests' {
    BeforeAll {
        $projectRootPath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
        $moduleRootPath = Join-Path -Path $projectRootPath -ChildPath 'DscResource.AnalyzerRules'
        $modulePath = Join-Path -Path $moduleRootPath -ChildPath 'DscResource.AnalyzerRules.Helper.psm1'

        Import-Module -Name $modulePath -Force
    }
    InModuleScope 'DscResource.AnalyzerRules.Helper' {
        Describe 'Get-StatementBlockAsRows' {
            Context 'When string contains CRLF as new line' {
                BeforeAll {
                    $expectedReturnValue1 = 'First line'
                    $expectedReturnValue2 = 'Second line'
                }

                It 'Should return the correct array of strings' {
                    $getStatementBlockAsRowsParameters = @{
                        StatementBlock = "First line`r`nSecond line"
                    }

                    $getStatementBlockAsRowsResult = `
                        Get-StatementBlockAsRows @getStatementBlockAsRowsParameters

                    $getStatementBlockAsRowsResult[0] | Should -Be $expectedReturnValue1
                    $getStatementBlockAsRowsResult[1] | Should -Be $expectedReturnValue2
                }
            }

            Context 'When string contains LF as new line' {
                It 'Should return the correct array of strings' {
                    $getStatementBlockAsRowsParameters = @{
                        StatementBlock = "First line`nSecond line"
                    }

                    $getStatementBlockAsRowsResult = `
                        Get-StatementBlockAsRows @getStatementBlockAsRowsParameters

                    $getStatementBlockAsRowsResult[0] | Should -Be $expectedReturnValue1
                    $getStatementBlockAsRowsResult[1] | Should -Be $expectedReturnValue2
                }
            }
        }

        Describe 'Test-StatementOpeningBraceOnSameLine' {
            Context 'When statement has an opening brace on the same line' {
                It 'Should return $true' {
                    $testStatementOpeningBraceOnSameLineParameters = @{
                        StatementBlock = `
                            'if ($true) {
                             }
                            '

                    }

                    $testStatementOpeningBraceOnSameLineResult = `
                        Test-StatementOpeningBraceOnSameLine @testStatementOpeningBraceOnSameLineParameters

                    $testStatementOpeningBraceOnSameLineResult | Should -Be $true
                }
            }

            # Regression test for issue reported in review comment for PR #180.
            Context 'When statement is using braces in the evaluation expression' {
                It 'Should return $false' {
                    $testStatementOpeningBraceOnSameLineParameters = @{
                        StatementBlock = `
                            'if (Get-Command | Where-Object -FilterScript { $_.Name -eq ''Get-Help'' } )
                             {
                             }
                            '

                    }

                    $testStatementOpeningBraceOnSameLineResult = `
                        Test-StatementOpeningBraceOnSameLine @testStatementOpeningBraceOnSameLineParameters

                    $testStatementOpeningBraceOnSameLineResult | Should -Be $false
                }
            }

            Context 'When statement follows style guideline' {
                It 'Should return $false' {
                    $testStatementOpeningBraceOnSameLineParameters = @{
                        StatementBlock = `
                            'if ($true)
                             {
                             }
                            '

                    }

                    $testStatementOpeningBraceOnSameLineResult = `
                        Test-StatementOpeningBraceOnSameLine @testStatementOpeningBraceOnSameLineParameters

                    $testStatementOpeningBraceOnSameLineResult | Should -Be $false
                }
            }
        }

        Describe 'Test-StatementOpeningBraceIsNotFollowedByNewLine' {
            Context 'When statement opening brace is not followed by a new line' {
                It 'Should return $true' {
                    $testStatementOpeningBraceIsNotFollowedByNewLineParameters = @{
                        StatementBlock = `
                            'if ($true)
                             { if ($false)
                                {
                                }
                             }
                            '

                    }

                    $testStatementOpeningBraceIsNotFollowedByNewLineResult = `
                        Test-StatementOpeningBraceIsNotFollowedByNewLine @testStatementOpeningBraceIsNotFollowedByNewLineParameters

                    $testStatementOpeningBraceIsNotFollowedByNewLineResult | Should -Be $true
                }
            }

            Context 'When statement follows style guideline' {
                It 'Should return $false' {
                    $testStatementOpeningBraceIsNotFollowedByNewLineParameters = @{
                        StatementBlock = `
                            'if ($true)
                             {
                                if ($false)
                                {
                                }
                             }
                            '

                    }

                    $testStatementOpeningBraceIsNotFollowedByNewLineResult = `
                        Test-StatementOpeningBraceIsNotFollowedByNewLine @testStatementOpeningBraceIsNotFollowedByNewLineParameters

                    $testStatementOpeningBraceIsNotFollowedByNewLineResult | Should -Be $false
                }
            }
        }

        Describe 'Test-StatementOpeningBraceIsFollowedByMoreThanOneNewLine' {
            Context 'When statement opening brace is not followed by a new line' {
                It 'Should return $true' {
                    $testStatementOpeningBraceIsFollowedByMoreThanOneNewLineParameters = @{
                        StatementBlock = `
                            'if ($true)
                             {
 
                                if ($false)
                                {
                                }
                             }
                            '

                    }

                    $testStatementOpeningBraceIsFollowedByMoreThanOneNewLineResult = `
                        Test-StatementOpeningBraceIsFollowedByMoreThanOneNewLine @testStatementOpeningBraceIsFollowedByMoreThanOneNewLineParameters

                    $testStatementOpeningBraceIsFollowedByMoreThanOneNewLineResult | Should -Be $true
                }
            }

            Context 'When statement follows style guideline' {
                It 'Should return $false' {
                    $testStatementOpeningBraceIsFollowedByMoreThanOneNewLineParameters = @{
                        StatementBlock = `
                            'if ($true)
                             {
                                if ($false)
                                {
                                }
                             }
                            '

                    }

                    $testStatementOpeningBraceIsFollowedByMoreThanOneNewLineResult = `
                        Test-StatementOpeningBraceIsFollowedByMoreThanOneNewLine @testStatementOpeningBraceIsFollowedByMoreThanOneNewLineParameters

                    $testStatementOpeningBraceIsFollowedByMoreThanOneNewLineResult | Should -Be $false
                }
            }
        }

        Describe 'Test-isInClass work' {
            Context 'Non Class AST' {
                It 'Should return false for an AST not in a Class AST' {
                    $definition = '
                    function Get-Something
                    {
                        Param
                        (
                            [Parameter(Mandatory=$true)]
                            [string]
                            $Path
                        )
 
                        $Path
                    }
                '

                    $Ast = [System.Management.Automation.Language.Parser]::ParseInput($definition, [ref] $null, [ref] $null)
                    $ParameterAst = $Ast.Find( {
                        param
                        (
                            [System.Management.Automation.Language.Ast]
                            $AST
                        )
                        $Ast -is [System.Management.Automation.Language.ParameterAst]
                    }, $true)
                    ($ParameterAst -is [System.Management.Automation.Language.ParameterAst]) | Should -Be $true
                    $isInClass = Test-isInClass -Ast $ParameterAst
                    $isInClass | Should -Be $false
                }
            }

            Context 'Class AST' {
                It 'Should Return True for an AST contained in a class AST' {
                    $definition = '
                    class Something
                    {
                        [void] Write([int] $Num)
                        {
                            Write-Host "Writing $Num"
                        }
                    }
                '

                    $Ast = [System.Management.Automation.Language.Parser]::ParseInput($definition, [ref] $null, [ref] $null)
                    $ParameterAst = $Ast.Find( {
                        param
                        (
                            [System.Management.Automation.Language.Ast]
                            $AST
                        )
                        $Ast -is [System.Management.Automation.Language.ParameterAst]
                    }, $true)
                    ($ParameterAst -is [System.Management.Automation.Language.ParameterAst]) | Should -Be $true
                    $isInClass = Test-isInClass -Ast $ParameterAst
                    $isInClass | Should -Be $true
                }

                It "Should return false for an AST contained in a ScriptBlock`r`n`tthat is a value assignment for a property or method in a class AST" {
                    $definition = '
                    class Something
                    {
                        [Func[Int,Int]] $MakeInt = {
                            [Parameter(Mandatory=$true)]
                            Param
                            (
                                [Parameter(Mandatory)]
                                [int] $Input
                            )
                            $Input * 2
                        }
                    }
                '

                    $Ast = [System.Management.Automation.Language.Parser]::ParseInput($definition, [ref] $null, [ref] $null)
                    $ParameterAst = $Ast.Find( {
                        param
                        (
                            [System.Management.Automation.Language.Ast]
                            $AST
                        )
                        $Ast -is [System.Management.Automation.Language.ParameterAst]
                    }, $true)
                    ($ParameterAst -is [System.Management.Automation.Language.ParameterAst]) | Should -Be $true
                    $isInClass = Test-isInClass -Ast $ParameterAst
                    $isInClass | Should -Be $false

                }
            }
        }

        Describe 'Test-StatementContainsUpperCase' {
            Context 'When statement is all lower case' {
                It 'Should return false' {
                    $statementBlock = 'foreach ($a in $b)'

                    Test-StatementContainsUpperCase -StatementBlock $statementBlock | Should -Be $false
                }
            }

            Context 'When statement is all upper case' {
                It 'Should return true' {
                    $statementBlock = 'FOREACH ($a in $b)'

                    Test-StatementContainsUpperCase -StatementBlock $statementBlock | Should -Be $true
                }
            }

            Context 'When statement is starts with lower case but contains upper case letters' {
                It 'Should return true' {
                    $statementBlock = 'forEach ($a in $b)'

                    Test-StatementContainsUpperCase -StatementBlock $statementBlock | Should -Be $true
                }
            }
        }
    }
}