feat: use child process package to execute command (#391)

* feat: use `execFileSync` from `node:child_process` package

* feat: modify `configureProject` function to be sync

* feat: modify `buildProject` function to be sync

* feat: display command output to the parent output
This commit is contained in:
Alfi Maulana
2024-08-06 11:29:28 +07:00
committed by GitHub
parent f0425ca4df
commit 23aad7b33f
6 changed files with 122 additions and 1474 deletions

View File

@@ -21,8 +21,8 @@ const defaultInputs: Inputs = {
buildArgs: [],
};
jest.unstable_mockModule("@actions/exec", () => ({
exec: jest.fn(),
jest.unstable_mockModule("node:child_process", () => ({
execFileSync: jest.fn(),
}));
describe("configure a CMake project", () => {
@@ -115,15 +115,18 @@ describe("configure a CMake project", () => {
for (const testCase of testCases) {
it(`should execute the correct command ${testCase.name}`, async () => {
const { configureProject } = await import("./cmake.js");
const { exec } = await import("@actions/exec");
const { execFileSync } = await import("node:child_process");
jest.mocked(exec).mockReset();
jest.mocked(execFileSync).mockReset();
const prom = configureProject({ ...defaultInputs, ...testCase.inputs });
await expect(prom).resolves.toBeUndefined();
configureProject({ ...defaultInputs, ...testCase.inputs });
expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenLastCalledWith("cmake", testCase.expectedArgs);
expect(execFileSync).toHaveBeenCalledTimes(1);
expect(execFileSync).toHaveBeenLastCalledWith(
"cmake",
testCase.expectedArgs,
{ stdio: "inherit" },
);
});
}
});
@@ -157,15 +160,18 @@ describe("build a CMake project", () => {
for (const testCase of testCases) {
it(`should execute the correct command ${testCase.name}`, async () => {
const { buildProject } = await import("./cmake.js");
const { exec } = await import("@actions/exec");
const { execFileSync } = await import("node:child_process");
jest.mocked(exec).mockReset();
jest.mocked(execFileSync).mockReset();
const prom = buildProject({ ...defaultInputs, ...testCase.inputs });
await expect(prom).resolves.toBeUndefined();
buildProject({ ...defaultInputs, ...testCase.inputs });
expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenLastCalledWith("cmake", testCase.expectedArgs);
expect(execFileSync).toHaveBeenCalledTimes(1);
expect(execFileSync).toHaveBeenLastCalledWith(
"cmake",
testCase.expectedArgs,
{ stdio: "inherit" },
);
});
}
});

View File

@@ -1,4 +1,4 @@
import { exec } from "@actions/exec";
import { execFileSync } from "node:child_process";
import type { Inputs } from "./inputs.js";
/**
@@ -6,7 +6,7 @@ import type { Inputs } from "./inputs.js";
*
* @param inputs - The action inputs.
*/
export async function configureProject(inputs: Inputs): Promise<void> {
export function configureProject(inputs: Inputs): void {
const configureArgs = [];
if (inputs.sourceDir) {
@@ -38,7 +38,7 @@ export async function configureProject(inputs: Inputs): Promise<void> {
configureArgs.push(...inputs.options.map((opt) => "-D" + opt));
configureArgs.push(...inputs.args);
await exec("cmake", configureArgs);
execFileSync("cmake", configureArgs, { stdio: "inherit" });
}
/**
@@ -46,6 +46,8 @@ export async function configureProject(inputs: Inputs): Promise<void> {
*
* @param inputs - The action inputs.
*/
export async function buildProject(inputs: Inputs): Promise<void> {
await exec("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs]);
export function buildProject(inputs: Inputs): void {
execFileSync("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs], {
stdio: "inherit",
});
}

View File

@@ -5,12 +5,12 @@ import { getInputs } from "./inputs.js";
try {
const inputs = getInputs();
await configureProject(inputs);
configureProject(inputs);
core.setOutput("build-dir", inputs.buildDir);
if (inputs.runBuild) {
await buildProject(inputs);
buildProject(inputs);
}
} catch (err) {
core.setFailed(err);