|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require("child_process"); |
| 4 | +const fs = require("fs"); |
| 5 | + |
| 6 | +// These are the directories we want to check for correct naming |
| 7 | +const TEST_DIRS = ["tests/unit", "tests/integration", "tests/tck", "tests/fuzz"]; |
| 8 | + |
| 9 | +// These are the excluded paths and file names inside the TEST_DIRS |
| 10 | +const IGNORED = ["tests/fuzz/support"]; |
| 11 | +const EXCEPTIONS = ["conftest.py", "__init__.py", "init.py", "mock_server.py", "utils.py"]; |
| 12 | + |
| 13 | +// Collect naming errors to report at the end |
| 14 | +const nameErrors = []; |
| 15 | + |
| 16 | +// Use "git ls-files" to get all tracked files in the repository |
| 17 | +const output = execSync("git ls-files", { encoding: "utf-8" }); |
| 18 | +// Split output into lines and filter out empty lines for easy manipulation |
| 19 | +// e.g. output = "tests/unit/my_test.py\ntests/integration/other_test.py" |
| 20 | +const files = output.split("\n").filter(Boolean); |
| 21 | + |
| 22 | +for (const file of files) { |
| 23 | + // --- PATH FILTERING --- |
| 24 | + // Skip files that are not in any of the specified test directories |
| 25 | + if (!TEST_DIRS.some(dir => file.startsWith(dir))) continue; |
| 26 | + |
| 27 | + // Skip ignored paths (e.g. helpers) |
| 28 | + if (IGNORED.some(path => file.startsWith(path))) continue; |
| 29 | + |
| 30 | + // Now we have file paths that we need to check for correct naming |
| 31 | + // e.g. file = "tests/unit/my_test.py" |
| 32 | + |
| 33 | + // --- Correct PATH now apply NAMING checks --- |
| 34 | + |
| 35 | + // Extract the file name from the path |
| 36 | + // e.g. from file = "tests/unit/my_test.py" get name = "my_test.py" |
| 37 | + const name = file.split("/").pop(); |
| 38 | + |
| 39 | + // Skip allowed special files |
| 40 | + if (EXCEPTIONS.includes(name)) continue; |
| 41 | + |
| 42 | + // Enforce naming rule on files |
| 43 | + if (!name.endsWith("_test.py")) { |
| 44 | + console.error(`Invalid test file name: ${file}`); |
| 45 | + console.error(`::error file=${file}::Must end with '_test.py'`); |
| 46 | + nameErrors.push(file); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Generate a summary of the results for the GitHub Actions UI |
| 51 | +const summaryPath = process.env.GITHUB_STEP_SUMMARY; |
| 52 | + |
| 53 | +if (summaryPath) { |
| 54 | + let summary = `## 🧪 Test File Naming Check\n\n`; |
| 55 | + |
| 56 | + if (nameErrors.length === 0) { |
| 57 | + summary += `✅ All test files are correctly named\n`; |
| 58 | + } else { |
| 59 | + // Counts and lists all the incorrectly named test files |
| 60 | + summary += `❌ Found ${nameErrors.length} incorrectly named test files:\n\n`; |
| 61 | + nameErrors.forEach(f => { |
| 62 | + summary += `- \`${f}\`\n`; |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + fs.appendFileSync(summaryPath, summary); |
| 67 | +} |
| 68 | + |
| 69 | +// Fail job if needed |
| 70 | +if (nameErrors.length > 0) { |
| 71 | + process.exit(1); |
| 72 | +} |
0 commit comments