From bddcc7400ae731123c31ccb22b34e1cde69bb0a9 Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Wed, 17 Jun 2026 17:27:35 +1000 Subject: [PATCH] Harden affected test selection for examples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- eng/testing/select-affected-tests.cs | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/eng/testing/select-affected-tests.cs b/eng/testing/select-affected-tests.cs index 412c9ccc0..3a1d52026 100644 --- a/eng/testing/select-affected-tests.cs +++ b/eng/testing/select-affected-tests.cs @@ -172,6 +172,13 @@ public static async Task MainAsync(string[] args) continue; } + if (TryGetExampleTests(filePath, projectPaths, nodeToTests, out var exampleName, out var impactedExampleTests)) + { + selected.UnionWith(impactedExampleTests); + reasons.Add($"Selected {impactedExampleTests.Count} tests because {filePath} belongs to example {exampleName}."); + continue; + } + if (filePath.StartsWith("src/", StringComparison.Ordinal) || filePath.StartsWith("tests/", StringComparison.Ordinal) || filePath.StartsWith("examples/", StringComparison.Ordinal) || @@ -571,6 +578,42 @@ private static bool TryGetTypeScriptAppHostTests( return false; } + private static bool TryGetExampleTests( + string filePath, + List projectPaths, + Dictionary> nodeToTests, + out string exampleName, + out HashSet tests) + { + tests = []; + exampleName = string.Empty; + + if (!filePath.StartsWith("examples/", StringComparison.Ordinal)) + { + return false; + } + + var segments = filePath.Split('/', StringSplitOptions.RemoveEmptyEntries); + if (segments.Length < 2) + { + return false; + } + + exampleName = segments[1]; + var examplePrefix = $"examples/{exampleName}/"; + var exampleProjects = projectPaths.Where(projectPath => projectPath.StartsWith(examplePrefix, StringComparison.Ordinal)); + + foreach (var projectPath in exampleProjects) + { + if (nodeToTests.TryGetValue(projectPath, out var impacted)) + { + tests.UnionWith(impacted); + } + } + + return tests.Count > 0; + } + private static Dictionary ParseStringConstants(string contents) { var constants = new Dictionary(StringComparer.Ordinal);