From d5238b21ac70fff0d361032de5301c5f651424ea Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sat, 23 Mar 2024 00:12:39 +0700 Subject: [PATCH] test: add test for `configureProject` function --- src/cmake.test.ts | 163 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/cmake.test.ts diff --git a/src/cmake.test.ts b/src/cmake.test.ts new file mode 100644 index 0000000..ee85b45 --- /dev/null +++ b/src/cmake.test.ts @@ -0,0 +1,163 @@ +import { jest } from "@jest/globals"; +import type { Inputs } from "./inputs.js"; + +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", + inputs: defaultInputs, + expectedArgs: [".", "-B", "build"], + }, + { + name: "with source directory specified", + inputs: { + ...defaultInputs, + sourceDir: "project", + }, + expectedArgs: ["project", "-B", "build"], + }, + { + name: "with build directory specified", + inputs: { + ...defaultInputs, + buildDir: "output", + }, + expectedArgs: [".", "-B", "output"], + }, + { + name: "with generator specified", + inputs: { + ...defaultInputs, + generator: "Ninja", + }, + expectedArgs: [".", "-B", "build", "-G", "Ninja"], + }, + { + name: "with C compiler specified", + inputs: { + ...defaultInputs, + cCompiler: "clang", + }, + expectedArgs: [".", "-B", "build", "-DCMAKE_C_COMPILER=clang"], + }, + { + name: "with C++ compiler specified", + inputs: { + ...defaultInputs, + cxxCompiler: "clang++", + }, + expectedArgs: [".", "-B", "build", "-DCMAKE_CXX_COMPILER=clang++"], + }, + { + name: "with C flags specified", + inputs: { + ...defaultInputs, + cFlags: "-Werror -Wall", + }, + expectedArgs: [".", "-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"], + }, + { + name: "with C++ flags specified", + inputs: { + ...defaultInputs, + cxxFlags: "-Werror -Wall -Wextra", + }, + expectedArgs: [ + ".", + "-B", + "build", + "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra", + ], + }, + { + name: "with additional options specified", + inputs: { + ...defaultInputs, + options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], + }, + expectedArgs: [ + ".", + "-B", + "build", + "-DBUILD_TESTING=ON", + "-DBUILD_EXAMPLES=ON", + ], + }, + { + name: "with additional arguments specified", + inputs: { + ...defaultInputs, + args: ["-Wdev", "-Wdeprecated"], + }, + expectedArgs: [".", "-B", "build", "-Wdev", "-Wdeprecated"], + }, + { + name: "with all specified", + inputs: { + ...defaultInputs, + sourceDir: "project", + buildDir: "output", + generator: "Ninja", + cCompiler: "clang", + cxxCompiler: "clang++", + cFlags: "-Werror -Wall", + cxxFlags: "-Werror -Wall -Wextra", + options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], + args: ["-Wdev", "-Wdeprecated"], + }, + expectedArgs: [ + "project", + "-B", + "output", + "-G", + "Ninja", + "-DCMAKE_C_COMPILER=clang", + "-DCMAKE_CXX_COMPILER=clang++", + "-DCMAKE_C_FLAGS=-Werror -Wall", + "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra", + "-DBUILD_TESTING=ON", + "-DBUILD_EXAMPLES=ON", + "-Wdev", + "-Wdeprecated", + ], + }, + ]; + + for (const { name, inputs, expectedArgs } of testCases) { + it(`should execute the correct command ${name}`, async () => { + const { configureProject } = await import("./cmake.js"); + const { exec } = await import("@actions/exec"); + + jest.mocked(exec).mockReset(); + + await expect(configureProject(inputs)).resolves.toBeUndefined(); + + expect(exec).toHaveBeenCalledTimes(1); + expect(exec).toHaveBeenLastCalledWith("cmake", expectedArgs); + }); + } +});