mirror of
https://github.com/threeal/cmake-action.git
synced 2025-06-09 02:31:21 +00:00
feat: log executed command (#524)
Some checks failed
Build / Build Package (push) Failing after 1s
Test / Test Action (ubuntu-22.04) (push) Failing after 30s
Test / Test Action With Specified Directories (push) Failing after 35s
Check / Check Package (push) Failing after 1s
Test / Test Package (push) Failing after 1s
Test / Test Action Without Run Build (push) Failing after 36s
Test / Test Action With Additional Options (push) Failing after 3s
Test / Test Action With Custom Generator (push) Failing after 2s
Test / Test Action (macos-14) (push) Has been cancelled
Test / Test Action (windows-2022) (push) Has been cancelled
Some checks failed
Build / Build Package (push) Failing after 1s
Test / Test Action (ubuntu-22.04) (push) Failing after 30s
Test / Test Action With Specified Directories (push) Failing after 35s
Check / Check Package (push) Failing after 1s
Test / Test Package (push) Failing after 1s
Test / Test Action Without Run Build (push) Failing after 36s
Test / Test Action With Additional Options (push) Failing after 3s
Test / Test Action With Custom Generator (push) Failing after 2s
Test / Test Action (macos-14) (push) Has been cancelled
Test / Test Action (windows-2022) (push) Has been cancelled
* feat: log executed command Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com> * feat: log command based on spawned process arguments Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com> --------- Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
This commit is contained in:
parent
c7ba931617
commit
a93af4bd1e
11
dist/action.mjs
generated
vendored
11
dist/action.mjs
generated
vendored
@ -50,6 +50,16 @@ function logError(err) {
|
|||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
process.stdout.write(`::error::${message}${os.EOL}`);
|
process.stdout.write(`::error::${message}${os.EOL}`);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Logs a command along with its arguments in GitHub Actions.
|
||||||
|
*
|
||||||
|
* @param command - The command to log.
|
||||||
|
* @param args - The arguments of the command.
|
||||||
|
*/
|
||||||
|
function logCommand(command, ...args) {
|
||||||
|
const message = [command, ...args].join(" ");
|
||||||
|
process.stdout.write(`[command]${message}${os.EOL}`);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes a command with the given arguments.
|
* Executes a command with the given arguments.
|
||||||
@ -65,6 +75,7 @@ async function exec(command, args) {
|
|||||||
const proc = spawn(command, args, {
|
const proc = spawn(command, args, {
|
||||||
stdio: ["ignore", "inherit", "inherit"],
|
stdio: ["ignore", "inherit", "inherit"],
|
||||||
});
|
});
|
||||||
|
logCommand(proc.spawnfile, ...proc.spawnargs.splice(1));
|
||||||
proc.on("error", reject);
|
proc.on("error", reject);
|
||||||
proc.on("close", (code) => {
|
proc.on("close", (code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
|
@ -1,13 +1,30 @@
|
|||||||
import { exec } from "./exec.js";
|
import { jest } from "@jest/globals";
|
||||||
|
|
||||||
describe("execute commands", () => {
|
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 () => {
|
it("should successfully execute a command", async () => {
|
||||||
|
const { exec } = await import("./exec.js");
|
||||||
|
|
||||||
await exec("node", ["--version"]);
|
await exec("node", ["--version"]);
|
||||||
|
|
||||||
|
expect(logCommand).toHaveBeenCalledTimes(1);
|
||||||
|
expect(logCommand).toHaveBeenCalledWith("node", "--version");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fail to execute a command", async () => {
|
it("should fail to execute a command", async () => {
|
||||||
|
const { exec } = await import("./exec.js");
|
||||||
|
|
||||||
await expect(exec("node", ["--invalid"])).rejects.toThrow(
|
await expect(exec("node", ["--invalid"])).rejects.toThrow(
|
||||||
"Command exited with status code 9",
|
"Command exited with status code 9",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expect(logCommand).toHaveBeenCalledTimes(1);
|
||||||
|
expect(logCommand).toHaveBeenCalledWith("node", "--invalid");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { logCommand } from "gha-utils";
|
||||||
import { spawn } from "node:child_process";
|
import { spawn } from "node:child_process";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,6 +15,7 @@ export async function exec(command: string, args: string[]): Promise<void> {
|
|||||||
const proc = spawn(command, args, {
|
const proc = spawn(command, args, {
|
||||||
stdio: ["ignore", "inherit", "inherit"],
|
stdio: ["ignore", "inherit", "inherit"],
|
||||||
});
|
});
|
||||||
|
logCommand(proc.spawnfile, ...proc.spawnargs.splice(1));
|
||||||
proc.on("error", reject);
|
proc.on("error", reject);
|
||||||
proc.on("close", (code) => {
|
proc.on("close", (code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user