Skip to content

Commit 2dc1695

Browse files
add check-code tests for state/prepare
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3dd2e17 commit 2dc1695

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const info = vi.fn();
2+
const checkForCodeReviewViolations = vi.fn();
3+
4+
vi.doMock("@actions/core", () => ({
5+
info,
6+
error: vi.fn(),
7+
setOutput: vi.fn(),
8+
startGroup: vi.fn(),
9+
endGroup: vi.fn(),
10+
}));
11+
12+
vi.doMock("@actions/exec", () => ({
13+
exec: vi.fn(),
14+
}));
15+
16+
vi.doMock("@flyway-actions/shared/check-for-code-review-violations", () => ({
17+
checkForCodeReviewViolations,
18+
}));
19+
20+
const { runCheckCode } = await import("../../src/flyway/check-code.js");
21+
22+
describe("runCheckCode", () => {
23+
beforeEach(() => {
24+
checkForCodeReviewViolations.mockResolvedValue({ exitCode: 0, violationCount: 0, violationCodes: [] });
25+
});
26+
27+
it("should return undefined when skipCodeReview is true", async () => {
28+
expect(await runCheckCode({ skipCodeReview: true }, "V001__create.sql")).toBeUndefined();
29+
expect(info).toHaveBeenCalledWith(expect.stringContaining("Skipping code review"));
30+
expect(checkForCodeReviewViolations).not.toHaveBeenCalled();
31+
});
32+
33+
it("should return undefined when skipCodeReview is true even if failOnCodeReview is true", async () => {
34+
expect(await runCheckCode({ skipCodeReview: true, failOnCodeReview: true }, "V001__create.sql")).toBeUndefined();
35+
expect(checkForCodeReviewViolations).not.toHaveBeenCalled();
36+
});
37+
38+
it("should pass args with check and -code", async () => {
39+
await runCheckCode({}, "V001__create.sql");
40+
41+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
42+
43+
expect(args[0]).toBe("check");
44+
expect(args).toContain("-code");
45+
});
46+
47+
it("should include -check.scope=script and -check.scriptFilename", async () => {
48+
await runCheckCode({}, "V001__create.sql");
49+
50+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
51+
52+
expect(args).toContain("-check.scope=script");
53+
expect(args).toContain("-check.scriptFilename=V001__create.sql");
54+
});
55+
56+
it("should include -check.code.failOnError=true when failOnCodeReview is true", async () => {
57+
await runCheckCode({ failOnCodeReview: true }, "V001__create.sql");
58+
59+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
60+
61+
expect(args).toContain("-check.code.failOnError=true");
62+
});
63+
64+
it("should not include -check.code.failOnError=true when failOnCodeReview is false", async () => {
65+
await runCheckCode({ failOnCodeReview: false }, "V001__create.sql");
66+
67+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
68+
69+
expect(args).not.toContain("-check.code.failOnError=true");
70+
});
71+
72+
it("should include target environment and working directory args", async () => {
73+
await runCheckCode(
74+
{ targetUrl: "jdbc:postgresql://localhost/db", workingDirectory: "/app/db" },
75+
"V001__create.sql",
76+
);
77+
78+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
79+
80+
expect(args).toContain("-url=jdbc:postgresql://localhost/db");
81+
expect(args).toContain("-workingDirectory=/app/db");
82+
});
83+
84+
it("should pass workingDirectory to checkForCodeReviewViolations", async () => {
85+
await runCheckCode({ workingDirectory: "/app/db" }, "V001__create.sql");
86+
87+
expect(checkForCodeReviewViolations).toHaveBeenCalledWith(expect.any(Array), "/app/db");
88+
});
89+
90+
it("should include reportFilename when driftReportName is set", async () => {
91+
await runCheckCode({ driftReportName: "my-report" }, "V001__create.sql");
92+
93+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
94+
95+
expect(args).toContain("-reportFilename=my-report");
96+
});
97+
98+
it("should not include reportFilename when driftReportName is not set", async () => {
99+
await runCheckCode({}, "V001__create.sql");
100+
101+
const args = checkForCodeReviewViolations.mock.calls[0][0] as string[];
102+
103+
expect(args.some((a: string) => a.startsWith("-reportFilename="))).toBe(false);
104+
});
105+
});

0 commit comments

Comments
 (0)