Skip to content

Commit

Permalink
Support custom Working Directory (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- authored Oct 21, 2024
2 parents e540c36 + 3d82c7a commit 17f5bae
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following inputs are supported
| `annotations` | Annotate the project code with the knip results. | `false` | `true` |
| `verbose` | Include annotated items in the comment report. | `false` | `false` |
| `ignore_results` | Do not fail the action run if knip results are found. | `false` | `false` |
| `working_directory` | Run knip in a different directory. | `false` | `.` |

### Issues

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ inputs:
description: Do not fail the action run if knip results are found.
default: false
required: false
working_directory:
description: Directory in which to run the knip action.
default: "."
required: false
runs:
using: node20
main: dist/index.mjs
50 changes: 35 additions & 15 deletions dist/index.mjs

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

10 changes: 10 additions & 0 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ describe("Action", () => {
annotations: actionInputs.annotations?.default,
verbose: actionInputs.verbose?.default,
ignore_results: actionInputs.ignore_results?.default,
working_directory: actionInputs.working_directory?.default,
};

vi.spyOn(core, "getInput").mockImplementation((input: string) => {
switch (input) {
case "token":
case "command_script_name":
case "comment_id":
case "working_directory":
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return mockEnvConfig[input];
default:
Expand Down Expand Up @@ -73,6 +75,7 @@ describe("Action", () => {
actionInputs.comment_id?.default.replaceAll(/\s/g, "-"),
);
expect(config.ignoreResults).toStrictEqual(actionInputs.ignore_results?.default);
expect(config.workingDirectory).toStrictEqual(actionInputs.working_directory?.default);
});

describe("custom values", () => {
Expand Down Expand Up @@ -131,6 +134,13 @@ describe("Action", () => {

expect(config.ignoreResults).toStrictEqual(true);
});

it("should load a custom value for workingDirectory", () => {
mockEnvConfig.working_directory = "some_directory";
const config: ActionConfig = getConfig();

expect(config.workingDirectory).toStrictEqual("some_directory");
});
});
});

Expand Down
7 changes: 7 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export interface ActionConfig {
* Do not fail the action run if knip results are found.
*/
ignoreResults: boolean;

/**
* Directory in which to run the knip action.
*/
workingDirectory?: string;
}

export function getConfig(): ActionConfig {
Expand All @@ -43,6 +48,7 @@ export function getConfig(): ActionConfig {
annotations: core.getBooleanInput("annotations", { required: false }),
verbose: core.getBooleanInput("verbose", { required: false }),
ignoreResults: core.getBooleanInput("ignore_results", { required: false }),
workingDirectory: core.getInput("working_directory", { required: false }) || undefined,
};
}

Expand All @@ -54,5 +60,6 @@ export function configToStr(cfg: ActionConfig): string {
annotations: ${cfg.annotations}
verbose: ${cfg.verbose}
ignoreResults: ${cfg.ignoreResults}
workingDirectory: ${cfg.workingDirectory}
`;
}
1 change: 1 addition & 0 deletions src/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe("API", () => {
annotations: true,
verbose: false,
ignoreResults: false,
workingDirectory: ".",
};

beforeEach(() => {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function run(): Promise<void> {
config.commandScriptName,
config.annotations,
config.verbose,
config.workingDirectory,
);

await runCommentTask(
Expand Down
20 changes: 17 additions & 3 deletions src/tasks/knip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cp from "node:child_process";

import * as core from "@actions/core";
import * as ni from "@antfu/ni";
import { afterAll, describe, expect, it, vi } from "vitest";
import { afterAll, afterEach, describe, expect, it, vi, type MockInstance } from "vitest";

import { invalidReportJson, reportJson } from "./__fixtures__/knip.fixture.ts";
import {
Expand Down Expand Up @@ -38,6 +38,12 @@ describe("knip", () => {
});

describe("buildRunKnipCommand", () => {
let niGetCliCommandMock: MockInstance<typeof ni.getCliCommand> | undefined;

afterEach(() => {
niGetCliCommandMock?.mockRestore();
});

it("should build a command with extended JSON reporter", async () => {
const cmd = await buildRunKnipCommand("knip");

Expand All @@ -46,12 +52,20 @@ describe("knip", () => {
});

it("should throw if a command could not be generated", async () => {
vi.spyOn(ni, "getCliCommand").mockResolvedValue(undefined);
niGetCliCommandMock = vi.spyOn(ni, "getCliCommand").mockResolvedValue(undefined);

await expect(async () => buildRunKnipCommand("knip")).rejects.toThrowError(
await expect(buildRunKnipCommand("knip")).rejects.toThrowError(
"Unable to generate command for package manager",
);
});

it("should build a command with working directory specific", async () => {
const cmd = await buildRunKnipCommand("knip", "./working/directory");

expect(cmd).toMatch("knip");
expect(cmd).toMatch("--reporter json");
expect(cmd).toMatch("--directory ./working/directory");
});
});

describe("run", () => {
Expand Down
11 changes: 8 additions & 3 deletions src/tasks/knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import { GITHUB_COMMENT_MAX_COMMENT_LENGTH } from "../api.ts";
import { timeTask } from "./task.ts";
import type { ItemMeta } from "./types.ts";

export async function buildRunKnipCommand(buildScriptName: string): Promise<string> {
const cmd = await getCliCommand(parseNr, [buildScriptName, "--reporter json"], {
export async function buildRunKnipCommand(buildScriptName: string, cwd?: string): Promise<string> {
const knipArgs = [buildScriptName, "--reporter json"];
if (cwd) {
knipArgs.push(`--directory ${cwd}`);
}
const cmd = await getCliCommand(parseNr, knipArgs, {
programmatic: true,
});
if (!cmd) {
Expand Down Expand Up @@ -548,11 +552,12 @@ export async function runKnipTasks(
buildScriptName: string,
annotationsEnabled: boolean,
verboseEnabled: boolean,
cwd?: string,
): Promise<{ sections: string[]; annotations: ItemMeta[] }> {
const taskMs = Date.now();
core.info("- Running Knip tasks");

const cmd = await timeTask("Build knip command", () => buildRunKnipCommand(buildScriptName));
const cmd = await timeTask("Build knip command", () => buildRunKnipCommand(buildScriptName, cwd));
const output = await timeTask("Run knip", async () => getJsonFromOutput(await run(cmd)));
const report = await timeTask("Parse knip report", () =>
Promise.resolve(parseJsonReport(output)),
Expand Down

0 comments on commit 17f5bae

Please sign in to comment.