Skip to content

Commit

Permalink
Merge pull request #178 from JoshuaKGoldberg/test-cli
Browse files Browse the repository at this point in the history
test: add coverage for cli.ts
  • Loading branch information
JoshuaKGoldberg authored Feb 18, 2024
2 parents 7bad1b2 + 5d21567 commit 06bb690
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 20 additions & 0 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it, vi } from "vitest";

import { shouldSemanticReleaseCLI } from "./cli.js";
import { shouldSemanticRelease } from "./shouldSemanticRelease.js";

vi.mock("./shouldSemanticRelease");

describe("cli", () => {
it("calls shouldSemanticRelease without verbose when --verbose is not provided", async () => {
await shouldSemanticReleaseCLI([]);

expect(shouldSemanticRelease).toHaveBeenCalledWith({ verbose: false });
});

it("calls shouldSemanticRelease with verbose when --verbose is provided", async () => {
await shouldSemanticReleaseCLI(["--verbose"]);

expect(shouldSemanticRelease).toHaveBeenCalledWith({ verbose: true });
});
});
37 changes: 37 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it, vi } from "vitest";

import { execOrThrow } from "./utils.js";

const mockExec = vi.fn();

vi.mock("node:child_process", () => ({
default: {
get exec() {
return mockExec;
},
},
}));

describe("execOrThrow", () => {
it("throws an error when exec prints to stderr", async () => {
mockExec.mockImplementation((command, callback) => {
callback(null, { stderr: "Oh no!" });
});

await expect(
async () => await execOrThrow("ls"),
).rejects.toMatchInlineSnapshot(`[Error: Oh no!]`);
});

it("returns stdout when exec doesn't print to stderr", async () => {
const stdout = "abc123";

mockExec.mockImplementation((command, callback) => {
callback(null, { stdout });
});

const actual = await execOrThrow("ls");

expect(actual).toEqual(stdout);
});
});
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import cp from "node:child_process";
import { promisify } from "node:util";

const exec = promisify(cp.exec);

export async function execOrThrow(command: string) {
const exec = promisify(cp.exec);
const { stderr, stdout } = await exec(command);

if (stderr) {
Expand Down

0 comments on commit 06bb690

Please sign in to comment.