Functions/Gherkin.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
if ($PSVersionTable.PSVersion.Major -le 2) { return }

Add-Type -Path "${Script:PesterRoot}\lib\Gherkin.dll"

$StepPrefix = "Gherkin-Step "
$GherkinSteps = @{}
$GherkinHooks = @{
    BeforeAllFeatures = @()
    BeforeFeature = @()
    BeforeScenario = @()
    BeforeStep = @()
    AfterAllFeatures = @()
    AfterFeature = @()
    AfterScenario = @()
    AfterStep = @()
}

function Invoke-GherkinHook {
    [CmdletBinding()]
    param([string]$Hook, [string]$Name, [string[]]$Tags)

    if($GherkinHooks.${Hook}) {
        foreach($GherkinHook in $GherkinHooks.${Hook}) {
            if($GherkinHook.Tags -and $Tags) {
                :tags foreach($hookTag in $GherkinHook.Tags) {
                    foreach($testTag in $Tags) {
                        if($testTag -match "^($hookTag)$") {
                            & $hook.Script $Name
                            break :tags
                        }
                    }
                }
            } elseif($GherkinHook.Tags) {
                # If the hook has tags, it can't run if the step doesn't
            } else {
                & $GherkinHook.Script $Name
            }
        } # @{ Tags = $Tags; Script = $Test }
    }
}

