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: ensure given docker-compose file(s) are valid and at least one is provided #65

Merged
merged 1 commit into from
Apr 2, 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
8 changes: 4 additions & 4 deletions .github/workflows/__check-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ jobs:

- name: "Assert: only expected services are running"
run: |
docker-compose -f ./docker/docker-compose.yml ps
docker compose -f ./docker/docker-compose.yml ps

docker-compose -f ./docker/docker-compose.yml ps | grep docker-service-b-1 || (echo "Service service-b is not running" && exit 1)
docker-compose -f ./docker/docker-compose.yml ps | grep docker-service-c-1 || (echo "Service service-c is not running" && exit 1)
(docker-compose -f ./docker/docker-compose.yml ps | grep docker-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true
docker compose -f ./docker/docker-compose.yml ps | grep docker-service-b-1 || (echo "Service service-b is not running" && exit 1)
docker compose -f ./docker/docker-compose.yml ps | grep docker-service-c-1 || (echo "Service service-c is not running" && exit 1)
(docker compose -f ./docker/docker-compose.yml ps | grep docker-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true

test-action-with-down-flags:
runs-on: ubuntu-latest
Expand Down
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
Loading