src/test/Invoke-TestSuiteRunner.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
using module .\..\module\manifest\AutoUpdateSemVerDelegate.ps1

function Invoke-TestSuiteRunner {
    [CmdletBinding(PositionalBinding = $true, 
        DefaultParameterSetName = "ByPath")]
    Param
    (
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $false, 
            ParameterSetName = "ByPath")]
        [string]$Path = '.',

        [Parameter(Mandatory = $false,
            Position = 1,
            ValueFromPipeline = $false)]
        [string]$TestFolderPath = 'test',

        [Parameter(Mandatory = $false,
            Position = 2,
            ValueFromPipeline = $false)]
        [string[]]$Exclude = 'mocks',

        [Parameter(Mandatory = $false,
            Position = 3,
            ValueFromPipeline = $false)]
        [Pester.OutputTypes[]]$Show = @('Header', 'Fails'),

        [switch]$PassThru
    )

    DynamicParam {
        return GetModuleNameSet -Position 0 -Mandatory 
    }
    
    begin {
        $Name = $PSBoundParameters['Name']

        if ($Name) {
            $ModInfo = Get-MKModuleInfo -Name $Name
        }
        else {
            $ModInfo = Get-MKModuleInfo -Path $Path
        }

        AutoUpdateSemVerDelegate($ModInfo.Path)

        Push-Location -StackName 'PriorTestLocation'
        Set-Location $ModInfo.Path
        
        $TestPath = Join-Path -Path $ModInfo.Path -ChildPath $TestFolderPath -Resolve
        $ChildTestPaths = Get-ChildItem $TestPath -Exclude $Exclude -Directory | `
            Select-Object -ExpandProperty Name | `
            ForEach-Object {Join-Path -Path $TestPath -ChildPath $_}
            
        # FYI: https://github.com/pester/Pester/wiki/Invoke-Pester
        $ArgList = @{ Script = $ChildTestPaths; Show = $Show; PassThru = $PassThru.IsPresent }
    }

    process {
        Write-Host "PowerBolt is now testing in: $TestPath" -ForegroundColor Green
        Start-Job -Name "JobPester" -ScriptBlock {
            param($AL) Invoke-Pester @AL
        } -ArgumentList $ArgList | Wait-Job -Force | ForEach-Object {Receive-Job -Name JobPester}
    }

    end {
        Import-Module ($ModInfo.Path)
        
        Pop-Location -StackName 'PriorTestLocation'
    }
}