function Invoke-Gherkin {
    <#
        .Synopsis
            Invokes Pester to run all tests defined in .feature files
        .Description
            Upon calling Invoke-Gherkin, all files that have a name matching *.feature in the current folder (and child folders recursively), will be parsed and executed.
 
            If ScenarioName is specified, only scenarios which match the provided name(s) will be run. If FailedLast is specified, only scenarios which failed the previous run will be re-executed.
 
            Optionally, Pester can generate a report of how much code is covered by the tests, and information about any commands which were not executed.
        .Example
            Invoke-Gherkin
 
            This will find all *.feature specifications and execute their tests. No exit code will be returned and no log file will be saved.
 
        .Example
            Invoke-Gherkin -Path ./tests/Utils*
 
            This will run all *.feature specifications under ./Tests that begin with Utils.
 
        .Example
            Invoke-Gherkin -ScenarioName "Add Numbers"
 
            This will only run the Scenario named "Add Numbers"
 
        .Example
            Invoke-Gherkin -EnableExit -OutputXml "./artifacts/TestResults.xml"
 
            This runs all tests from the current directory downwards and writes the results according to the NUnit schema to artifatcs/TestResults.xml just below the current directory. The test run will return an exit code equal to the number of test failures.
 
        .Example
            Invoke-Gherkin -CodeCoverage 'ScriptUnderTest.ps1'
 
            Runs all *.feature specifications in the current directory, and generates a coverage report for all commands in the "ScriptUnderTest.ps1" file.
 
        .Example
            Invoke-Gherkin -CodeCoverage @{ Path = 'ScriptUnderTest.ps1'; Function = 'FunctionUnderTest' }
 
            Runs all *.feature specifications in the current directory, and generates a coverage report for all commands in the "FunctionUnderTest" function in the "ScriptUnderTest.ps1" file.
 
        .Example
            Invoke-Gherkin -CodeCoverage @{ Path = 'ScriptUnderTest.ps1'; StartLine = 10; EndLine = 20 }
 
            Runs all *.feature specifications in the current directory, and generates a coverage report for all commands on lines 10 through 20 in the "ScriptUnderTest.ps1" file.
 
        .Link
            Invoke-Pester
    #>

    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param(
        # Rerun only the scenarios which failed last time
        [Parameter(Mandatory = $True, ParameterSetName = "RetestFailed")]
        [switch]$FailedLast,

        # This parameter indicates which feature files should be tested.
        # Aliased to 'Script' for compatibility with Pester, but does not support hashtables, since feature files don't take parameters.
        [Parameter(Position=0,Mandatory=0)]
        [Alias('Script','relative_path')]
        [string]$Path = $Pwd,

        # When set, invokes testing of scenarios which match this name.
        # Aliased to 'Name' and 'TestName' for compatibility with Pester.
        [Parameter(Position=1,Mandatory=0)]
        [Alias("Name","TestName")]
        [string[]]$ScenarioName,

        # Will cause Invoke-Gherkin to exit with a exit code equal to the number of failed tests once all tests have been run. Use this to "fail" a build when any tests fail.
        [Parameter(Position=2,Mandatory=0)]
        [switch]$EnableExit,

        # Filters Scenarios and Features and runs only the ones tagged with the specified tags.
        [Parameter(Position=4,Mandatory=0)]
        [Alias('Tags')]
        [string[]]$Tag,

        # Informs Invoke-Pester to not run blocks tagged with the tags specified.
        [string[]]$ExcludeTag,

        # Instructs Pester to generate a code coverage report in addition to running tests. You may pass either hashtables or strings to this parameter.
        # If strings are used, they must be paths (wildcards allowed) to source files, and all commands in the files are analyzed for code coverage.
        # By passing hashtables instead, you can limit the analysis to specific lines or functions within a file.
        # Hashtables must contain a Path key (which can be abbreviated to just "P"), and may contain Function (or "F"), StartLine (or "S"), and EndLine ("E") keys to narrow down the commands to be analyzed.
        # If Function is specified, StartLine and EndLine are ignored.
        # If only StartLine is defined, the entire script file starting with StartLine is analyzed.
        # If only EndLine is present, all lines in the script file up to and including EndLine are analyzed.
        # Both Function and Path (as well as simple strings passed instead of hashtables) may contain wildcards.
        [object[]] $CodeCoverage = @(),

        # Makes Pending and Skipped tests to Failed tests. Useful for continuous integration where you need to make sure all tests passed.
        [Switch]$Strict,

        # The path to write a report file to. If this path is not provided, no log will be generated.
        [string] $OutputFile,

        # The format for output (LegacyNUnitXml or NUnitXml), defaults to NUnitXml
        [ValidateSet('NUnitXml')]
        [string] $OutputFormat = 'NUnitXml',

        # Disables the output Pester writes to screen. No other output is generated unless you specify PassThru, or one of the Output parameters.
        [Switch]$Quiet,

        [switch]$PassThru
    )
    begin {
        Import-LocalizedData -BindingVariable Script:ReportStrings -BaseDirectory $PesterRoot -FileName Gherkin.psd1

        # Make sure broken tests don't leave you in space:
        $Location = Get-Location
        $FileLocation = Get-Location -PSProvider FileSystem

        $script:GherkinSteps = @{}
        $script:GherkinHooks = @{
            BeforeAllFeatures = @()
            BeforeFeature = @()
            BeforeScenario = @()
            BeforeStep = @()
            AfterAllFeatures = @()
            AfterFeature = @()
            AfterScenario = @()
            AfterStep = @()
        }

    }
    end {

        if($PSCmdlet.ParameterSetName -eq "RetestFailed") {
            if((Test-Path variable:script:pester) -and $pester.FailedScenarios.Count -gt 0 ) {
                $ScenarioName = $Pester.FailedScenarios | Select-Object -Expand Name
            }
            else {
                throw "There's no existing failed tests to re-run"
            }
        }

        # Clear mocks
        $script:mockTable = @{}

        $pester = New-PesterState -TestNameFilter $ScenarioName -TagFilter @($Tag -split "\s+") -ExcludeTagFilter ($ExcludeTag -split "\s") -SessionState $PSCmdlet.SessionState -Strict:$Strict -Quiet:$Quiet |
            Add-Member -MemberType NoteProperty -Name Features -Value (New-Object System.Collections.Generic.List[Gherkin.Ast.Feature]) -PassThru |
            Add-Member -MemberType ScriptProperty -Name FailedScenarios -Value {
                $Names = $this.TestResult | Group Context | Where { $_.Group | Where { -not $_.Passed } } | Select-Object -Expand Name
                $this.Features.Scenarios | Where { $Names -contains $_.Name }
            } -PassThru |
            Add-Member -MemberType ScriptProperty -Name PassedScenarios -Value {
                $Names = $this.TestResult | Group Context | Where { -not ($_.Group | Where { -not $_.Passed }) } | Select-Object -Expand Name
                $this.Features.Scenarios | Where { $Names -contains $_.Name }
            } -PassThru

        Write-PesterStart $pester $Path

        Enter-CoverageAnalysis -CodeCoverage $CodeCoverage -PesterState $pester

        $parser = New-Object Gherkin.Parser
        $BeforeAllFeatures = $false
        foreach($FeatureFile in Get-ChildItem $Path -Filter "*.feature" -Recurse ) {

            # Remove all the steps
            $Script:GherkinSteps.Clear()
            # Import all the steps that are at the same level or a subdirectory
            $StepPath = Split-Path $FeatureFile.FullName
            $StepFiles = Get-ChildItem $StepPath -Filter "*.steps.ps1" -Recurse
            foreach($StepFile in $StepFiles){
                . $StepFile.FullName
            }
            Write-Verbose "Loaded $($Script:GherkinSteps.Count) step definitions from $(@($StepFiles).Count) steps file(s)"

            if(!$BeforeAllFeatures) {
                Invoke-GherkinHook BeforeAllFeatures
                $BeforeAllFeatures = $true
            }

            try {
                $Feature = $parser.Parse($FeatureFile.FullName).Feature | Convert-Tags
            } catch [Gherkin.ParserException] {
                Write-Warning "Skipped '$($FeatureFile.FullName)' because of parser errors:`n$(($_.Exception.Errors | Select-Object -Expand Message) -join "`n`n")"
                continue
            }

            $null = $Pester.Features.Add($Feature)

            ## This is Pesters "Describe" function
            $Pester.EnterTestGroup($Feature.Name, 'Describe')
            New-TestDrive

            Invoke-GherkinHook BeforeFeature $Feature.Name $Feature.Tags
            ## Hypothetically, we could add FEATURE setup/teardown?
            # Add-SetupAndTeardown -ScriptBlock $Fixture
            # Invoke-TestGroupSetupBlocks -Scope $pester.Scope

            $Background = $null
            $Scenarios = foreach($Scenario in $Feature.Children) {
                            switch($Scenario.Keyword.Trim())
                            {
                                "Scenario" {
                                    $Scenario = Convert-Tags -Input $Scenario -BaseTags $Feature.Tags
                                }
                                "Scenario Outline" {
                                    $Scenario = Convert-Tags -Input $Scenario -BaseTags $Feature.Tags
                                }
                                "Background" {
                                    $Background = Convert-Tags -Input $Scenario -BaseTags $Feature.Tags
                                    continue
                                }
                                default {
                                    Write-Warning "Unexpected Feature Child: $_"
                                }
                            }

                            if($Scenario.Examples) {
                                foreach($ExampleSet in $Scenario.Examples) {
                                    $Names = @($ExampleSet.TableHeader.Cells | Select -Expand Value)
                                    $NamesPattern = "<(?:" + ($Names -join "|") + ")>"
                                    $Steps = foreach($Example in $ExampleSet.TableBody) {
                                                foreach ($Step in $Scenario.Steps) {
                                                    [string]$StepText = $Step.Text
                                                    $StepArgument = $Step.Argument
                                                    if($StepText -match $NamesPattern) {
                                                        for($n = 0; $n -lt $Names.Length; $n++) {
                                                            $Name = $Names[$n]
                                                            if($Example.Cells[$n].Value -and $StepText -match "<${Name}>") {
                                                                $StepText = $StepText -replace "<${Name}>", $Example.Cells[$n].Value
                                                            }
                                                        }
                                                    }
                                                    if($StepText -ne $Step.Text) {
                                                        New-Object Gherkin.Ast.Step $Step.Location, $Step.Keyword.Trim(), $StepText, $Step.Argument
                                                    } else {
                                                        $Step
                                                    }
                                                }
                                            }
                                    $ScenarioName = $Scenario.Name
                                    if($ExampleSet.Name) {
                                        $ScenarioName = $ScenarioName + "`n Examples:" + $ExampleSet.Name.Trim()
                                    }
                                    New-Object Gherkin.Ast.Scenario $ExampleSet.Tags, $Scenario.Location, $Scenario.Keyword.Trim(), $ScenarioName, $Scenario.Description, $Steps | Convert-Tags $Scenario.Tags
                                }
                            } else {
                                $Scenario
                            }
                        }
            Add-Member -Input $Feature -Type NoteProperty -Name Scenarios -Value $Scenarios -Force

            # Move the test name filter first, since it'll likely return only a single item
            if($pester.TestNameFilter) {
                $Scenarios = foreach($nameFilter in $pester.TestNameFilter) {
                    $Scenarios | Where { $_.Name -like $NameFilter }
                }
                $Scenarios = $Scenarios | Get-Unique
            }

            # if($Pester.TagFilter -and @(Compare-Object $Tags $Pester.TagFilter -IncludeEqual -ExcludeDifferent).count -eq 0) {return}
            if($pester.TagFilter) {
                $Scenarios = $Scenarios | Where { Compare-Object $_.Tags $pester.TagFilter -IncludeEqual -ExcludeDifferent }
            }

            # if($Pester.ExcludeTagFilter -and @(Compare-Object $Tags $Pester.ExcludeTagFilter -IncludeEqual -ExcludeDifferent).count -gt 0) {return}
            if($Pester.ExcludeTagFilter) {
                $Scenarios = $Scenarios | Where { !(Compare-Object $_.Tags $Pester.ExcludeTagFilter -IncludeEqual -ExcludeDifferent) }
            }

            if($Scenarios -and !$Quiet) {
                Write-Describe $Feature
            }

            foreach($Scenario in $Scenarios) {
                # This is Pester's Context function
                $Pester.EnterTestGroup($Scenario.Name, 'Context')
                $TestDriveContent = Get-TestDriveChildItem

                Invoke-GherkinScenario $Pester $Scenario $Background -Quiet:$Quiet

                Clear-TestDrive -Exclude ($TestDriveContent | select -ExpandProperty FullName)
                # Exit-MockScope
                $Pester.LeaveTestGroup($Scenario.Name, 'Context')
            }

            ## This is Pesters "Describe" function again
            Invoke-GherkinHook AfterFeature $Feature.Name $Feature.Tags

            Remove-TestDrive
            ## Hypothetically, we could add FEATURE setup/teardown?
            # Clear-SetupAndTeardown
            Exit-MockScope
            $Pester.LeaveTestGroup($Feature.Name, 'Describe')
        }
        Invoke-GherkinHook AfterAllFeatures

        # Remove all the steps
        $Script:GherkinSteps.Clear()

        $Location | Set-Location
        [Environment]::CurrentDirectory = Convert-Path $FileLocation

        $pester | Write-PesterReport
        $coverageReport = Get-CoverageReport -PesterState $pester
        Write-CoverageReport -CoverageReport $coverageReport
        Exit-CoverageAnalysis -PesterState $pester

        if(Get-Variable -Name OutputFile -ValueOnly -ErrorAction $script:IgnoreErrorPreference) {
            Export-PesterResults -PesterState $pester -Path $OutputFile -Format $OutputFormat
        }

        if ($PassThru) {
            # Remove all runtime properties like current* and Scope
            $properties = @(
                "Path","TagFilter","TestNameFilter","TotalCount","PassedCount","FailedCount","Time","TestResult","PassedScenarios","FailedScenarios"

                if ($CodeCoverage)
                {
                    @{ Name = 'CodeCoverage'; Expression = { $coverageReport } }
                }
            )
            $pester | Select -Property $properties
        }
        if ($EnableExit) { Exit-WithCode -FailedCount $pester.FailedCount }
    }
}

