Skip to content

Commit

Permalink
fix: ensure given docker-compose file(s) are valid and at least one i…
Browse files Browse the repository at this point in the history
…s provided

Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
  • Loading branch information
neilime committed Apr 2, 2024
1 parent 99447ae commit 5158049
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 97 deletions.
19 changes: 7 additions & 12 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 7 additions & 12 deletions dist/post.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 18 additions & 45 deletions src/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ const runMock = jest.spyOn(runner, "run");

// Mock the external libraries and services used by the action
let debugMock: jest.SpiedFunction<typeof LoggerService.prototype.debug>;
let warnMock: jest.SpiedFunction<typeof LoggerService.prototype.warn>;
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>;
let getInputsMock: jest.SpiedFunction<typeof InputService.prototype.getInputs>;
describe("run", () => {
beforeEach(() => {
jest.clearAllMocks();

debugMock = jest.spyOn(LoggerService.prototype, "debug").mockImplementation();
warnMock = jest.spyOn(LoggerService.prototype, "warn").mockImplementation();
setFailedMock = jest.spyOn(core, "setFailed").mockImplementation();
getInputsMock = jest.spyOn(InputService.prototype, "getInputs");
});

it("should return and warns when composeFile is empty", async () => {
it("should call given callback when inputs are valid", async () => {
getInputsMock.mockImplementation(() => ({
composeFiles: [],
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
Expand All @@ -41,17 +39,30 @@ describe("run", () => {
}));

const callbackMock = jest.fn();
callbackMock.mockResolvedValueOnce(null);

await runner.run(callbackMock);
expect(runMock).toHaveReturned();

// Verify that all of the functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
1,
'inputs: {"composeFiles":[],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/current/working/dir"}'
'inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/current/working/dir"}'
);
expect(warnMock).toHaveBeenNthCalledWith(1, "no compose files found");
expect(callbackMock).not.toHaveBeenCalled();

expect(callbackMock).toHaveBeenCalledWith(
{
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
},
expect.any(LoggerService),
expect.any(DockerComposeService)
);

expect(setFailedMock).not.toHaveBeenCalled();
});

Expand All @@ -75,42 +86,4 @@ describe("run", () => {
expect(callbackMock).toHaveBeenCalled();
expect(setFailedMock).toHaveBeenNthCalledWith(1, "Error: unkown error");
});

it("should call callback when some compose files are provided", async () => {
getInputsMock.mockImplementation(() => ({
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
}));

const callbackMock = jest.fn();
callbackMock.mockResolvedValueOnce(null);

await runner.run(callbackMock);
expect(runMock).toHaveReturned();

// Verify that all of the functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
1,
'inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/current/working/dir"}'
);

expect(callbackMock).toHaveBeenCalledWith(
{
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
},
expect.any(LoggerService),
expect.any(DockerComposeService)
);

expect(setFailedMock).not.toHaveBeenCalled();
});
});
7 changes: 1 addition & 6 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ export type RunCallback = (
export async function run(callback: RunCallback): Promise<void> {
try {
const loggerService = new LoggerService();
const inputService = new InputService(loggerService);
const inputService = new InputService();
const dockerComposeService = new DockerComposeService();

const inputs = inputService.getInputs();
loggerService.debug(`inputs: ${JSON.stringify(inputs)}`);

if (!inputs.composeFiles.length) {
loggerService.warn("no compose files found");
return;
}

await callback(inputs, loggerService, dockerComposeService);
} catch (error) {
setFailed(`${error instanceof Error ? error : JSON.stringify(error)}`);
Expand Down
Loading

0 comments on commit 5158049

Please sign in to comment.