|
| 1 | +import * as path from "path"; |
| 2 | +import {fileURLToPath} from "url"; |
| 3 | +import {loadTestCases, runTestCase} from "./runner"; |
| 4 | +import {ValidationConfig} from "../../validate"; |
| 5 | +import {ActionMetadata, ActionReference} from "../../action"; |
| 6 | +import {clearCache} from "../../utils/workflow-cache"; |
| 7 | + |
| 8 | +// ESM-compatible __dirname |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | + |
| 12 | +// Mock action metadata provider for tests |
| 13 | +const validationConfig: ValidationConfig = { |
| 14 | + actionsMetadataProvider: { |
| 15 | + fetchActionMetadata: (ref: ActionReference): Promise<ActionMetadata | undefined> => { |
| 16 | + const key = `${ref.owner}/${ref.name}@${ref.ref}`; |
| 17 | + |
| 18 | + const metadata: Record<string, ActionMetadata> = { |
| 19 | + "actions/cache@v1": { |
| 20 | + name: "Cache", |
| 21 | + description: "Cache dependencies", |
| 22 | + inputs: { |
| 23 | + path: { |
| 24 | + description: "A list of files to cache", |
| 25 | + required: true |
| 26 | + }, |
| 27 | + key: { |
| 28 | + description: "Cache key", |
| 29 | + required: true |
| 30 | + }, |
| 31 | + "restore-keys": { |
| 32 | + description: "Restore keys", |
| 33 | + required: false |
| 34 | + } |
| 35 | + } |
| 36 | + }, |
| 37 | + "actions/setup-node@v3": { |
| 38 | + name: "Setup Node", |
| 39 | + description: "Setup Node.js", |
| 40 | + inputs: { |
| 41 | + "node-version": { |
| 42 | + description: "Node version", |
| 43 | + required: true, |
| 44 | + default: "16" |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + return Promise.resolve(metadata[key]); |
| 51 | + } |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +// Point to the source testdata directory |
| 56 | +const testdataDir = path.join(__dirname, "testdata"); |
| 57 | + |
| 58 | +beforeEach(() => { |
| 59 | + clearCache(); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("code action golden tests", () => { |
| 63 | + const testCases = loadTestCases(testdataDir); |
| 64 | + |
| 65 | + if (testCases.length === 0) { |
| 66 | + it.todo("no test cases found - add .yml files to testdata/"); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + for (const testCase of testCases) { |
| 71 | + it(testCase.name, async () => { |
| 72 | + const result = await runTestCase(testCase, validationConfig); |
| 73 | + |
| 74 | + if (!result.passed) { |
| 75 | + let errorMessage = result.error || "Test failed"; |
| 76 | + |
| 77 | + if (result.expected !== undefined && result.actual !== undefined) { |
| 78 | + errorMessage += "\n\n"; |
| 79 | + errorMessage += "=== EXPECTED (golden file) ===\n"; |
| 80 | + errorMessage += result.expected; |
| 81 | + errorMessage += "\n\n"; |
| 82 | + errorMessage += "=== ACTUAL ===\n"; |
| 83 | + errorMessage += result.actual; |
| 84 | + } |
| 85 | + |
| 86 | + throw new Error(errorMessage); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | +}); |
0 commit comments