Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions azuredevops-pihole-pester-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ trigger:
include:
- main
- develop
# Pushes (i.e. merges into develop/main) only run when module or test code changed, so a
# docs/CI-only merge doesn't kick off the full ~1 hour suite. No path filter on the pr trigger
# below on purpose: PesterPiHoleShell is a required check, so it must always report - PR builds
# with nothing to test skip Pester inside the run step instead.
paths:
include:
- PiHoleShell/*
- tests/*

pr:
branches:
Expand Down Expand Up @@ -117,8 +125,44 @@ jobs:
Write-Host "Only additions in $file - no full-suite fallback needed"
}
}
elseif ($file -eq 'PiHoleShell/PiHoleShell.psd1') {
# Same rule as the .psm1 above, but the manifest is UTF-16 and marked
# -text in .gitattributes, so git can't give a usable text diff of it.
# Instead check out the target branch's copy and compare: new
# FunctionsToExport entries alone don't need the full suite, a removed
# export or any other manifest change does.
$baseDir = Join-Path ([System.IO.Path]::GetTempPath()) "psd1-base-$([guid]::NewGuid())"
New-Item -ItemType Directory -Path $baseDir | Out-Null
try {
git --work-tree="$baseDir" restore --source="origin/$targetBranch" --worktree -- $file
if ($LASTEXITCODE -ne 0) { throw "git restore failed for '$file'" }
$basePath = Join-Path $baseDir $file

$baseExports = (Import-PowerShellDataFile -Path $basePath).FunctionsToExport
$headExports = (Import-PowerShellDataFile -Path $file).FunctionsToExport
$trulyRemoved = $baseExports | Where-Object { $_ -notin $headExports }

$exportBlock = '(?s)FunctionsToExport\s*=\s*@\(.*?\)'
$baseRest = (Get-Content -Path $basePath -Raw) -replace $exportBlock, ''
$headRest = (Get-Content -Path $file -Raw) -replace $exportBlock, ''
}
finally {
Remove-Item -Path $baseDir -Recurse -Force -ErrorAction SilentlyContinue
}

if ($trulyRemoved) {
Write-Host "Export(s) removed from $file ($($trulyRemoved -join ', ')) - running the full suite"
$runFullSuite = $true
}
elseif ($baseRest -ne $headRest) {
Write-Host "Manifest settings changed in $file - running the full suite"
$runFullSuite = $true
}
else {
Write-Host "Only additions in $file - no full-suite fallback needed"
}
}
elseif ($file -match '^PiHoleShell/Private/' -or
$file -eq 'PiHoleShell/PiHoleShell.psd1' -or
$file -eq 'PiHoleShell/Public/Authentication/Request-PiHoleAuth.ps1' -or
$file -eq 'tests/Initialize-PiHoleTestData.ps1' -or
$file -eq 'tests/IntegrationConfig.example.ps1') {
Expand Down Expand Up @@ -158,8 +202,16 @@ jobs:
$config.TestResult.OutputFormat = 'NUnitXml'
$config.TestResult.OutputPath = '$(testResultsFile)'

$testPaths = $testPaths | Select-Object -Unique
if ($runFullSuite -or $testPaths.Count -eq 0) {
$testPaths = @($testPaths | Select-Object -Unique)
if (-not $runFullSuite -and $testPaths.Count -eq 0) {
# A PR that touches no function, shared code or test (docs, workflows, tools/) has
# nothing to test - pass quickly so the required check doesn't hold it up for an hour.
Write-Host "No module or test files changed - skipping Pester"
Write-Host "##vso[task.setvariable variable=testsSkipped]true"
exit 0
}

if ($runFullSuite) {
Write-Host "Running the full test suite"
$config.Run.Path = './tests'
}
Expand Down Expand Up @@ -197,7 +249,7 @@ jobs:

- task: PublishTestResults@2
displayName: 'Publish test results'
condition: succeededOrFailed()
condition: and(succeededOrFailed(), ne(variables['testsSkipped'], 'true'))
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '$(testResultsFile)'
Expand Down
Loading