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
14 changes: 14 additions & 0 deletions __tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ describe("cli tests", () => {
expect(mockExit).toHaveBeenCalledWith(0);
});

test("skips lint setup when no input is provided", async () => {
const getLintConfig = jest.fn();
jest.doMock("../src/utils/configure", () => ({ getLintConfig }));
const mockExit = jest.spyOn(process, "exit").mockImplementation((() => {
throw new Error("process.exit");
}) as never);
const { main } = require("../src/lint-md");

await expect(main(["node", "lint-md"])).rejects.toThrow("process.exit");

expect(getLintConfig).not.toHaveBeenCalled();
expect(mockExit).toHaveBeenCalledWith(0);
});

test("rejects file arguments combined with --stdin", async () => {
const runFileLint = jest.fn().mockResolvedValue({ exitCode: 0 });
const runStdinLint = jest.fn().mockReturnValue({ exitCode: 0 });
Expand Down
12 changes: 7 additions & 5 deletions src/lint-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export const createProgram = (): Command => {
const isFixMode = Boolean(fix);
const isDev = Boolean(dev);

// No input at all: show help before touching config, threads, or
// stdin. A broken .lintmdrc must not turn bare "lint-md" into a
// config error.
if (!files.length && !stdin) {
program.help();
}

// Fail before touching stdin or the file list so neither input mode
// starts on a command that mixes both.
if (stdin && files.length > 0) {
Expand Down Expand Up @@ -130,11 +137,6 @@ export const createProgram = (): Command => {
export const main = async (argv: string[] = process.argv): Promise<void> => {
const program = createProgram();
await program.parseAsync(argv);

const isStdin = argv.includes("--stdin") || argv.includes("-i");
if (!program.args.length && !isStdin) {
program.help();
}
};

export const runCli = (argv: string[] = process.argv): void => {
Expand Down