cmake-action/src/context.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

206 lines
5.4 KiB
TypeScript

import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import type { Context } from "./context.js";
vi.mock("gha-utils", () => ({ getInput: vi.fn() }));
describe("get action context", () => {
interface TestCase {
name: string;
inputs?: Record<string, string>;
expectedContext?: Partial<Context>;
}
const testCases: TestCase[] = [
{
name: "with nothing specified",
},
{
name: "with source directory specified",
inputs: { "source-dir": "project" },
expectedContext: {
sourceDir: "project",
buildDir: path.join("project", "build"),
},
},
{
name: "with build directory specified",
inputs: { "build-dir": "output" },
expectedContext: { buildDir: "output" },
},
{
name: "with source and build directories specified",
inputs: {
"source-dir": "project",
"build-dir": "output",
},
expectedContext: {
sourceDir: "project",
buildDir: "output",
},
},
{
name: "with generator specified",
inputs: { generator: "Ninja" },
expectedContext: {
configure: {
generator: "Ninja",
options: [],
args: [],
},
},
},
{
name: "with C compiler specified",
inputs: { "c-compiler": "clang" },
expectedContext: {
configure: {
generator: "",
options: ["CMAKE_C_COMPILER=clang"],
args: [],
},
},
},
{
name: "with C++ compiler specified",
inputs: { "cxx-compiler": "clang++" },
expectedContext: {
configure: {
generator: "",
options: ["CMAKE_CXX_COMPILER=clang++"],
args: [],
},
},
},
{
name: "with C flags specified",
inputs: { "c-flags": "-Werror -Wall\n-Wextra" },
expectedContext: {
configure: {
generator: "",
options: ["CMAKE_C_FLAGS=-Werror -Wall -Wextra"],
args: [],
},
},
},
{
name: "with C++ flags specified",
inputs: { "cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic" },
expectedContext: {
configure: {
generator: "",
options: ["CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic"],
args: [],
},
},
},
{
name: "with additional options specified",
inputs: {
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON "FOO=BAR BAZ"`,
},
expectedContext: {
configure: {
generator: "",
options: [
"BUILD_TESTING=ON",
"BUILD_EXAMPLES=ON",
"BUILD_DOCS=ON",
"FOO=BAR BAZ",
],
args: [],
},
},
},
{
name: "with additional arguments specified",
inputs: { args: `-Wdev -Wdeprecated\n--fresh --foo "bar baz"` },
expectedContext: {
configure: {
generator: "",
options: [],
args: ["-Wdev", "-Wdeprecated", "--fresh", "--foo", "bar baz"],
},
},
},
{
name: "with run build specified",
inputs: { "run-build": "true" },
expectedContext: { build: { enabled: true, args: [] } },
},
{
name: "with additional build arguments specified",
inputs: { "build-args": `--target foo\n--parallel 8 --foo "bar baz"` },
expectedContext: {
build: {
enabled: false,
args: ["--target", "foo", "--parallel", "8", "--foo", "bar baz"],
},
},
},
{
name: "with all specified",
inputs: {
"source-dir": "project",
"build-dir": "output",
generator: "Ninja",
"c-compiler": "clang",
"cxx-compiler": "clang++",
"c-flags": "-Werror -Wall\n-Wextra",
"cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic",
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON "FOO=BAR BAZ"`,
args: `-Wdev -Wdeprecated\n--fresh --foo "bar baz"`,
"run-build": "true",
"build-args": `--target foo\n--parallel 8 --foo "bar baz"`,
},
expectedContext: {
sourceDir: "project",
buildDir: "output",
configure: {
generator: "Ninja",
options: [
"CMAKE_C_COMPILER=clang",
"CMAKE_CXX_COMPILER=clang++",
"CMAKE_C_FLAGS=-Werror -Wall -Wextra",
"CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic",
"BUILD_TESTING=ON",
"BUILD_EXAMPLES=ON",
"BUILD_DOCS=ON",
"FOO=BAR BAZ",
],
args: ["-Wdev", "-Wdeprecated", "--fresh", "--foo", "bar baz"],
},
build: {
enabled: true,
args: ["--target", "foo", "--parallel", "8", "--foo", "bar baz"],
},
},
},
];
for (const testCase of testCases) {
it(`should get the action context ${testCase.name}`, async () => {
const { getInput } = await import("gha-utils");
const { getContext } = await import("./context.js");
const inputs = testCase.inputs || {};
vi.mocked(getInput).mockImplementation((name) => inputs[name] ?? "");
expect(getContext()).toStrictEqual({
sourceDir: "",
buildDir: "build",
configure: {
generator: "",
options: [],
args: [],
},
build: {
enabled: false,
args: [],
},
...testCase.expectedContext,
});
});
}
});