|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +// Registry of non-smoke regression specs selectable from changed paths. |
| 5 | +const ALL_REGRESSION_TESTS = [ |
| 6 | + "tests/ui/site-e2e.spec.js", |
| 7 | + "tests/ui/worker-node.spec.js", |
| 8 | +]; |
| 9 | + |
| 10 | +const WORKER_NODE_PATTERNS = [ |
| 11 | + /^controllers\/machine(_node_deploy)?\.go$/, |
| 12 | + /^object\/machine(_node_deploy)?\.go$/, |
| 13 | + /^web\/src\/Machine(ListPage|EditPage|NodeDeployPanel)\.js$/, |
| 14 | + /^web\/src\/backend\/Machine(NodeDeploy)?Backend\.js$/, |
| 15 | + /^web\/tests\/ui\/worker-node\.spec\.js$/, |
| 16 | +]; |
| 17 | + |
| 18 | +const SMOKE_COVERED_PATTERNS = [ |
| 19 | + /^web\/src\/SiteEditPage\.js$/, |
| 20 | +]; |
| 21 | + |
| 22 | +const SITE_PATTERNS = [ |
| 23 | + /^controllers\/site\.go$/, |
| 24 | + /^object\/site\.go$/, |
| 25 | + /^web\/src\/SiteListPage\.js$/, |
| 26 | + /^web\/src\/backend\/SiteBackend\.js$/, |
| 27 | + /^web\/tests\/ui\/site-e2e\.spec\.js$/, |
| 28 | +]; |
| 29 | + |
| 30 | +const FULL_REGRESSION_PATTERNS = [ |
| 31 | + /^\.github\/workflows\//, |
| 32 | + /^conf\/app\.conf$/, |
| 33 | + /^routers\/router\.go$/, |
| 34 | + /^web\/package\.json$/, |
| 35 | + /^web\/playwright\.config\.js$/, |
| 36 | + /^web\/src\/(Conf|Setting)\.js$/, |
| 37 | + /^web\/src\/locales\//, |
| 38 | + /^web\/tests\/ui\/e2e-helpers\.js$/, |
| 39 | + /^web\/yarn\.lock$/, |
| 40 | +]; |
| 41 | + |
| 42 | +const DOCS_ONLY_PATTERNS = [ |
| 43 | + /(^|\/)(README|CHANGELOG|LICENSE)(\.[^/]*)?$/i, |
| 44 | + /\.md$/i, |
| 45 | + /^docs\//, |
| 46 | +]; |
| 47 | + |
| 48 | +const CODE_ROOT_PATTERNS = [ |
| 49 | + /^conf\//, |
| 50 | + /^controllers\//, |
| 51 | + /^main\.go$/, |
| 52 | + /^object\//, |
| 53 | + /^proxy\//, |
| 54 | + /^routers\//, |
| 55 | + /^web\/scripts\//, |
| 56 | + /^web\/src\//, |
| 57 | +]; |
| 58 | + |
| 59 | +function normalizeChangedPath(filePath) { |
| 60 | + return String(filePath || "") |
| 61 | + .trim() |
| 62 | + .replace(/\\/g, "/") |
| 63 | + .replace(/^\.\//, ""); |
| 64 | +} |
| 65 | + |
| 66 | +function matchesAny(filePath, patterns) { |
| 67 | + return patterns.some(pattern => pattern.test(filePath)); |
| 68 | +} |
| 69 | + |
| 70 | +function isCodePath(filePath) { |
| 71 | + return matchesAny(filePath, CODE_ROOT_PATTERNS); |
| 72 | +} |
| 73 | + |
| 74 | +function normalizeChangedFiles(changedFiles) { |
| 75 | + if (!Array.isArray(changedFiles)) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + return Array.from(new Set( |
| 79 | + changedFiles.map(normalizeChangedPath).filter(Boolean) |
| 80 | + )); |
| 81 | +} |
| 82 | + |
| 83 | +function selectRegressionTestsFromNormalized(normalizedFiles) { |
| 84 | + if (normalizedFiles.length === 0) { |
| 85 | + return [...ALL_REGRESSION_TESTS]; |
| 86 | + } |
| 87 | + |
| 88 | + const selectedTests = new Set(); |
| 89 | + let runAllRegression = false; |
| 90 | + |
| 91 | + // Ordering matters: skip docs, honor all-regression triggers, then apply targeted and smoke-covered matches. |
| 92 | + for (const filePath of normalizedFiles) { |
| 93 | + if (matchesAny(filePath, DOCS_ONLY_PATTERNS)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + if (matchesAny(filePath, FULL_REGRESSION_PATTERNS)) { |
| 97 | + runAllRegression = true; |
| 98 | + continue; |
| 99 | + } |
| 100 | + if (matchesAny(filePath, WORKER_NODE_PATTERNS)) { |
| 101 | + selectedTests.add("tests/ui/worker-node.spec.js"); |
| 102 | + continue; |
| 103 | + } |
| 104 | + if (matchesAny(filePath, SITE_PATTERNS)) { |
| 105 | + selectedTests.add("tests/ui/site-e2e.spec.js"); |
| 106 | + continue; |
| 107 | + } |
| 108 | + if (matchesAny(filePath, SMOKE_COVERED_PATTERNS)) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + if (isCodePath(filePath)) { |
| 112 | + runAllRegression = true; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (runAllRegression) { |
| 117 | + return [...ALL_REGRESSION_TESTS]; |
| 118 | + } |
| 119 | + |
| 120 | + return ALL_REGRESSION_TESTS.filter(testFile => selectedTests.has(testFile)); |
| 121 | +} |
| 122 | + |
| 123 | +// Selects non-smoke UI regression specs for repository-relative changed paths. |
| 124 | +function selectRegressionTests(changedFiles) { |
| 125 | + return selectRegressionTestsFromNormalized(normalizeChangedFiles(changedFiles)); |
| 126 | +} |
| 127 | + |
| 128 | +function main(argv) { |
| 129 | + const changedFilesPath = argv[2]; |
| 130 | + if (!changedFilesPath) { |
| 131 | + process.stderr.write("Usage: node scripts/select-ui-tests.js <changed-files.txt>\n"); |
| 132 | + process.exitCode = 1; |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + const repoRoot = path.resolve(__dirname, "..", ".."); |
| 137 | + let resolvedChangedFilesPath; |
| 138 | + try { |
| 139 | + resolvedChangedFilesPath = fs.realpathSync(path.resolve(changedFilesPath)); |
| 140 | + } catch (error) { |
| 141 | + process.stderr.write(`Error reading changed files list: ${error.message}\n`); |
| 142 | + process.exitCode = 1; |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + const changedFilesPathRelative = path.relative(repoRoot, resolvedChangedFilesPath); |
| 147 | + if (changedFilesPathRelative.startsWith("..")) { |
| 148 | + process.stderr.write(`Error: changed files path is outside the repository: ${changedFilesPath}\n`); |
| 149 | + process.exitCode = 1; |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + let rawChangedFiles; |
| 154 | + try { |
| 155 | + rawChangedFiles = fs.readFileSync(resolvedChangedFilesPath, "utf8"); |
| 156 | + } catch (error) { |
| 157 | + process.stderr.write(`Error reading changed files list: ${error.message}\n`); |
| 158 | + process.exitCode = 1; |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + const changedFiles = rawChangedFiles.split(/\r?\n/); |
| 163 | + const normalizedFiles = normalizeChangedFiles(changedFiles); |
| 164 | + if (normalizedFiles.length === 0) { |
| 165 | + process.stderr.write("Warning: no changed files detected; falling back to all regression tests.\n"); |
| 166 | + } |
| 167 | + const tests = selectRegressionTestsFromNormalized(normalizedFiles); |
| 168 | + process.stdout.write(tests.length > 0 ? `${tests.join("\n")}\n` : ""); |
| 169 | +} |
| 170 | + |
| 171 | +if (require.main === module) { |
| 172 | + main(process.argv); |
| 173 | +} |
| 174 | + |
| 175 | +module.exports = { |
| 176 | + ALL_REGRESSION_TESTS, |
| 177 | + selectRegressionTests, |
| 178 | +}; |
0 commit comments