function Invoke-GherkinScenario {
    [CmdletBinding()]
    param(
        $Pester, $Scenario, $Background, [Switch]$Quiet
    )

    if(!$Quiet) { Write-Context $Scenario }
    # If there's a background, we have to run that before the actual tests
    if($Background) {
        Invoke-GherkinScenario $Pester $Background -Quiet
    }

    Invoke-GherkinHook BeforeScenario $Scenario.Name $Scenario.Tags

    foreach($Step in $Scenario.Steps) {
        Invoke-GherkinStep $Pester $Step -Quiet:$Quiet
    }

    Invoke-GherkinHook AfterScenario $Scenario.Name $Scenario.Tags
}


function Invoke-GherkinStep {
    [CmdletBinding()]
    param (
        $Pester, $Step, [Switch]$Quiet
    )
    # Pick the match with the least grouping wildcards in it...
    $StepCommand = $(
        foreach($StepCommand in $Script:GherkinSteps.Keys) {
            if($Step.Text -match "^${StepCommand}$") {
                $StepCommand | Add-Member MatchCount $Matches.Count -PassThru
            }
        }
    ) | Sort MatchCount | Select -First 1
    $StepText = "{0} {1}" -f $Step.Keyword.Trim(), $Step.Text

    if(!$StepCommand) {
        $Pester.AddTestResult($StepText, "Skipped", $null, "Could not find test for step!", $null )
    } else {
        $NamedArguments, $Parameters = Get-StepParameters $Step $StepCommand

        $PesterException = $null
        $watch = New-Object System.Diagnostics.Stopwatch
        $watch.Start()
        try{
            Invoke-GherkinHook BeforeStep $Step.Text $Step.Tags

            if($NamedArguments.Count) {
                $ScriptBlock = { & $Script:GherkinSteps.$StepCommand @NamedArguments @Parameters }
            } else {
                $ScriptBlock = { & $Script:GherkinSteps.$StepCommand @Parameters }
            }
            # Set-ScriptBlockScope -ScriptBlock $scriptBlock -SessionState $PSCmdlet.SessionState
            $null = & $ScriptBlock

            Invoke-GherkinHook AfterStep $Step.Text $Step.Tags

            $Success = "Passed"
        } catch {
            $Success = "Failed"
            $PesterException = $_
        }

        $watch.Stop()

        $Pester.AddTestResult($StepText, $Success, $watch.Elapsed, $PesterException.Exception.Message, ($PesterException.ScriptStackTrace -split "`n")[1] )
    }

    if(!$Quiet) {
        $Pester.testresult[-1] | Write-PesterResult
    }
}

