Skip to content

Commit

Permalink
feat: log executed command
Browse files Browse the repository at this point in the history
Signed-off-by: Alfi Maulana <[email protected]>
  • Loading branch information
threeal committed Nov 23, 2024
1 parent c7ba931 commit 7ad4029
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions dist/action.mjs

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

19 changes: 18 additions & 1 deletion src/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { exec } from "./exec.js";
import { jest } from "@jest/globals";

describe("execute commands", () => {
const logCommand = jest.fn<(command: string, ...args: string[]) => void>();
jest.unstable_mockModule("gha-utils", () => ({ logCommand }));

beforeEach(() => {
logCommand.mockClear();
});

it("should successfully execute a command", async () => {
const { exec } = await import("./exec.js");

await exec("node", ["--version"]);

expect(logCommand).toHaveBeenCalledTimes(1);
expect(logCommand).toHaveBeenCalledWith("node", "--version");
});

it("should fail to execute a command", async () => {
const { exec } = await import("./exec.js");

await expect(exec("node", ["--invalid"])).rejects.toThrow(
"Command exited with status code 9",
);

expect(logCommand).toHaveBeenCalledTimes(1);
expect(logCommand).toHaveBeenCalledWith("node", "--invalid");
});
});
2 changes: 2 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logCommand } from "gha-utils";
import { spawn } from "node:child_process";

/**
Expand All @@ -11,6 +12,7 @@ import { spawn } from "node:child_process";
*/
export async function exec(command: string, args: string[]): Promise<void> {
return new Promise<void>((resolve, reject) => {
logCommand(command, ...args);
const proc = spawn(command, args, {
stdio: ["ignore", "inherit", "inherit"],
});
Expand Down

0 comments on commit 7ad4029

Please sign in to comment.