Files

126 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

2023-01-09 19:02:19 -05:00
import * as fs from "fs";
import * as path from "path";
import * as YAML from "yaml";
import {convertWorkflowTemplate} from "./model/convert";
2023-03-17 11:17:18 -04:00
import {NoOperationTraceWriter} from "./templates/trace-writer";
2023-02-06 15:10:34 -05:00
import {File} from "./workflows/file";
import {FileProvider} from "./workflows/file-provider";
import {fileIdentifier, FileReference} from "./workflows/file-reference";
2023-01-09 19:02:19 -05:00
import {parseWorkflow} from "./workflows/workflow-parser";
interface TestOptions {
2023-01-09 19:02:19 -05:00
"include-source"?: boolean;
skip?: string[];
}
2023-03-17 11:17:18 -04:00
const nullTrace = new NoOperationTraceWriter();
2023-01-09 19:02:19 -05:00
const testFiles = "./testdata/reader";
2023-02-17 18:36:00 -05:00
const skippedTestsFile = "./testdata/skipped-tests.txt";
describe("x-lang tests", () => {
2023-01-09 19:02:19 -05:00
const files = fs.readdirSync(testFiles);
2023-02-17 18:36:00 -05:00
const skippedTests = new Set(fs.readFileSync(skippedTestsFile, "utf8").split(/\n/));
for (const file of files) {
2023-01-09 19:02:19 -05:00
const fileName = path.join(testFiles, file);
2023-01-09 19:02:19 -05:00
const fileStat = fs.statSync(fileName);
if (fileStat.isDirectory()) {
2023-01-09 19:02:19 -05:00
throw new Error("sub-directories are not supported");
}
2023-01-09 19:02:19 -05:00
if (path.extname(fileName) !== ".yml" && path.extname(fileName) !== ".yaml") {
throw new Error("only y(a)ml files are supported " + file);
}
2023-01-09 19:02:19 -05:00
const inputFile = fs.readFileSync(fileName, "utf8");
2023-01-09 19:02:19 -05:00
const testDocs: string[] = inputFile.split(/\r?\n---\r?\n/);
expect(testDocs.length).toBeGreaterThanOrEqual(3);
2023-03-17 11:17:18 -04:00
const testOptions = YAML.parse(testDocs[0]) as TestOptions;
2023-02-17 18:36:00 -05:00
const unsupportedTest = skippedTests.has(file);
2023-02-06 15:10:34 -05:00
const test = async () => {
const testFileName = ".github/workflows" + fileName.substring(fileName.lastIndexOf("/"));
const testInput = testDocs[1];
const expectedTemplate = testDocs[testDocs.length - 1].trim();
// For reusable workflow tests, additional workflows are passed in as pairs of
// file names and file contents
const reusableWorkflows: Record<string, File> = {};
if (fileName.indexOf("reusable") !== -1) {
2023-02-06 15:10:34 -05:00
for (let i = 2; i < testDocs.length - 1; i = i + 2) {
reusableWorkflows[testDocs[i]] = {
name: testDocs[i],
content: testDocs[i + 1]
};
}
}
2023-02-06 15:10:34 -05:00
const testFileProvider: FileProvider = {
2023-03-17 11:17:18 -04:00
getFileContent: (ref: FileReference) => {
const file = reusableWorkflows[fileIdentifier(ref)];
2023-02-06 15:10:34 -05:00
if (file) {
2023-03-17 11:17:18 -04:00
return Promise.resolve(file);
2023-02-06 15:10:34 -05:00
}
throw new Error("File not found: " + fileName);
}
};
const parseResult = parseWorkflow(
2023-02-06 11:15:15 -05:00
{
name: testFileName,
content: testInput
},
nullTrace
2023-01-09 19:02:19 -05:00
);
2023-01-09 19:02:19 -05:00
expect(parseResult.value).not.toBeUndefined();
2023-02-06 15:10:34 -05:00
const workflowTemplate = await convertWorkflowTemplate(
parseResult.context,
2023-03-17 11:17:18 -04:00
parseResult.value!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
2023-02-06 15:10:34 -05:00
testFileProvider,
{
fetchReusableWorkflowDepth: 1
}
);
// Unless this tests is only used by TypeScript, remove the events for now.
// TODO: Remove this once we parse events everywhere
const includeEvents =
2023-01-09 19:02:19 -05:00
testOptions.skip !== undefined && contains(testOptions.skip, "Go") && contains(testOptions.skip, "C#");
if (!includeEvents) {
2023-03-17 11:17:18 -04:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
2023-01-09 19:02:19 -05:00
delete (workflowTemplate as any).events;
}
// Other parsers don't have a partial template when there are errors
if (workflowTemplate.errors) {
2023-03-17 11:17:18 -04:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
2023-01-09 19:02:19 -05:00
delete (workflowTemplate as any).jobs;
}
2023-01-09 19:02:19 -05:00
const result = JSON.stringify(workflowTemplate, null, " ");
expect(result).toBe(expectedTemplate);
};
if (unsupportedTest) {
2023-01-09 19:02:19 -05:00
it.failing(fileName, test);
} else {
2023-01-09 19:02:19 -05:00
it(fileName, test);
}
}
2023-01-09 19:02:19 -05:00
});
// Case-insensitive contains
function contains(arr: string[] | undefined, term: string): boolean {
if (!arr) {
2023-01-09 19:02:19 -05:00
return false;
}
2023-01-09 19:02:19 -05:00
return arr.map(x => x.toLowerCase()).indexOf(term.toLowerCase()) !== -1;
}