Tests/Pester.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
param (
    $Show = "None"
)

Write-Host "Starting Tests" -ForegroundColor Green
# if ($env:BUILD_BUILDURI -like "vstfs*" -or $env:TRAVIS -eq 'true') {
    Write-Host "Installing Pester" -ForegroundColor Cyan
    Install-Module Pester -Force -SkipPublisherCheck
    Write-Host "Installing PSFramework" -ForegroundColor Cyan
    Install-Module PSFramework -Force -SkipPublisherCheck
    Write-Host "Installing dbatools" -ForegroundColor Cyan
    Install-Module dbatools -Force -SkipPublisherCheck
    Write-Host "Installing PSScriptAnalyzer" -ForegroundColor Cyan
    Install-Module PSScriptAnalyzer -Force -SkipPublisherCheck
    Import-Module dbatools
    Import-Module Pester
    Import-Module PsFramework
    Import-Module PSScriptAnalyzer
# }

Write-Host "Loading constants"
. "$PSScriptRoot\constants.ps1"

Write-Host "Building Test Scenarios"
#instance slow to start mssql, so:
Start-Sleep -Seconds 30
if ($script:IgnoreSQLCMD) {
    try {
        $error.clear
        $srv = Connect-DbaInstance -SqlInstance $script:appvSqlInstance -SqlCredential $script:appvSqlCredential
    }
    catch {
        foreach ($e in $error) {
            $e | Select-Object * 
        }
    }
    ForEach ($file in (Get-ChildItem "$PSScriptRoot\scenarios" -File -Filter "*.sql" -recurse)) {
        Write-Host "Setting up $($file.name)"
        $c = Get-Content $file.FullName -Raw
        $srv.Databases['master'].ExecuteNonQuery($c)
        # (& sqlcmd -S "$sqlInstance" -U "sa" -P "Password12!" -b -i "$($file.fullname)" -d "master")
    }
} else {
    ForEach ($file in (Get-ChildItem "$PSScriptRoot\scenarios" -File -Filter "*.sql" -recurse)){
        (& sqlcmd -S "$script:appvSqlInstance" -U "sa" -P "Password12!" -b -i "$($file.fullname)" -d "master")
    }
}
Write-Host "Importing dbaSecurityScans"
Import-Module "$PSScriptRoot\..\dbaSecurityScan.psd1"

$totalFailed = 0
$totalRun = 0

$testresults = @()
Write-Host "Running individual tests"
foreach ($file in (Get-ChildItem "$PSScriptRoot" -File -Filter "*.Tests.ps1" -Recurse)) {
    Write-Host "Executing $($file.Name)"
    $shortName = $file.BaseName.Substring(0,$file.BaseName.Length-6)
    $testResultsFile = ".\TestsResults-$ShortName.xml"
    $results = Invoke-Pester -Script $file.FullName -Show None -PassThru -OutputFormat NUnitXml -OutputFile $testResultsFile
    foreach ($result in $results) {
        $totalRun += $result.TotalCount
        $totalFailed += $result.FailedCount
        # $result.TestResult | Where-Object { -not $_.Passed } | ForEach-Object {
        $result.TestResult | Where-Object { $true } | ForEach-Object {
            $name = $_.Name
            $testresults += [pscustomobject]@{
                Describe = $_.Describe
                Context  = $_.Context
                Name     = "It $name"
                Result   = $_.Result
                Message  = $_.FailureMessage
            }
        }
    }
}

$testresults | Sort-Object Describe, Context, Name, Result, Message | Format-List
if ($null -ne $env:APPVEYOR_JOB_ID){
    (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $testResultsFile))
}
if ($totalFailed -gt 0) {
    throw "$totalFailed / $totalRun tests failed"
}