tasks/testing.tasks.ps1

# Control flags
$EnableCoverage = $true

# Options
$ReportGeneratorToolVersion = "4.8.3"

# Synopsis: Run .NET solution tests
task RunTests -If {$SolutionToBuild} {
    exec { 
        dotnet test $SolutionToBuild `
                    --configuration $Configuration `
                    --no-build `
                    --no-restore `
                    /p:CollectCoverage="$EnableCoverage" `
                    /p:CoverletOutputFormat=cobertura `
                    /p:ExcludeByFile="$($ExcludeFilesFromCodeCoverage.Replace(",","%2C"))" `
                    --verbosity $LogLevel
    }
}

# Synopsis: Generate test report file
task GenerateTestReport {
    Install-DotNetTool -Name "dotnet-reportgenerator-globaltool" -Version $ReportGeneratorToolVersion
    exec {
        reportgenerator "-reports:$SourcesDir/**/**/coverage.cobertura.xml" `
                        "-targetdir:$CoverageDir" `
                        "-reporttypes:$TestReportTypes"
    }
}

# Synopsis: Runs all the available Pester tests
task RunPesterTests -If {!$SkipPesterTests -and $PesterTestsDir} {
    [array]$existingModule = Get-Module -ListAvailable Pester
    if (!$existingModule -or ($existingModule.Version -notcontains $pesterVer)) {
        Install-Module Pester -RequiredVersion $pesterVer -Force -Scope CurrentUser -SkipPublisherCheck
    }
    Import-Module Pester

    if (!$PesterOutputFormat) { $PesterOutputFormat = "NUnitXml" }
    if (!$PesterOutputFilePath) { $PesterOutputFilePath = "_buildOutputs/PesterTestResults.xml" }

    $results = Invoke-Pester -Path $PesterTestsDir `
                             -OutputFormat $PesterOutputFormat `
                             -OutputFile $PesterOutputFilePath `
                             -PassThru `
                             -Show Summary,Fails

    if ($results.FailedCount -gt 0) {
        throw ("{0} out of {1} tests failed - check previous logging for more details" -f $results.FailedCount, $results.TotalCount)
    }
}