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";
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
describe("parseWorkflow", () => {
|
|
|
|
|
it("parses valid workflow", () => {
|
|
|
|
|
const result = parseWorkflow(
|
|
|
|
|
"test.yaml",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: "test.yaml",
|
2023-01-09 19:02:19 -05:00
|
|
|
content: "on: push\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - run: echo 'hello'"
|
|
|
|
|
}
|
2023-01-06 15:54:31 -08:00
|
|
|
],
|
|
|
|
|
nullTrace
|
2023-01-09 19:02:19 -05:00
|
|
|
);
|
2023-01-06 15:54:31 -08:00
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
expect(result.context.errors.getErrors()).toHaveLength(0);
|
|
|
|
|
});
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
it("contains range for error", () => {
|
|
|
|
|
const result = parseWorkflow(
|
|
|
|
|
"test.yaml",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: "test.yaml",
|
2023-01-09 19:02:19 -05:00
|
|
|
content: "on: push\njobs:\n build:\n steps:\n - run: echo 'hello'"
|
|
|
|
|
}
|
2023-01-06 15:54:31 -08:00
|
|
|
],
|
|
|
|
|
nullTrace
|
2023-01-09 19:02:19 -05:00
|
|
|
);
|
2023-01-06 15:54:31 -08: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, {
|
|
|
|
|
start: [4, 5],
|
|
|
|
|
end: [5, 24]
|
|
|
|
|
})
|
|
|
|
|
]);
|
|
|
|
|
});
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
it("error range for expression is constrained to scalar node", () => {
|
|
|
|
|
const result = parseWorkflow(
|
|
|
|
|
"test.yaml",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
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-01-06 15:54:31 -08:00
|
|
|
],
|
|
|
|
|
nullTrace
|
2023-01-09 19:02:19 -05:00
|
|
|
);
|
2023-01-06 15:54:31 -08: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,
|
|
|
|
|
{
|
|
|
|
|
start: [6, 13],
|
2023-01-09 19:02:19 -05:00
|
|
|
end: [6, 37]
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
2023-01-09 19:02:19 -05:00
|
|
|
)
|
|
|
|
|
]);
|
|
|
|
|
});
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
it("tokens contain descriptions", () => {
|
|
|
|
|
const result = parseWorkflow(
|
|
|
|
|
"test.yaml",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: "test.yaml",
|
|
|
|
|
content:
|
2023-01-09 19:02:19 -05:00
|
|
|
"on: push\nname: hello\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - run: echo 'hello'"
|
|
|
|
|
}
|
2023-01-06 15:54:31 -08:00
|
|
|
],
|
|
|
|
|
nullTrace
|
2023-01-09 19:02:19 -05:00
|
|
|
);
|
2023-01-06 15:54:31 -08:00
|
|
|
|
2023-01-09 19:02:19 -05:00
|
|
|
expect(result.context.errors.getErrors()).toHaveLength(0);
|
|
|
|
|
expect(result.value).not.toBeUndefined();
|
|
|
|
|
const root = result.value!.assertMapping("root");
|
|
|
|
|
expect(root.description).toBe("Workflow file with strict validation");
|
2023-01-06 15:54:31 -08:00
|
|
|
for (const pair of root) {
|
2023-01-09 19:02:19 -05:00
|
|
|
const key = pair.key.assertString("key").value;
|
2023-01-06 15:54:31 -08:00
|
|
|
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;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
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-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 19:02:19 -05:00
|
|
|
});
|
|
|
|
|
});
|