Skip to content

Commit e28b0d7

Browse files
committed
fix(output): pluralize number of merged branches
1 parent f5c9783 commit e28b0d7

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export function logError(prefix: string, error: unknown): void {
55
console.warn(`${prefix}: ${String(error)}`);
66
}
77
}
8+
9+
export function pluralize(count: number, words: [string, string]): string {
10+
return `${count} ${count === 1 ? words[0] : words[1]}`;
11+
}

src/output.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pluralize } from "./helpers.js";
12
import { fetchRemoteBranches, GitMergedConfig } from "./repo.js";
23
import { isValidURL } from "./validate.js";
34

@@ -39,7 +40,7 @@ export function outputMergedBranches(branches: string[], targetBranch: string, c
3940
return console.info(`No branches merged into '${targetBranch}'.`);
4041
}
4142

42-
console.info(`${branches.length} branches merged into '${targetBranch}':`)
43+
console.info(`${pluralize(branches.length, ["branch", "branches"])} merged into '${targetBranch}':`)
4344
console.info(formatTaskBranches(branches, config).join("\n"));
4445

4546
const remoteBranches = fetchRemoteBranches("origin");

src/tests/output.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe("outputMergedBranches", () => {
9393

9494
it("should log the correct branches when there are remote merged branches", () => {
9595
const branches = ["feat/TOKEN-800_new-feature", "fix/TOKEN-123_some-fix"];
96-
vi.spyOn(repoMethods, "fetchRemoteBranches").mockReturnValue(branches);
96+
const spy = vi.spyOn(repoMethods, "fetchRemoteBranches").mockReturnValue(branches);
9797

9898
outputMergedBranches(branches, "master", DEFAULT_CONFIG);
9999
expect(infoSpy).toHaveBeenNthCalledWith(1, "2 branches merged into 'master':");
@@ -110,8 +110,24 @@ describe("outputMergedBranches", () => {
110110
expect(infoSpy).toHaveBeenNthCalledWith(5, `remotely:\n ${remoteDelete}`);
111111
expect(infoSpy).toHaveBeenCalledTimes(5);
112112
expect(warnSpy).not.toHaveBeenCalled();
113+
114+
spy.mockRestore();
113115
});
114116

117+
it("should log the correct messages when there is single local merged branch", () => {
118+
const branches = ["feat/TOKEN-800_new-feature"];
119+
120+
outputMergedBranches(branches, "master", DEFAULT_CONFIG);
121+
expect(infoSpy).toHaveBeenNthCalledWith(1, "1 branch merged into 'master':");
122+
expect(infoSpy).toHaveBeenNthCalledWith(2, "feat/TOKEN-800_new-feature <https://test-instance.org/browse/TOKEN-800>");
123+
124+
const localDelete = `git branch --delete ${branches.join(" ")}`;
125+
expect(infoSpy).toHaveBeenNthCalledWith(3, "\nRun the following to delete branches:");
126+
expect(infoSpy).toHaveBeenNthCalledWith(4, `locally:\n ${localDelete}`);
127+
expect(infoSpy).toHaveBeenCalledTimes(4);
128+
expect(warnSpy).not.toHaveBeenCalled();
129+
})
130+
115131
it("should log a message when no branches are merged", () => {
116132
outputMergedBranches([], "master", DEFAULT_CONFIG);
117133
expect(infoSpy).toHaveBeenCalledWith("No branches merged into 'master'.");

0 commit comments

Comments
 (0)