Skip to content

Commit ba79b24

Browse files
committed
Fix: do not fail when arguments are empty (fixes: #4)
1 parent 1da5c82 commit ba79b24

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import isGit from "is-git-repository";
22
import process from "node:process";
3-
import { execSync } from "child_process";
3+
import { execSync } from "node:child_process";
44

55
const cwd = process.cwd();
66
const defaultOptions = {
@@ -9,9 +9,13 @@ const defaultOptions = {
99
};
1010

1111
const sanitize = (input) => {
12-
if (!Array.isArray(input)) return input.replace(/[^a-zA-Z0-9-_]/g, "");
12+
if (Array.isArray(input)) return input.map(sanitize).join(" ").trim();
1313

14-
return input.map(sanitize).join(" ");
14+
if (typeof input !== "string") {
15+
return "";
16+
}
17+
18+
return input?.replace(/[^a-zA-Z0-9-_]/g, "");
1519
};
1620

1721
const branchName = (options = defaultOptions) => {

index.spec.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ import path from "node:path";
44
import process from "node:process";
55

66
import branchName from "./index";
7-
import { afterAll, beforeAll, describe, expect, it } from "vitest";
7+
import {
8+
afterAll,
9+
beforeAll,
10+
beforeEach,
11+
describe,
12+
expect,
13+
it,
14+
vi,
15+
} from "vitest";
16+
import { execSync } from "node:child_process";
817

918
const cwd = process.cwd();
1019
const fixtures = path.join(cwd, "test", "fixtures");
1120

1221
const folders = ["feat_test", "master"];
1322

23+
vi.mock("node:child_process", { spy: true });
24+
1425
describe("branchName", () => {
1526
beforeAll(() => {
1627
folders.map((folder) =>
@@ -21,6 +32,10 @@ describe("branchName", () => {
2132
);
2233
});
2334

35+
beforeEach(() => {
36+
vi.clearAllMocks();
37+
});
38+
2439
afterAll(() => {
2540
folders.map((folder) =>
2641
fs.renameSync(
@@ -30,6 +45,25 @@ describe("branchName", () => {
3045
);
3146
});
3247

48+
it("check the default", () => {
49+
branchName();
50+
51+
expect(execSync).toHaveBeenCalledWith("git branch", { cwd });
52+
});
53+
54+
it("check if values are properly ignored", () => {
55+
branchName({ cwd: path.join(fixtures, "master"), branchOptions: [] });
56+
branchName({ branchOptions: [null, 0, "--no-color"] });
57+
58+
expect(execSync).toHaveBeenCalledTimes(2);
59+
expect(execSync).toHaveBeenNthCalledWith(1, "git branch", {
60+
cwd: path.join(fixtures, "master"),
61+
});
62+
expect(execSync).toHaveBeenNthCalledWith(2, "git branch --no-color", {
63+
cwd,
64+
});
65+
});
66+
3367
it("check if the given directory is the branch master", () => {
3468
expect(
3569
branchName({
@@ -53,4 +87,8 @@ describe("branchName", () => {
5387
false,
5488
);
5589
});
90+
91+
it("check any non existing dir", () => {
92+
expect(branchName({ cwd: "ʕっ•ᴥ•ʔっ" })).toBe(false);
93+
});
5694
});

0 commit comments

Comments
 (0)