refactor: remove core actions toolkit dependency (#397)

* refactor: add a `getInput` function

* refactor: replace `core.getBooleanInput` with `getInput`

* refactor: replace `core.getMultilineInput` with `getInput`

* refactor: replace `core.setOutput` with dirrect append to `GITHUB_OUTPUT`

* refactor: replace `core.setFailed` with direct stdout write
This commit is contained in:
Alfi Maulana
2024-08-06 22:42:23 +07:00
committed by GitHub
parent fe729787d8
commit 44a0a4c6c2
6 changed files with 106 additions and 26807 deletions

View File

@@ -1,5 +1,6 @@
import * as core from "@actions/core";
import { getErrorMessage } from "catched-error-message";
import fs from "node:fs";
import os from "node:os";
import { buildProject, configureProject } from "./cmake.js";
import { getInputs } from "./inputs.js";
@@ -8,11 +9,15 @@ try {
configureProject(inputs);
core.setOutput("build-dir", inputs.buildDir);
fs.appendFileSync(
process.env["GITHUB_OUTPUT"] as string,
`build-dir=${inputs.buildDir}${os.EOL}`,
);
if (inputs.runBuild) {
buildProject(inputs);
}
} catch (err) {
core.setFailed(getErrorMessage(err));
process.exitCode = 1;
process.stdout.write(`::error::${getErrorMessage(err)}${os.EOL}`);
}

View File

@@ -1,19 +1,10 @@
import { jest } from "@jest/globals";
import path from "node:path";
import type { Inputs } from "./inputs.js";
jest.unstable_mockModule("@actions/core", () => ({
getBooleanInput: jest.fn(),
getInput: jest.fn(),
getMultilineInput: jest.fn(),
}));
import { Inputs, getInputs } from "./inputs.js";
describe("get action inputs", () => {
interface TestCase {
name: string;
booleanInputs?: Record<string, boolean>;
stringInputs?: Record<string, string>;
multilineInputs?: Record<string, string[]>;
env?: Record<string, string>;
expectedInputs?: Partial<Inputs>;
}
@@ -23,7 +14,7 @@ describe("get action inputs", () => {
},
{
name: "with source directory specified",
stringInputs: { "source-dir": "project" },
env: { "INPUT_SOURCE-DIR": "project" },
expectedInputs: {
sourceDir: "project",
buildDir: path.join("project", "build"),
@@ -31,14 +22,14 @@ describe("get action inputs", () => {
},
{
name: "with build directory specified",
stringInputs: { "build-dir": "output" },
env: { "INPUT_BUILD-DIR": "output" },
expectedInputs: { buildDir: "output" },
},
{
name: "with source and build directories specified",
stringInputs: {
"source-dir": "project",
"build-dir": "output",
env: {
"INPUT_SOURCE-DIR": "project",
"INPUT_BUILD-DIR": "output",
},
expectedInputs: {
sourceDir: "project",
@@ -47,33 +38,33 @@ describe("get action inputs", () => {
},
{
name: "with generator specified",
stringInputs: { generator: "Ninja" },
env: { INPUT_GENERATOR: "Ninja" },
expectedInputs: { generator: "Ninja" },
},
{
name: "with C compiler specified",
stringInputs: { "c-compiler": "clang" },
env: { "INPUT_C-COMPILER": "clang" },
expectedInputs: { cCompiler: "clang" },
},
{
name: "with C++ compiler specified",
stringInputs: { "cxx-compiler": "clang++" },
env: { "INPUT_CXX-COMPILER": "clang++" },
expectedInputs: { cxxCompiler: "clang++" },
},
{
name: "with C flags specified",
multilineInputs: { "c-flags": ["-Werror -Wall", "-Wextra"] },
env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" },
expectedInputs: { cFlags: "-Werror -Wall -Wextra" },
},
{
name: "with C++ flags specified",
multilineInputs: { "cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"] },
env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" },
expectedInputs: { cxxFlags: "-Werror -Wall -Wextra -Wpedantic" },
},
{
name: "with additional options specified",
multilineInputs: {
options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
env: {
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
},
expectedInputs: {
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
@@ -81,37 +72,33 @@ describe("get action inputs", () => {
},
{
name: "with additional arguments specified",
multilineInputs: { args: ["-Wdev -Wdeprecated", "--fresh"] },
env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" },
expectedInputs: { args: ["-Wdev", "-Wdeprecated", "--fresh"] },
},
{
name: "with run build specified",
booleanInputs: { "run-build": false },
expectedInputs: { runBuild: false },
env: { "INPUT_RUN-BUILD": "true" },
expectedInputs: { runBuild: true },
},
{
name: "with additional build arguments specified",
multilineInputs: { "build-args": ["--target foo", "--parallel 8"] },
env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" },
expectedInputs: { buildArgs: ["--target", "foo", "--parallel", "8"] },
},
{
name: "with all specified",
booleanInputs: {
"run-build": false,
},
stringInputs: {
"source-dir": "project",
"build-dir": "output",
generator: "Ninja",
"c-compiler": "clang",
"cxx-compiler": "clang++",
},
multilineInputs: {
"c-flags": ["-Werror -Wall", "-Wextra"],
"cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"],
options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev -Wdeprecated", "--fresh"],
"build-args": ["--target foo", "--parallel 8"],
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",
},
expectedInputs: {
sourceDir: "project",
@@ -123,7 +110,7 @@ describe("get action inputs", () => {
cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev", "-Wdeprecated", "--fresh"],
runBuild: false,
runBuild: true,
buildArgs: ["--target", "foo", "--parallel", "8"],
},
},
@@ -131,26 +118,11 @@ describe("get action inputs", () => {
for (const testCase of testCases) {
it(`should get the action inputs ${testCase.name}`, async () => {
const { getInputs } = await import("./inputs.js");
const core = await import("@actions/core");
const booleanInputs: Record<string, boolean> = {
"run-build": true,
...testCase.booleanInputs,
const prevEnv = process.env;
process.env = {
...process.env,
...testCase.env,
};
jest.mocked(core.getBooleanInput).mockImplementation((name) => {
return booleanInputs[name] ?? false;
});
const stringInputs = { ...testCase.stringInputs };
jest.mocked(core.getInput).mockImplementation((name) => {
return stringInputs[name] ?? "";
});
const multilineInputs = { ...testCase.multilineInputs };
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
return multilineInputs[name] ?? [];
});
expect(getInputs()).toStrictEqual({
sourceDir: "",
@@ -162,10 +134,12 @@ describe("get action inputs", () => {
cxxFlags: "",
options: [],
args: [],
runBuild: true,
runBuild: false,
buildArgs: [],
...testCase.expectedInputs,
});
process.env = prevEnv;
});
}
});

View File

@@ -1,4 +1,3 @@
import { getBooleanInput, getInput, getMultilineInput } from "@actions/core";
import path from "node:path";
export interface Inputs {
@@ -15,6 +14,16 @@ export interface Inputs {
buildArgs: string[];
}
/**
* 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 getInputs(): Inputs {
const sourceDir = getInput("source-dir");
return {
@@ -23,13 +32,17 @@ export function getInputs(): Inputs {
generator: getInput("generator"),
cCompiler: getInput("c-compiler"),
cxxCompiler: getInput("cxx-compiler"),
cFlags: getMultilineInput("c-flags").join(" "),
cxxFlags: getMultilineInput("cxx-flags").join(" "),
options: getMultilineInput("options").flatMap((opts) => opts.split(" ")),
args: getMultilineInput("args").flatMap((args) => args.split(" ")),
runBuild: getBooleanInput("run-build"),
buildArgs: getMultilineInput("build-args").flatMap((args) =>
args.split(" "),
),
cFlags: getInput("c-flags").replaceAll(/\s+/g, " "),
cxxFlags: getInput("cxx-flags").replaceAll(/\s+/g, " "),
options: getInput("options")
.split(/\s+/)
.filter((arg) => arg != ""),
args: getInput("args")
.split(/\s+/)
.filter((arg) => arg != ""),
runBuild: getInput("run-build") == "true",
buildArgs: getInput("build-args")
.split(/\s+/)
.filter((arg) => arg != ""),
};
}