From 571e81560be6c78afd077b33bff0ff455fd0c9f8 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Thu, 8 Aug 2024 15:29:56 +0700 Subject: [PATCH] feat: utilize GitHub Actions Utilities package (#406) * feat: utilize GitHub Actions Utilities package * test: modify get action context test by mocking `getInput` function --- dist/index.mjs | 44 ++++++++++++++++++-------- package.json | 2 +- src/context.test.ts | 77 ++++++++++++++++++++++++--------------------- src/context.ts | 11 +------ src/index.ts | 13 +++----- yarn.lock | 16 +++++----- 6 files changed, 86 insertions(+), 77 deletions(-) diff --git a/dist/index.mjs b/dist/index.mjs index 9d69773..fe10a25 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -3,7 +3,34 @@ import os from 'node:os'; import { execFileSync } from 'node:child_process'; import path from 'node:path'; -function r(r){return function(r){if("object"==typeof(e=r)&&null!==e&&"message"in e&&"string"==typeof e.message)return r;var e;try{return new Error(JSON.stringify(r))}catch(e){return new Error(String(r))}}(r).message} +/** + * Retrieves the value of a GitHub Actions input. + * + * @param name - The name of the GitHub Actions input. + * @returns The value of the GitHub Actions input, or an empty string if not found. + */ +function getInput(name) { + const value = process.env[`INPUT_${name.toUpperCase()}`] || ""; + return value.trim(); +} +/** + * Sets the value of a GitHub Actions output. + * + * @param name - The name of the GitHub Actions output. + * @param value - The value of the GitHub Actions output + */ +function setOutput(name, value) { + fs.appendFileSync(process.env["GITHUB_OUTPUT"], `${name}=${value}${os.EOL}`); +} +/** + * Logs an error message on GitHub Actions. + * + * @param err - The error, which can be of any type. + */ +function error(err) { + const message = err instanceof Error ? err.message : String(err); + process.stdout.write(`::error::${message}${os.EOL}`); +} /** * Configures the build system of a CMake project. @@ -34,15 +61,6 @@ function buildProject(context) { }); } -/** - * Retrieves an action input. - * @param key - The key of the action input. - * @returns The action input value as a string. - */ -function getInput(key) { - const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; - return value.trim(); -} function getContext() { const sourceDir = getInput("source-dir"); const options = []; @@ -91,12 +109,12 @@ function getContext() { try { const context = getContext(); configureProject(context); - fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`); + setOutput("build-dir", context.buildDir); if (context.build.enabled) { buildProject(context); } } catch (err) { - process.exitCode = 1; - process.stdout.write(`::error::${r(err)}${os.EOL}`); + error(err); + process.exit(1); } diff --git a/package.json b/package.json index 535825d..b8e6f87 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "test": "jest" }, "dependencies": { - "catched-error-message": "^0.0.1" + "gha-utils": "^0.1.0" }, "devDependencies": { "@eslint/js": "^9.8.0", diff --git a/src/context.test.ts b/src/context.test.ts index ee0ba3e..918fc7d 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -1,10 +1,15 @@ +import { jest } from "@jest/globals"; import path from "node:path"; -import { Context, getContext } from "./context.js"; +import type { Context } from "./context.js"; + +jest.unstable_mockModule("gha-utils", () => ({ + getInput: jest.fn(), +})); describe("get action context", () => { interface TestCase { name: string; - env?: Record; + inputs?: Record; expectedContext?: Partial; } @@ -14,7 +19,7 @@ describe("get action context", () => { }, { name: "with source directory specified", - env: { "INPUT_SOURCE-DIR": "project" }, + inputs: { "source-dir": "project" }, expectedContext: { sourceDir: "project", buildDir: path.join("project", "build"), @@ -22,14 +27,14 @@ describe("get action context", () => { }, { name: "with build directory specified", - env: { "INPUT_BUILD-DIR": "output" }, + inputs: { "build-dir": "output" }, expectedContext: { buildDir: "output" }, }, { name: "with source and build directories specified", - env: { - "INPUT_SOURCE-DIR": "project", - "INPUT_BUILD-DIR": "output", + inputs: { + "source-dir": "project", + "build-dir": "output", }, expectedContext: { sourceDir: "project", @@ -38,7 +43,7 @@ describe("get action context", () => { }, { name: "with generator specified", - env: { INPUT_GENERATOR: "Ninja" }, + inputs: { generator: "Ninja" }, expectedContext: { configure: { generator: "Ninja", @@ -49,7 +54,7 @@ describe("get action context", () => { }, { name: "with C compiler specified", - env: { "INPUT_C-COMPILER": "clang" }, + inputs: { "c-compiler": "clang" }, expectedContext: { configure: { generator: "", @@ -60,7 +65,7 @@ describe("get action context", () => { }, { name: "with C++ compiler specified", - env: { "INPUT_CXX-COMPILER": "clang++" }, + inputs: { "cxx-compiler": "clang++" }, expectedContext: { configure: { generator: "", @@ -71,7 +76,7 @@ describe("get action context", () => { }, { name: "with C flags specified", - env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" }, + inputs: { "c-flags": "-Werror -Wall\n-Wextra" }, expectedContext: { configure: { generator: "", @@ -82,7 +87,7 @@ describe("get action context", () => { }, { name: "with C++ flags specified", - env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" }, + inputs: { "cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic" }, expectedContext: { configure: { generator: "", @@ -93,8 +98,8 @@ describe("get action context", () => { }, { name: "with additional options specified", - env: { - INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", + inputs: { + options: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", }, expectedContext: { configure: { @@ -106,7 +111,7 @@ describe("get action context", () => { }, { name: "with additional arguments specified", - env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" }, + inputs: { args: "-Wdev -Wdeprecated\n--fresh" }, expectedContext: { configure: { generator: "", @@ -117,12 +122,12 @@ describe("get action context", () => { }, { name: "with run build specified", - env: { "INPUT_RUN-BUILD": "true" }, + inputs: { "run-build": "true" }, expectedContext: { build: { enabled: true, args: [] } }, }, { name: "with additional build arguments specified", - env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" }, + inputs: { "build-args": "--target foo\n--parallel 8" }, expectedContext: { build: { enabled: false, @@ -132,18 +137,18 @@ describe("get action context", () => { }, { name: "with all specified", - env: { - "INPUT_SOURCE-DIR": "project", - "INPUT_BUILD-DIR": "output", - INPUT_GENERATOR: "Ninja", - "INPUT_C-COMPILER": "clang", - "INPUT_CXX-COMPILER": "clang++", - "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra", - "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic", - INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", - INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh", - "INPUT_RUN-BUILD": "true", - "INPUT_BUILD-ARGS": "--target foo\n--parallel 8", + inputs: { + "source-dir": "project", + "build-dir": "output", + generator: "Ninja", + "c-compiler": "clang", + "cxx-compiler": "clang++", + "c-flags": "-Werror -Wall\n-Wextra", + "cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic", + options: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", + args: "-Wdev -Wdeprecated\n--fresh", + "run-build": "true", + "build-args": "--target foo\n--parallel 8", }, expectedContext: { sourceDir: "project", @@ -171,11 +176,13 @@ describe("get action context", () => { for (const testCase of testCases) { it(`should get the action context ${testCase.name}`, async () => { - const prevEnv = process.env; - process.env = { - ...process.env, - ...testCase.env, - }; + const { getInput } = await import("gha-utils"); + const { getContext } = await import("./context.js"); + + const inputs = testCase.inputs || {}; + jest.mocked(getInput).mockImplementation((name) => { + return inputs[name] || ""; + }); expect(getContext()).toStrictEqual({ sourceDir: "", @@ -191,8 +198,6 @@ describe("get action context", () => { }, ...testCase.expectedContext, }); - - process.env = prevEnv; }); } }); diff --git a/src/context.ts b/src/context.ts index 8f6ac2a..c8a20b4 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,3 +1,4 @@ +import { getInput } from "gha-utils"; import path from "node:path"; export interface Context { @@ -14,16 +15,6 @@ export interface Context { }; } -/** - * Retrieves an action input. - * @param key - The key of the action input. - * @returns The action input value as a string. - */ -function getInput(key: string): string { - const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; - return value.trim(); -} - export function getContext(): Context { const sourceDir = getInput("source-dir"); const options: string[] = []; diff --git a/src/index.ts b/src/index.ts index 5238f92..494beee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,4 @@ -import { getErrorMessage } from "catched-error-message"; -import fs from "node:fs"; -import os from "node:os"; +import { error, setOutput } from "gha-utils"; import { buildProject, configureProject } from "./cmake.js"; import { getContext } from "./context.js"; @@ -9,15 +7,12 @@ try { configureProject(context); - fs.appendFileSync( - process.env["GITHUB_OUTPUT"] as string, - `build-dir=${context.buildDir}${os.EOL}`, - ); + setOutput("build-dir", context.buildDir); if (context.build.enabled) { buildProject(context); } } catch (err) { - process.exitCode = 1; - process.stdout.write(`::error::${getErrorMessage(err)}${os.EOL}`); + error(err); + process.exit(1); } diff --git a/yarn.lock b/yarn.lock index 6f1c40b..b8e7102 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1670,13 +1670,6 @@ __metadata: languageName: node linkType: hard -"catched-error-message@npm:^0.0.1": - version: 0.0.1 - resolution: "catched-error-message@npm:0.0.1" - checksum: 10c0/1f10cd4323a73bec7a57b7495730e5dad9995120b04e85b5f654f7b40dc6a36320d95cc55e49cdf78753158e18790ef725e2ec7309ebced3acd6c9a557ac075b - languageName: node - linkType: hard - "chalk@npm:^2.4.2": version: 2.4.2 resolution: "chalk@npm:2.4.2" @@ -2387,6 +2380,13 @@ __metadata: languageName: node linkType: hard +"gha-utils@npm:^0.1.0": + version: 0.1.0 + resolution: "gha-utils@npm:0.1.0" + checksum: 10c0/8c1cc68e89434a8af8a64d71a2dae9a9248ce581b522718ba51f78b5447cad9c16b8d70097a885f797d547bbafdb7bc481d9d41ac1ce0b2db084d500ebfbc838 + languageName: node + linkType: hard + "glob-parent@npm:^5.1.2": version: 5.1.2 resolution: "glob-parent@npm:5.1.2" @@ -4132,8 +4132,8 @@ __metadata: "@rollup/plugin-typescript": "npm:^11.1.6" "@types/jest": "npm:^29.5.12" "@types/node": "npm:^22.1.0" - catched-error-message: "npm:^0.0.1" eslint: "npm:^9.8.0" + gha-utils: "npm:^0.1.0" jest: "npm:^29.7.0" prettier: "npm:^3.3.3" rollup: "npm:^4.20.0"