Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve error message on mockStep failure #74

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/step-mocker/step-mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class StepMocker {
this.updateStep(workflow, jobId, stepIndex, mockStep);
}
} else {
throw new Error("Could not find step");
const { mockWith: _, ...stepQuery } = mockStep;
throw new Error(`Could not find step ${JSON.stringify(stepQuery)} in job ${jobId}\nin ${filePath}`);
}
}
stepsToAdd.forEach(s => this.addStep(workflow, s.jobId, s.stepIndex, s.mockStep));
Expand Down
94 changes: 90 additions & 4 deletions test/unit/step-mocker/step-mocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ describe("locateSteps", () => {
existsSyncMock.mockReturnValueOnce(true);
writeFileSyncMock.mockReturnValueOnce(undefined);
});
test("no job found", async () => {

test("no job found using wrong name", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
Expand All @@ -117,7 +118,7 @@ describe("locateSteps", () => {
},
],
})
).rejects.toThrowError();
).rejects.toThrowError(`Could not find step {"name":"step"} in job incorrectName\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using id", async () => {
Expand All @@ -142,6 +143,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect id", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
id: "not-echo",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"id":"not-echo"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using name", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -162,6 +180,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect name", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
name: "Incorrect Name",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"name":"Incorrect Name"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using uses", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -184,6 +219,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect uses", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
uses: "invalid/action@v0",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"uses":"invalid/action@v0"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using run", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -203,7 +255,24 @@ describe("locateSteps", () => {
"utf8"
);
});


test("no step found using incorrect run", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
run: "echo incorrect",
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"run":"echo incorrect"} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test("step found using index", async () => {
const data = await readFile(path.join(resources, "steps.yaml"), "utf8");
readFileSyncMock.mockReturnValueOnce(data);
Expand All @@ -224,6 +293,23 @@ describe("locateSteps", () => {
);
});

test("no step found using incorrect index", async () => {
readFileSyncMock.mockReturnValueOnce(
await readFile(path.join(resources, "steps.yaml"), "utf8")
);
const stepMocker = new StepMocker("workflow.yaml", __dirname);
await expect(
stepMocker.mock({
name: [
{
index: 42,
mockWith: "echo step",
},
],
})
).rejects.toThrowError(`Could not find step {"index":42} in job name\nin ${path.join(__dirname, "workflow.yaml")}`);
});

test.each([
["index", 0, 0],
["name", "secrets", 0]
Expand Down Expand Up @@ -469,4 +555,4 @@ describe("add step", () => {
"utf8"
);
});
});
});
Loading