Files
languageservices/workflow-parser/src/index.test.ts
T

99 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-01-09 19:02:19 -05:00
import {TemplateValidationError} from "./templates/template-validation-error";
import {nullTrace} from "./test-utils/null-trace";
import {parseWorkflow} from "./workflows/workflow-parser";
describe("parseWorkflow", () => {
it("parses valid workflow", () => {
const result = parseWorkflow(
2023-02-06 11:15:15 -05:00
{
name: "test.yaml",
content: "on: push\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - run: echo 'hello'"
},
nullTrace
2023-01-09 19:02:19 -05:00
);
2023-01-09 19:02:19 -05:00
expect(result.context.errors.getErrors()).toHaveLength(0);
});
it("contains range for error", () => {
const result = parseWorkflow(
2023-02-06 11:15:15 -05:00
{
name: "test.yaml",
content: "on: push\njobs:\n build:\n steps:\n - run: echo 'hello'"
},
nullTrace
2023-01-09 19:02:19 -05:00
);
expect(result.context.errors.getErrors()).toEqual([
2023-01-09 19:02:19 -05:00
new TemplateValidationError("Required property is missing: runs-on", "test.yaml (Line: 4, Col: 5)", undefined, {
2023-01-27 15:54:51 -05:00
start: {line: 4, column: 5},
end: {line: 5, column: 24}
2023-01-09 19:02:19 -05:00
})
]);
});
it("error range for expression is constrained to scalar node", () => {
const result = parseWorkflow(
2023-02-06 11:15:15 -05:00
{
name: "test.yaml",
content: `on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: \${{ github.event = 12 }}
2023-01-09 19:02:19 -05:00
run: echo 'hello'`
2023-02-06 11:15:15 -05:00
},
nullTrace
2023-01-09 19:02:19 -05:00
);
expect(result.context.errors.getErrors()).toEqual([
new TemplateValidationError(
"Unexpected symbol: '='. Located at position 14 within expression: github.event = 12",
"test.yaml (Line: 6, Col: 13)",
undefined,
{
2023-01-27 15:54:51 -05:00
start: {line: 6, column: 13},
end: {line: 6, column: 37}
}
2023-01-09 19:02:19 -05:00
)
]);
});
it("tokens contain descriptions", () => {
const result = parseWorkflow(
2023-02-06 11:15:15 -05:00
{
name: "test.yaml",
content:
"on: push\nname: hello\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - run: echo 'hello'"
},
nullTrace
2023-01-09 19:02:19 -05:00
);
2023-01-09 19:02:19 -05:00
expect(result.context.errors.getErrors()).toHaveLength(0);
expect(result.value).not.toBeUndefined();
2023-03-17 11:17:18 -04:00
const root = result.value!.assertMapping("root"); // eslint-disable-line @typescript-eslint/no-non-null-assertion
2023-01-09 19:02:19 -05:00
expect(root.description).toBe("Workflow file with strict validation");
for (const pair of root) {
2023-01-09 19:02:19 -05:00
const key = pair.key.assertString("key").value;
switch (key) {
case "name": {
2023-01-09 19:02:19 -05:00
const nameKey = pair.key.assertString("name");
expect(nameKey.definition).not.toBeUndefined();
2023-01-13 12:32:08 -08:00
expect(nameKey.description).toContain("The name of the workflow");
2023-01-09 19:02:19 -05:00
break;
}
case "on": {
2023-01-09 19:02:19 -05:00
const onKey = pair.key.assertString("on");
const onValue = pair.value.assertString("push");
expect(onKey.definition).not.toBeUndefined();
2023-01-13 12:32:08 -08:00
expect(onKey.description).toContain("The GitHub event that triggers the workflow.");
2023-01-09 19:02:19 -05:00
expect(onValue.definition).not.toBeUndefined();
expect(onValue.description).toBe("Runs your workflow when you push a commit or tag.");
break;
}
}
}
2023-01-09 19:02:19 -05:00
});
});