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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 122 additions and 1474 deletions

1526
dist/index.js generated vendored

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,7 @@
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1"
"@actions/core": "^1.10.1"
},
"devDependencies": {
"@eslint/js": "^9.8.0",

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);

17
yarn.lock generated
View File

@ -22,15 +22,6 @@ __metadata:
languageName: node
linkType: hard
"@actions/exec@npm:^1.1.1":
version: 1.1.1
resolution: "@actions/exec@npm:1.1.1"
dependencies:
"@actions/io": "npm:^1.0.1"
checksum: 10c0/4a09f6bdbe50ce68b5cf8a7254d176230d6a74bccf6ecc3857feee209a8c950ba9adec87cc5ecceb04110182d1c17117234e45557d72fde6229b7fd3a395322a
languageName: node
linkType: hard
"@actions/http-client@npm:^2.0.1":
version: 2.2.0
resolution: "@actions/http-client@npm:2.2.0"
@ -41,13 +32,6 @@ __metadata:
languageName: node
linkType: hard
"@actions/io@npm:^1.0.1":
version: 1.1.3
resolution: "@actions/io@npm:1.1.3"
checksum: 10c0/5b8751918e5bf0bebd923ba917fb1c0e294401e7ff0037f32c92a4efa4215550df1f6633c63fd4efb2bdaae8711e69b9e36925857db1f38935ff62a5c92ec29e
languageName: node
linkType: hard
"@ampproject/remapping@npm:^2.2.0":
version: 2.3.0
resolution: "@ampproject/remapping@npm:2.3.0"
@ -3901,7 +3885,6 @@ __metadata:
resolution: "root@workspace:."
dependencies:
"@actions/core": "npm:^1.10.1"
"@actions/exec": "npm:^1.1.1"
"@eslint/js": "npm:^9.8.0"
"@jest/globals": "npm:^29.7.0"
"@types/jest": "npm:^29.5.12"