Skip to content

Commit

Permalink
test: extend action yaml loading tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 24, 2023
1 parent 9179e76 commit dd1ec61
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ inputs:
description: ID to use when updating the PR comment.
default: "${{ github.workflow }}-knip-reporter"
required: false
ignore_result:
ignore_results:
description: Do not fail the action run if knip results are found
default: false
required: false
Expand Down
2 changes: 1 addition & 1 deletion dist/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 57 additions & 13 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";

import * as core from "@actions/core";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { parse } from "yaml";

import { type ActionConfig, getConfig } from "./action.ts";

vi.mock("@actions/core");

describe("Action", () => {
describe("getConfig", () => {
// Represent the process.env inputs.
let actionInputs: Record<string, { description: string; default: string; required: boolean }>;
let mockEnvConfig: any;

beforeAll(async () => {
// Load the actual action yaml so we can properly assert the defaults
const rawYml = await readFile(resolve(__dirname, "..", "action.yml"));
const actionYml = parse(rawYml.toString());
actionInputs = actionYml.inputs;
});

beforeEach(() => {
mockEnvConfig = {};
mockEnvConfig = {
token: actionInputs.token?.default,
command_script_name: actionInputs.command_script_name?.default,
comment_id: actionInputs.comment_id?.default,
ignore_results: actionInputs.ignore_results?.default,
};

vi.spyOn(core, "getInput").mockImplementation((input: string) => {
switch (input) {
case "commandScriptName":
return mockEnvConfig.commandScriptName ?? "";
case "token":
case "command_script_name":
case "comment_id":
case "ignore_results":
return mockEnvConfig[input];
default:
throw new Error("invalid input requested");
throw new Error(`invalid input requested ${input}`);
}
});
});
Expand All @@ -27,18 +46,43 @@ describe("Action", () => {
vi.restoreAllMocks();
});

it("should return a valid config", () => {
it("should load the defaults from the yaml", () => {
const config: ActionConfig = getConfig();

// Assert that the numbers / types have been properly loaded.
expect(config.commandScriptName).toStrictEqual("knip");
expect(config.token).toStrictEqual(actionInputs.token?.default);
expect(config.commandScriptName).toStrictEqual(actionInputs.command_script_name?.default);
expect(config.commentId).toStrictEqual(actionInputs.comment_id?.default);
expect(config.ignoreResults).toStrictEqual(actionInputs.ignore_results?.default);
});

it("should provide a default command script name if none is supplied", () => {
mockEnvConfig.commandScriptName = "custom:knip";
const config: ActionConfig = getConfig();
describe("custom values", () => {
it("should load a custom value for token", () => {
mockEnvConfig.token = "newToken";
const config: ActionConfig = getConfig();

expect(config.token).toStrictEqual("newToken");
});

it("should load a custom value for commandScriptName", () => {
mockEnvConfig.command_script_name = "custom:knip";
const config: ActionConfig = getConfig();

expect(config.commandScriptName).toStrictEqual("custom:knip");
expect(config.commandScriptName).toStrictEqual("custom:knip");
});

it("should load a custom value for commentId", () => {
mockEnvConfig.comment_id = "special-comment";
const config: ActionConfig = getConfig();

expect(config.commentId).toStrictEqual("special-comment");
});

it("should load a custom value for ignoreResults", () => {
mockEnvConfig.ignore_results = "true";
const config: ActionConfig = getConfig();

expect(config.ignoreResults).toStrictEqual(true);
});
});
});
});
5 changes: 1 addition & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ export function getConfig(): ActionConfig {
token: core.getInput("token", { required: true }),
commandScriptName: core.getInput("command_script_name", { required: false }) || "knip",
commentId: core.getInput("comment_id", { required: true }),
ignoreResults: core.getInput("comment_id", { required: false }) === "true",
ignoreResults: core.getInput("ignore_results", { required: false }) === "true",
};
}

/**
* @param indent indentation multiplier
*/
export function configToStr(cfg: ActionConfig): string {
return ` with config:
token: ###
Expand Down

0 comments on commit dd1ec61

Please sign in to comment.