Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions __tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,84 @@ describe("cli tests", () => {
expect(runFileLint).not.toHaveBeenCalled();
expect(process.exitCode).toBe(1);
});

test("rejects --threads combined with --stdin before validating its value", async () => {
const runFileLint = jest.fn().mockResolvedValue({ exitCode: 0 });
const runStdinLint = jest.fn().mockReturnValue({ exitCode: 0 });
jest.doMock("../src/cli/run-lint", () => ({
runFileLint,
runStdinLint,
}));
const mockError = jest.spyOn(console, "error").mockImplementation();
const { runCli } = require("../src/lint-md");

process.exitCode = undefined;
runCli(["node", "lint-md", "--stdin", "--threads", "abc"]);
await new Promise<void>((resolve) => setImmediate(resolve));

expect(mockError).toHaveBeenCalledWith(
expect.stringContaining(
"[lint-md] --threads cannot be used with --stdin."
)
);
expect(mockError).not.toHaveBeenCalledWith(
expect.stringContaining("INVALID_THREADS")
);
expect(runStdinLint).not.toHaveBeenCalled();
expect(process.exitCode).toBe(1);
});

test("rejects --max-file-size combined with --stdin", async () => {
const runFileLint = jest.fn().mockResolvedValue({ exitCode: 0 });
const runStdinLint = jest.fn().mockReturnValue({ exitCode: 0 });
jest.doMock("../src/cli/run-lint", () => ({
runFileLint,
runStdinLint,
}));
const mockError = jest.spyOn(console, "error").mockImplementation();
const { runCli } = require("../src/lint-md");

process.exitCode = undefined;
runCli(["node", "lint-md", "--stdin", "--max-file-size", "5mb"]);
await new Promise<void>((resolve) => setImmediate(resolve));

expect(mockError).toHaveBeenCalledWith(
expect.stringContaining(
"[lint-md] --max-file-size cannot be used with --stdin."
)
);
expect(runStdinLint).not.toHaveBeenCalled();
expect(process.exitCode).toBe(1);
});

test("aggregates all file-only options rejected with --stdin", async () => {
const runFileLint = jest.fn().mockResolvedValue({ exitCode: 0 });
const runStdinLint = jest.fn().mockReturnValue({ exitCode: 0 });
jest.doMock("../src/cli/run-lint", () => ({
runFileLint,
runStdinLint,
}));
const mockError = jest.spyOn(console, "error").mockImplementation();
const { runCli } = require("../src/lint-md");

process.exitCode = undefined;
runCli([
"node",
"lint-md",
"--stdin",
"--threads",
"4",
"--max-file-size",
"5mb",
]);
await new Promise<void>((resolve) => setImmediate(resolve));

expect(mockError).toHaveBeenCalledWith(
expect.stringContaining(
"[lint-md] The following options cannot be used with --stdin:\n--threads\n--max-file-size"
)
);
expect(runStdinLint).not.toHaveBeenCalled();
expect(process.exitCode).toBe(1);
});
});
35 changes: 22 additions & 13 deletions __tests__/threads-validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const TSX = path.resolve(__dirname, "../node_modules/tsx/dist/cli.mjs");
const CLI = path.resolve(__dirname, "../src/lint-md.ts");

describe("--threads validation across CLI paths", () => {
test("stdin + --threads abc → exit 1 + stderr", () => {
test("stdin + --threads abc → rejected as a conflicting option, exit 1", () => {
try {
execFileSync(
process.execPath,
Expand All @@ -21,7 +21,9 @@ describe("--threads validation across CLI paths", () => {
throw new Error("should have thrown");
} catch (e: any) {
expect(e.status).toBe(1);
expect(e.stderr).toContain("--threads must be a positive integer");
expect(e.stderr).toContain(
"[lint-md] --threads cannot be used with --stdin."
);
}
});

Expand All @@ -42,17 +44,24 @@ describe("--threads validation across CLI paths", () => {
}
});

test("stdin + --threads auto → does not exit 1 (numeric validation skipped)", () => {
const result = execFileSync(
process.execPath,
[TSX, CLI, "--stdin", "--threads", "auto"],
{
input: "# title\n",
encoding: "utf8",
stdio: ["pipe", "pipe", "pipe"],
}
);
expect(result).toContain("Done in");
test("stdin + --threads auto → rejected as a conflicting option, exit 1", () => {
try {
execFileSync(
process.execPath,
[TSX, CLI, "--stdin", "--threads", "auto"],
{
input: "# title\n",
encoding: "utf8",
stdio: ["pipe", "pipe", "pipe"],
}
);
throw new Error("should have thrown");
} catch (e: any) {
expect(e.status).toBe(1);
expect(e.stderr).toContain(
"[lint-md] --threads cannot be used with --stdin."
);
}
});

test("files + --threads auto → exit 0 on a small markdown file", () => {
Expand Down
25 changes: 25 additions & 0 deletions src/lint-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ export const createProgram = (): Command => {
);
}

// --threads and --max-file-size only affect file linting. Reject them
// here instead of silently accepting options that would do nothing.
const conflictingOptions = [
[threads !== undefined, "--threads"],
[maxFileSize !== undefined, "--max-file-size"],
]
.filter(([present]) => present)
.map(([, name]) => name);

if (stdin && conflictingOptions.length === 1) {
throw new CliError(
"CONFLICTING_INPUT",
`[lint-md] ${conflictingOptions[0]} cannot be used with --stdin.`
);
}

if (stdin && conflictingOptions.length > 1) {
throw new CliError(
"CONFLICTING_INPUT",
`[lint-md] The following options cannot be used with --stdin:\n${conflictingOptions.join(
"\n"
)}`
);
}

if (isDev) {
console.log(`dev -- version: ${version}, ${new Date().toString()}`);
}
Expand Down