cmake-action/src/cmake.test.ts
Alfi Maulana db9fa0b745
Some checks are pending
Build / Build Package (push) Waiting to run
Check / Check Package (push) Waiting to run
Test / Test Package (push) Waiting to run
Test / Test Action (macos-14) (push) Waiting to run
Test / Test Action (ubuntu-24.04) (push) Waiting to run
Test / Test Action (windows-2022) (push) Waiting to run
Test / Test Action With Specified Directories (push) Waiting to run
Test / Test Action Without Run Build (push) Waiting to run
Test / Test Action With Additional Options (push) Waiting to run
Test / Test Action With Custom Generator (push) Waiting to run
test: use Vitest for testing (#564)
* test: use Vitest for testing

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* build: remove `esModuleInterop` option in TypeScript configuration

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
2025-01-03 22:53:57 +07:00

153 lines
3.7 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { buildProject, configureProject } from "./cmake.js";
import type { Context } from "./context.js";
import { exec } from "./exec.js";
interface TestCase {
name: string;
context?: Partial<Context>;
expectedArgs: string[];
}
const defaultContext: Context = {
sourceDir: "",
buildDir: "build",
configure: {
generator: "",
options: [],
args: [],
},
build: {
enabled: true,
args: [],
},
};
vi.mock("./exec.js", () => ({ exec: vi.fn() }));
describe("configure a CMake project", () => {
const testCases: TestCase[] = [
{
name: "with nothing specified",
expectedArgs: ["-B", "build"],
},
{
name: "with source directory specified",
context: { sourceDir: "project" },
expectedArgs: ["project", "-B", "build"],
},
{
name: "with build directory specified",
context: { buildDir: "output" },
expectedArgs: ["-B", "output"],
},
{
name: "with generator specified",
context: { configure: { generator: "Ninja", options: [], args: [] } },
expectedArgs: ["-B", "build", "-G", "Ninja"],
},
{
name: "with additional options specified",
context: {
configure: {
generator: "",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
args: [],
},
},
expectedArgs: [
"-B",
"build",
"-DBUILD_TESTING=ON",
"-DBUILD_EXAMPLES=ON",
],
},
{
name: "with additional arguments specified",
context: {
configure: {
generator: "",
options: [],
args: ["-Wdev", "-Wdeprecated"],
},
},
expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"],
},
{
name: "with all specified",
context: {
sourceDir: "project",
buildDir: "output",
configure: {
generator: "Ninja",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
args: ["-Wdev", "-Wdeprecated"],
},
},
expectedArgs: [
"project",
"-B",
"output",
"-G",
"Ninja",
"-DBUILD_TESTING=ON",
"-DBUILD_EXAMPLES=ON",
"-Wdev",
"-Wdeprecated",
],
},
];
for (const testCase of testCases) {
it(`should execute the correct command ${testCase.name}`, async () => {
vi.mocked(exec).mockReset();
await configureProject({ ...defaultContext, ...testCase.context });
expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenLastCalledWith("cmake", testCase.expectedArgs);
});
}
});
describe("build a CMake project", () => {
const testCases: TestCase[] = [
{
name: "with nothing specified",
expectedArgs: ["--build", "build"],
},
{
name: "with build directory specified",
context: { buildDir: "output" },
expectedArgs: ["--build", "output"],
},
{
name: "with additional arguments specified",
context: { build: { enabled: true, args: ["--target", "foo"] } },
expectedArgs: ["--build", "build", "--target", "foo"],
},
{
name: "with all specified",
context: {
buildDir: "output",
build: {
enabled: true,
args: ["--target", "foo"],
},
},
expectedArgs: ["--build", "output", "--target", "foo"],
},
];
for (const testCase of testCases) {
it(`should execute the correct command ${testCase.name}`, async () => {
vi.mocked(exec).mockReset();
await buildProject({ ...defaultContext, ...testCase.context });
expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenLastCalledWith("cmake", testCase.expectedArgs);
});
}
});