test: utilize partial test case inputs in cmake.test.ts (#272)

This commit is contained in:
Alfi Maulana 2024-03-25 11:52:35 +07:00 committed by GitHub
parent 21ddda7def
commit 6c4e2145fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,7 @@ import type { Inputs } from "./inputs.js";
interface TestCase { interface TestCase {
name: string; name: string;
inputs: Inputs; inputs?: Partial<Inputs>;
expectedArgs: string[]; expectedArgs: string[];
} }
@ -29,63 +29,41 @@ describe("configure a CMake project", () => {
const testCases: TestCase[] = [ const testCases: TestCase[] = [
{ {
name: "with nothing specified", name: "with nothing specified",
inputs: defaultInputs,
expectedArgs: [".", "-B", "build"], expectedArgs: [".", "-B", "build"],
}, },
{ {
name: "with source directory specified", name: "with source directory specified",
inputs: { inputs: { sourceDir: "project" },
...defaultInputs,
sourceDir: "project",
},
expectedArgs: ["project", "-B", "build"], expectedArgs: ["project", "-B", "build"],
}, },
{ {
name: "with build directory specified", name: "with build directory specified",
inputs: { inputs: { buildDir: "output" },
...defaultInputs,
buildDir: "output",
},
expectedArgs: [".", "-B", "output"], expectedArgs: [".", "-B", "output"],
}, },
{ {
name: "with generator specified", name: "with generator specified",
inputs: { inputs: { generator: "Ninja" },
...defaultInputs,
generator: "Ninja",
},
expectedArgs: [".", "-B", "build", "-G", "Ninja"], expectedArgs: [".", "-B", "build", "-G", "Ninja"],
}, },
{ {
name: "with C compiler specified", name: "with C compiler specified",
inputs: { inputs: { cCompiler: "clang" },
...defaultInputs,
cCompiler: "clang",
},
expectedArgs: [".", "-B", "build", "-DCMAKE_C_COMPILER=clang"], expectedArgs: [".", "-B", "build", "-DCMAKE_C_COMPILER=clang"],
}, },
{ {
name: "with C++ compiler specified", name: "with C++ compiler specified",
inputs: { inputs: { cxxCompiler: "clang++" },
...defaultInputs,
cxxCompiler: "clang++",
},
expectedArgs: [".", "-B", "build", "-DCMAKE_CXX_COMPILER=clang++"], expectedArgs: [".", "-B", "build", "-DCMAKE_CXX_COMPILER=clang++"],
}, },
{ {
name: "with C flags specified", name: "with C flags specified",
inputs: { inputs: { cFlags: "-Werror -Wall" },
...defaultInputs,
cFlags: "-Werror -Wall",
},
expectedArgs: [".", "-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"], expectedArgs: [".", "-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"],
}, },
{ {
name: "with C++ flags specified", name: "with C++ flags specified",
inputs: { inputs: { cxxFlags: "-Werror -Wall -Wextra" },
...defaultInputs,
cxxFlags: "-Werror -Wall -Wextra",
},
expectedArgs: [ expectedArgs: [
".", ".",
"-B", "-B",
@ -95,10 +73,7 @@ describe("configure a CMake project", () => {
}, },
{ {
name: "with additional options specified", name: "with additional options specified",
inputs: { inputs: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] },
...defaultInputs,
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
},
expectedArgs: [ expectedArgs: [
".", ".",
"-B", "-B",
@ -109,16 +84,12 @@ describe("configure a CMake project", () => {
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
inputs: { inputs: { args: ["-Wdev", "-Wdeprecated"] },
...defaultInputs,
args: ["-Wdev", "-Wdeprecated"],
},
expectedArgs: [".", "-B", "build", "-Wdev", "-Wdeprecated"], expectedArgs: [".", "-B", "build", "-Wdev", "-Wdeprecated"],
}, },
{ {
name: "with all specified", name: "with all specified",
inputs: { inputs: {
...defaultInputs,
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", generator: "Ninja",
@ -147,17 +118,18 @@ describe("configure a CMake project", () => {
}, },
]; ];
for (const { name, inputs, expectedArgs } of testCases) { for (const testCase of testCases) {
it(`should execute the correct command ${name}`, async () => { it(`should execute the correct command ${testCase.name}`, async () => {
const { configureProject } = await import("./cmake.js"); const { configureProject } = await import("./cmake.js");
const { exec } = await import("@actions/exec"); const { exec } = await import("@actions/exec");
jest.mocked(exec).mockReset(); jest.mocked(exec).mockReset();
await expect(configureProject(inputs)).resolves.toBeUndefined(); const prom = configureProject({ ...defaultInputs, ...testCase.inputs });
await expect(prom).resolves.toBeUndefined();
expect(exec).toHaveBeenCalledTimes(1); expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenLastCalledWith("cmake", expectedArgs); expect(exec).toHaveBeenLastCalledWith("cmake", testCase.expectedArgs);
}); });
} }
}); });
@ -166,29 +138,21 @@ describe("build a CMake project", () => {
const testCases: TestCase[] = [ const testCases: TestCase[] = [
{ {
name: "with nothing specified", name: "with nothing specified",
inputs: defaultInputs,
expectedArgs: ["--build", "build"], expectedArgs: ["--build", "build"],
}, },
{ {
name: "with build directory specified", name: "with build directory specified",
inputs: { inputs: { buildDir: "output" },
...defaultInputs,
buildDir: "output",
},
expectedArgs: ["--build", "output"], expectedArgs: ["--build", "output"],
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
inputs: { inputs: { buildArgs: ["--target", "foo"] },
...defaultInputs,
buildArgs: ["--target", "foo"],
},
expectedArgs: ["--build", "build", "--target", "foo"], expectedArgs: ["--build", "build", "--target", "foo"],
}, },
{ {
name: "with all specified", name: "with all specified",
inputs: { inputs: {
...defaultInputs,
buildDir: "output", buildDir: "output",
buildArgs: ["--target", "foo"], buildArgs: ["--target", "foo"],
}, },
@ -196,17 +160,18 @@ describe("build a CMake project", () => {
}, },
]; ];
for (const { name, inputs, expectedArgs } of testCases) { for (const testCase of testCases) {
it(`should execute the correct command ${name}`, async () => { it(`should execute the correct command ${testCase.name}`, async () => {
const { buildProject } = await import("./cmake.js"); const { buildProject } = await import("./cmake.js");
const { exec } = await import("@actions/exec"); const { exec } = await import("@actions/exec");
jest.mocked(exec).mockReset(); jest.mocked(exec).mockReset();
await expect(buildProject(inputs)).resolves.toBeUndefined(); const prom = buildProject({ ...defaultInputs, ...testCase.inputs });
await expect(prom).resolves.toBeUndefined();
expect(exec).toHaveBeenCalledTimes(1); expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenLastCalledWith("cmake", expectedArgs); expect(exec).toHaveBeenLastCalledWith("cmake", testCase.expectedArgs);
}); });
} }
}); });