Files
languageservices/actions-languageservice/src/context/workflow-context.test.ts
T

91 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-11-23 15:35:34 -08:00
import {convertWorkflowTemplate, parseWorkflow, WorkflowTemplate} from "@github/actions-workflow-parser";
import {ActionStep, RunStep} from "@github/actions-workflow-parser/model/workflow-template";
2022-11-23 15:35:34 -08:00
import {nullTrace} from "../nulltrace";
import {getPositionFromCursor} from "../test-utils/cursor-position";
import {findToken} from "../utils/find-token";
import {getWorkflowContext, WorkflowContext} from "./workflow-context";
2023-02-06 15:10:34 -05:00
async function testGetWorkflowContext(input: string): Promise<WorkflowContext> {
2022-11-23 15:35:34 -08:00
const [textDocument, pos] = getPositionFromCursor(input);
const result = parseWorkflow(
2023-02-06 11:15:15 -05:00
{
content: textDocument.getText(),
name: "wf.yaml"
},
2022-11-23 15:35:34 -08:00
nullTrace
);
let template: WorkflowTemplate | undefined;
if (result.value) {
2023-02-06 15:10:34 -05:00
template = await convertWorkflowTemplate(result.context, result.value);
2022-11-23 15:35:34 -08:00
}
const {path} = findToken(pos, result.value);
return getWorkflowContext(textDocument.uri, template, path);
2022-11-23 15:35:34 -08:00
}
describe("getWorkflowContext", () => {
2023-02-06 15:10:34 -05:00
it("context for workflow", async () => {
const context = await testGetWorkflowContext(`on: push
2022-11-23 15:35:34 -08:00
name: te|st
jobs:
build:
runs-on: ubuntu-latest
steps:
- echo Hello`);
expect(context.uri).not.toBe("");
expect(context.template).not.toBeUndefined();
expect(context.job).toBeUndefined();
expect(context.step).toBeUndefined();
});
2023-02-06 15:10:34 -05:00
it("context for workflow job", async () => {
const context = await testGetWorkflowContext(`on: push
2022-11-23 15:35:34 -08:00
jobs:
build:
runs-on: ubuntu-lat|est
steps:
- run: echo Hello`);
expect(context.uri).not.toBe("");
expect(context.template).not.toBeUndefined();
expect(context.job).not.toBeUndefined();
expect(context.step).toBeUndefined();
});
2023-02-06 15:10:34 -05:00
it("context for workflow run step", async () => {
const context = await testGetWorkflowContext(`on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo |Hello
- uses: actions/checkout@v2`);
expect(context.uri).not.toBe("");
expect(context.template).not.toBeUndefined();
expect(context.job).not.toBeUndefined();
const step = context.step as RunStep;
expect(step).not.toBeUndefined();
expect(step.run.toDisplayString()).toBe("echo Hello");
});
2023-02-06 15:10:34 -05:00
it("context for workflow uses step", async () => {
const context = await testGetWorkflowContext(`on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo Hello
- uses: actions/checkout@v2|`);
expect(context.uri).not.toBe("");
expect(context.template).not.toBeUndefined();
expect(context.job).not.toBeUndefined();
const step = context.step as ActionStep;
expect(step).not.toBeUndefined();
expect(step.uses.value).toBe("actions/checkout@v2");
});
2022-11-23 15:35:34 -08:00
});