2022-11-23 15:35:34 -08:00
|
|
|
import {convertWorkflowTemplate, parseWorkflow, WorkflowTemplate} from "@github/actions-workflow-parser";
|
2022-12-05 16:50:39 -05:00
|
|
|
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);
|
|
|
|
|
|
2022-12-05 16:50:39 -05:00
|
|
|
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();
|
|
|
|
|
});
|
2022-12-05 16:50:39 -05:00
|
|
|
|
2023-02-06 15:10:34 -05:00
|
|
|
it("context for workflow run step", async () => {
|
|
|
|
|
const context = await testGetWorkflowContext(`on: push
|
2022-12-05 16:50:39 -05:00
|
|
|
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
|
2022-12-05 16:50:39 -05:00
|
|
|
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
|
|
|
});
|