From 81c5bdcff9e3c6fc5ae20616ffcfd74985b76ba1 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Mar 2024 22:35:40 +0700 Subject: [PATCH] refactor: add `buildProject` function from lines in `main` function --- dist/index.js | 11 ++++-- src/cmake.test.ts | 89 ++++++++++++++++++++++++++++++++++++----------- src/cmake.ts | 9 +++++ src/index.ts | 5 ++- 4 files changed, 89 insertions(+), 25 deletions(-) diff --git a/dist/index.js b/dist/index.js index c6e7846..7e507ca 100644 --- a/dist/index.js +++ b/dist/index.js @@ -27726,6 +27726,14 @@ async function configureProject(inputs) { configureArgs.push(...inputs.args); await (0,exec.exec)("cmake", configureArgs); } +/** + * Build a CMake project. + * + * @param inputs - The action inputs. + */ +async function buildProject(inputs) { + await (0,exec.exec)("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs]); +} ;// CONCATENATED MODULE: ./src/inputs.ts @@ -27749,13 +27757,12 @@ function getInputs() { - async function main() { const inputs = getInputs(); await configureProject(inputs); core.setOutput("build-dir", inputs.buildDir); if (inputs.runBuild) { - await (0,exec.exec)("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs]); + await buildProject(inputs); } } main().catch((err) => core.setFailed(err)); diff --git a/src/cmake.test.ts b/src/cmake.test.ts index ee85b45..041a2af 100644 --- a/src/cmake.test.ts +++ b/src/cmake.test.ts @@ -1,31 +1,31 @@ import { jest } from "@jest/globals"; import type { Inputs } from "./inputs.js"; +interface TestCase { + name: string; + inputs: Inputs; + expectedArgs: string[]; +} + +const defaultInputs: Inputs = { + sourceDir: ".", + buildDir: "build", + generator: "", + cCompiler: "", + cxxCompiler: "", + cFlags: "", + cxxFlags: "", + options: [], + args: [], + runBuild: true, + buildArgs: [], +}; + jest.unstable_mockModule("@actions/exec", () => ({ exec: jest.fn(), })); describe("configure a CMake project", () => { - const defaultInputs: Inputs = { - sourceDir: ".", - buildDir: "build", - generator: "", - cCompiler: "", - cxxCompiler: "", - cFlags: "", - cxxFlags: "", - options: [], - args: [], - runBuild: true, - buildArgs: [], - }; - - interface TestCase { - name: string; - inputs: Inputs; - expectedArgs: string[]; - } - const testCases: TestCase[] = [ { name: "with nothing specified", @@ -161,3 +161,52 @@ describe("configure a CMake project", () => { }); } }); + +describe("build a CMake project", () => { + const testCases: TestCase[] = [ + { + name: "with nothing specified", + inputs: defaultInputs, + expectedArgs: ["--build", "build"], + }, + { + name: "with build directory specified", + inputs: { + ...defaultInputs, + buildDir: "output", + }, + expectedArgs: ["--build", "output"], + }, + { + name: "with additional arguments specified", + inputs: { + ...defaultInputs, + buildArgs: ["--target", "foo"], + }, + expectedArgs: ["--build", "build", "--target", "foo"], + }, + { + name: "with all specified", + inputs: { + ...defaultInputs, + buildDir: "output", + buildArgs: ["--target", "foo"], + }, + expectedArgs: ["--build", "output", "--target", "foo"], + }, + ]; + + for (const { name, inputs, expectedArgs } of testCases) { + it(`should execute the correct command ${name}`, async () => { + const { buildProject } = await import("./cmake.js"); + const { exec } = await import("@actions/exec"); + + jest.mocked(exec).mockReset(); + + await expect(buildProject(inputs)).resolves.toBeUndefined(); + + expect(exec).toHaveBeenCalledTimes(1); + expect(exec).toHaveBeenLastCalledWith("cmake", expectedArgs); + }); + } +}); diff --git a/src/cmake.ts b/src/cmake.ts index 0a5fc6c..7b9f8c6 100644 --- a/src/cmake.ts +++ b/src/cmake.ts @@ -34,3 +34,12 @@ export async function configureProject(inputs: Inputs): Promise { await exec("cmake", configureArgs); } + +/** + * Build a CMake project. + * + * @param inputs - The action inputs. + */ +export async function buildProject(inputs: Inputs): Promise { + await exec("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs]); +} diff --git a/src/index.ts b/src/index.ts index 674a0ae..99f1f4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ import * as core from "@actions/core"; -import { exec } from "@actions/exec"; -import { configureProject } from "./cmake.js"; +import { buildProject, configureProject } from "./cmake.js"; import { getInputs } from "./inputs.js"; async function main() { @@ -11,7 +10,7 @@ async function main() { core.setOutput("build-dir", inputs.buildDir); if (inputs.runBuild) { - await exec("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs]); + await buildProject(inputs); } }