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
38 changes: 38 additions & 0 deletions @commitlint/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,44 @@ const cli = (args: string[], options: TestOptions) => {
const gitBootstrap = (fixture: string) => git.bootstrap(fixture, __dirname);
const fixBootstrap = (fixture: string) => fix.bootstrap(fixture, __dirname);

test("should fail when --cwd references a non-existent directory", async () => {
const cwd = await gitBootstrap("fixtures/default");
const result = cli(["--cwd", "doesNotExist"], { cwd })("foo: bar");
const output = await result;
expect(output.stderr).toContain(
'The specified --cwd directory "doesNotExist" does not exist.',
);
expect(result.exitCode).toBe(ExitCode.CommitlintErrorDefault);
});

test("should fail when -d references a non-existent directory", async () => {
const cwd = await gitBootstrap("fixtures/default");
const result = cli(["-d", "doesNotExist"], { cwd })("foo: bar");
const output = await result;
expect(output.stderr).toContain(
'The specified --cwd directory "doesNotExist" does not exist.',
);
expect(result.exitCode).toBe(ExitCode.CommitlintErrorDefault);
});

test("should fail when --cwd references an existing file instead of a directory", async () => {
const cwd = await gitBootstrap("fixtures/default");
const filePath = path.join(cwd, "commitlint.config.js");
const result = cli(["--cwd", filePath], {
cwd,
})("foo: bar");
const output = await result;
expect(output.stderr).toContain("is not a directory");
expect(result.exitCode).toBe(ExitCode.CommitlintErrorDefault);
});

test("should succeed when --cwd references an existing directory", async () => {
const cwd = await gitBootstrap("fixtures/default");
const result = cli(["--cwd", cwd], { cwd })("type: bar");
await result;
expect(result.exitCode).toBe(ExitCode.CommitlintDefault);
});

test("should throw when called without [input]", async () => {
const cwd = await gitBootstrap("fixtures/default");
const result = cli([], { cwd })();
Expand Down
24 changes: 24 additions & 0 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
Expand Down Expand Up @@ -210,6 +211,29 @@ async function main(args: MainArgs): Promise<void> {
const raw = options._;
const flags = normalizeFlags(options);

try {
if (!fs.statSync(flags.cwd).isDirectory()) {
const err = new CliError(
`The specified --cwd path "${flags.cwd}" is not a directory.`,
pkg.name,
);
console.error(err.message);
throw err;
Comment thread
omar-y-abdi marked this conversation as resolved.
}
} catch (e) {
if (e instanceof CliError) {
throw e;
}
const code = (e as NodeJS.ErrnoException).code;
const message =
code === "ENOENT"
? `The specified --cwd directory "${flags.cwd}" does not exist.`
: `Cannot access the specified --cwd directory "${flags.cwd}": ${code ?? (e as Error).message}`;
const err = new CliError(message, pkg.name);
console.error(err.message);
throw err;
}

if (typeof options["print-config"] === "string") {
const loaded = await load(getSeed(flags), {
cwd: flags.cwd,
Expand Down