function Get-StepParameters {
    param($Step, $CommandName)
    $Null = $Step.Text -match $CommandName

    $NamedArguments = @{}
    $Parameters = @{}
    foreach($kv in $Matches.GetEnumerator()) {
        switch ($kv.Name -as [int]) {
            0       {  } # toss zero (where it matches the whole string)
            $null   { $NamedArguments.($kv.Name) = $ExecutionContext.InvokeCommand.ExpandString($kv.Value)       }
            default { $Parameters.([int]$kv.Name) = $ExecutionContext.InvokeCommand.ExpandString($kv.Value) }
        }
    }
    $Parameters = @($Parameters.GetEnumerator() | Sort Name | Select -Expand Value)

    # TODO: Convert parsed tables to tables....
    if($Step.Argument -is [Gherkin.Ast.DataTable]) {
        $NamedArguments.Table = $Step.Argument.Rows | ConvertTo-HashTableArray
    }
    if($Step.Argument -is [Gherkin.Ast.DocString]) {
        # trim empty matches if we're attaching DocStringArgument
        $Parameters = @( $Parameters | Where { $_.Length } ) + $Step.Argument.Content
    }

    return @($NamedArguments, $Parameters)
}

function Convert-Tags {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true)]
        $InputObject,

        [Parameter(Position=0)]
        [string[]]$BaseTags = @()
    )
    process {
        # Adapt the Gherkin .Tags property to the way we prefer it...
        [string[]]$Tags = foreach($tag in $InputObject.Tags){ $tag.Name.TrimStart("@") }
        Add-Member -Input $InputObject -Type NoteProperty -Name Tags -Value ([string[]]($Tags + $BaseTags)) -Force
        Write-Output $InputObject
    }
}

function ConvertTo-HashTableArray {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [Gherkin.Ast.TableRow[]]$InputObject
    )
    begin {
        $Names = @()
        $Table = @()
    }
    process {
        # Convert the first table row into headers:
        $Rows = @($InputObject)
        if(!$Names) {
            Write-Verbose "Reading Names from Header"
            $Header, $Rows = $Rows
            $Names = $Header.Cells | Select-Object -Expand Value
        }

        Write-Verbose "Processing $($Rows.Length) Rows"
        foreach($row in $Rows) {
            $result = @{}
            for($n = 0; $n -lt $Names.Length; $n++) {
                $result.Add($Names[$n], $row.Cells[$n].Value)
            }
            $Table += @($result)
        }
    }
    end {
        Write-Output $Table
    }
}