From 8d34d0bc5903af69619c3cd5cb54f27cc8fabeda Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Sat, 31 Oct 2026 10:29:38 -0500 Subject: [PATCH] ci: skip Pester when a PR changes no module or test code A PR whose changes map to no test file (docs, workflows, tools/) fell through to the full ~1 hour suite. It now passes the required PesterPiHoleShell check in seconds instead. Merges into develop/main only trigger the pipeline when PiHoleShell/ or tests/ changed. Adding a function also ran the full suite, because any change to the UTF-16 manifest counted as a core change. The manifest is now compared the same way as the .psm1: new FunctionsToExport entries only run the changed function's tests, while a removed export or any other manifest change still runs everything. Co-Authored-By: Claude Opus 5.5 --- azuredevops-pihole-pester-tests.yml | 60 +++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/azuredevops-pihole-pester-tests.yml b/azuredevops-pihole-pester-tests.yml index 51c5219..5bfcdb7 100644 --- a/azuredevops-pihole-pester-tests.yml +++ b/azuredevops-pihole-pester-tests.yml @@ -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: @@ -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') { @@ -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' } @@ -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)'