Skip to content

Commit

Permalink
feat: add logs debug on post
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <[email protected]>
  • Loading branch information
neilime committed Apr 2, 2024
1 parent a48c80b commit fc3add1
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 180 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
This action runs your docker-compose file and clean up before action finished

<!-- end description -->

### Post hook

On post hook, the action will run `docker-compose down` to clean up the services.
In debug mode, the logs of the running services are printed before the cleanup.

<!-- start contents -->
<!-- end contents -->
<!-- start usage -->
Expand Down
54 changes: 29 additions & 25 deletions dist/index.js

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

59 changes: 34 additions & 25 deletions dist/post.js

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

57 changes: 48 additions & 9 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
/**
* Unit tests for the action's entrypoint, src/index.ts
*/
import * as core from "@actions/core";
import { DockerComposeService } from "./services/docker-compose.service";
import { InputService } from "./services/input.service";
import { LoggerService } from "./services/logger.service";

import * as main from "./main";

// Mock the action's entrypoint
const runMock = jest.spyOn(main, "run").mockImplementation();
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>;
let getInputsMock: jest.SpiedFunction<typeof InputService.prototype.getInputs>;
let debugMock: jest.SpiedFunction<typeof LoggerService.prototype.debug>;
let infoMock: jest.SpiedFunction<typeof LoggerService.prototype.info>;
let upMock: jest.SpiedFunction<typeof DockerComposeService.prototype.up>;

describe("index", () => {
beforeEach(() => {
jest.clearAllMocks();

setFailedMock = jest.spyOn(core, "setFailed").mockImplementation();
infoMock = jest.spyOn(LoggerService.prototype, "info").mockImplementation();
debugMock = jest.spyOn(LoggerService.prototype, "debug").mockImplementation();
getInputsMock = jest.spyOn(InputService.prototype, "getInputs");
upMock = jest.spyOn(DockerComposeService.prototype, "up");
});

it("calls run when imported", async () => {
getInputsMock.mockImplementation(() => ({
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
}));

upMock.mockResolvedValueOnce();

// eslint-disable-next-line @typescript-eslint/no-require-imports
require("../src/index");
await require("../src/index");

// 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(upMock).toHaveBeenCalledWith({
composeFiles: ["docker-compose.yml"],
services: [],
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
});

expect(runMock).toHaveBeenCalled();
expect(setFailedMock).not.toHaveBeenCalled();
expect(infoMock).toHaveBeenCalledWith("compose started");
});
});
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
/**
* The entrypoint for the action.
*/
import { run, RunAction } from "./main";
import { RunCallback, run } from "./runner";
import { DockerComposeService } from "./services/docker-compose.service";
import { Inputs } from "./services/input.service";
import { LoggerService } from "./services/logger.service";

const callback: RunCallback = async (
inputs: Inputs,
loggerService: LoggerService,
dockerComposeService: DockerComposeService
) => {
await dockerComposeService.up(inputs);
loggerService.info("compose started");
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
run(RunAction.UP);
run(callback);
42 changes: 0 additions & 42 deletions src/main.ts

This file was deleted.

Loading

0 comments on commit fc3add1

Please sign in to comment.