From 350e2b14c139d1cb835b945508091aace2987036 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Tue, 6 Aug 2024 16:34:30 +0700 Subject: [PATCH] refactor: replace `core.getMultilineInput` with `getInput` --- dist/index.js | 17 +++++++++++------ src/inputs.test.ts | 40 ++++++++++++---------------------------- src/inputs.ts | 19 +++++++++++-------- 3 files changed, 34 insertions(+), 42 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7f970b3..995b403 100644 --- a/dist/index.js +++ b/dist/index.js @@ -26752,7 +26752,6 @@ const external_node_path_namespaceObject = __WEBPACK_EXTERNAL_createRequire(impo var external_node_path_default = /*#__PURE__*/__nccwpck_require__.n(external_node_path_namespaceObject); ;// CONCATENATED MODULE: ./src/inputs.ts - /** * Retrieves an action input. * @param key - The key of the action input. @@ -26770,12 +26769,18 @@ function getInputs() { generator: getInput("generator"), cCompiler: getInput("c-compiler"), cxxCompiler: getInput("cxx-compiler"), - cFlags: (0,core.getMultilineInput)("c-flags").join(" "), - cxxFlags: (0,core.getMultilineInput)("cxx-flags").join(" "), - options: (0,core.getMultilineInput)("options").flatMap((opts) => opts.split(" ")), - args: (0,core.getMultilineInput)("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: (0,core.getMultilineInput)("build-args").flatMap((args) => args.split(" ")), + buildArgs: getInput("build-args") + .split(/\s+/) + .filter((arg) => arg != ""), }; } diff --git a/src/inputs.test.ts b/src/inputs.test.ts index 6ea7f29..d171513 100644 --- a/src/inputs.test.ts +++ b/src/inputs.test.ts @@ -1,16 +1,10 @@ -import { jest } from "@jest/globals"; import path from "node:path"; -import type { Inputs } from "./inputs.js"; - -jest.unstable_mockModule("@actions/core", () => ({ - getMultilineInput: jest.fn(), -})); +import { Inputs, getInputs } from "./inputs.js"; describe("get action inputs", () => { interface TestCase { name: string; env?: Record; - multilineInputs?: Record; expectedInputs?: Partial; } @@ -59,18 +53,18 @@ describe("get action inputs", () => { }, { 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"], @@ -78,7 +72,7 @@ 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"] }, }, { @@ -88,7 +82,7 @@ describe("get action inputs", () => { }, { 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"] }, }, { @@ -99,14 +93,12 @@ describe("get action inputs", () => { 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", - }, - 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"], + "INPUT_BUILD-ARGS": "--target foo\n--parallel 8", }, expectedInputs: { sourceDir: "project", @@ -126,20 +118,12 @@ 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 prevEnv = process.env; process.env = { ...process.env, ...testCase.env, }; - const multilineInputs = { ...testCase.multilineInputs }; - jest.mocked(core.getMultilineInput).mockImplementation((name) => { - return multilineInputs[name] ?? []; - }); - expect(getInputs()).toStrictEqual({ sourceDir: "", buildDir: "build", diff --git a/src/inputs.ts b/src/inputs.ts index 39d46c5..f4a01eb 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -1,4 +1,3 @@ -import { getMultilineInput } from "@actions/core"; import path from "node:path"; export interface Inputs { @@ -33,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(" ")), + 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: getMultilineInput("build-args").flatMap((args) => - args.split(" "), - ), + buildArgs: getInput("build-args") + .split(/\s+/) + .filter((arg) => arg != ""), }; }