Skip to content

Commit a52ba97

Browse files
committed
Improve tests for getBackportBranchName
1 parent 611fdfe commit a52ba97

File tree

4 files changed

+98
-23
lines changed

4 files changed

+98
-23
lines changed

src/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.test.ts

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,78 @@ import { Commit } from '../../entrypoint.api';
22
import { ValidConfigOptions } from '../../options/options';
33
import { getBackportBranchName } from './getBackportBranchName';
44

5-
const commit = { sourcePullRequest: { number: 1234 } } as Commit;
6-
75
describe('getBackportBranchName', () => {
8-
it('returns the default name', () => {
9-
const name = getBackportBranchName({
10-
options: {
11-
backportBranchName: undefined,
12-
} as ValidConfigOptions,
13-
targetBranch: '7.x',
14-
commits: [commit],
6+
describe('when options.backportBranchName is not set', () => {
7+
it('returns the default name with PR number', () => {
8+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
9+
const name = getBackportBranchName({
10+
options: { backportBranchName: undefined } as ValidConfigOptions,
11+
targetBranch: '7.x',
12+
commits,
13+
});
14+
expect(name).toBe('backport/7.x/pr-1234');
15+
});
16+
17+
it('returns the default name with commit sha', () => {
18+
const commits = [{ sourceCommit: { sha: 'abcde' } }] as Commit[];
19+
const name = getBackportBranchName({
20+
options: { backportBranchName: undefined } as ValidConfigOptions,
21+
targetBranch: '7.x',
22+
commits,
23+
});
24+
expect(name).toBe('backport/7.x/commit-abcde');
1525
});
16-
expect(name).toBe('backport/7.x/pr-1234');
1726
});
1827

19-
it('returns a custom name', () => {
20-
const name = getBackportBranchName({
21-
options: {
22-
backportBranchName: 'bp/pull-{{sourcePullRequest.number}}',
23-
} as ValidConfigOptions,
24-
targetBranch: '7.x',
25-
commits: [commit],
28+
describe('template variables are supported when using options.backportBranchName', () => {
29+
it('{{targetBranch}}', () => {
30+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
31+
const name = getBackportBranchName({
32+
options: {
33+
backportBranchName: 'bp/target-{{targetBranch}}',
34+
} as ValidConfigOptions,
35+
targetBranch: '7.x',
36+
commits,
37+
});
38+
expect(name).toBe('bp/target-7.x');
39+
});
40+
41+
it('{{refValues}}', () => {
42+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
43+
const name = getBackportBranchName({
44+
options: {
45+
backportBranchName: 'bp/ref-{{refValues}}',
46+
} as ValidConfigOptions,
47+
targetBranch: '7.x',
48+
commits,
49+
});
50+
expect(name).toBe('bp/ref-pr-1234');
51+
});
52+
53+
it('{{sourcePullRequest.number}}', () => {
54+
const commits = [{ sourcePullRequest: { number: 1234 } }] as Commit[];
55+
const name = getBackportBranchName({
56+
options: {
57+
backportBranchName: 'bp/pr-{{sourcePullRequest.number}}',
58+
} as ValidConfigOptions,
59+
targetBranch: '7.x',
60+
commits,
61+
});
62+
expect(name).toBe('bp/pr-1234');
63+
});
64+
65+
it('{{sourcePullRequest.title}}', () => {
66+
const commits = [
67+
{ sourcePullRequest: { title: 'My PR title', number: 1234 } },
68+
] as Commit[];
69+
const name = getBackportBranchName({
70+
options: {
71+
backportBranchName: 'bp/pr-{{sourcePullRequest.title}}',
72+
} as ValidConfigOptions,
73+
targetBranch: '7.x',
74+
commits,
75+
});
76+
expect(name).toBe('bp/pr-My PR title');
2677
});
27-
expect(name).toBe('bp/pull-1234');
2878
});
2979
});

src/lib/cherrypickAndCreateTargetPullRequest/getBackportBranchName.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ export function getBackportBranchName({
2929
.join('_')
3030
.slice(0, 200);
3131

32+
const sourcePullRequest = commits[0].sourcePullRequest; // assume that all commits are from the same PR
3233
const defaultBackportBranchName = 'backport/{{targetBranch}}/{{refValues}}';
33-
const template = Handlebars.compile(
34-
options.backportBranchName ?? defaultBackportBranchName,
35-
);
3634

35+
const backportBranchName =
36+
options.backportBranchName ?? defaultBackportBranchName;
37+
38+
const template = Handlebars.compile(backportBranchName);
3739
return template({
38-
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
40+
sourcePullRequest,
3941
targetBranch,
4042
refValues,
4143
});

src/lib/github/v3/getPullRequest/getPullRequestBody.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ Please refer to the [Backport tool documentation](https://github.com/sorenlouv/b
449449
});
450450

451451
describe('handlebars', () => {
452-
it('should throw an error if Handlebars compilation fails', () => {
452+
it('should return default description if handlebars compilation fails', () => {
453453
const compileError = new Error('Simulated compile error');
454454

455455
// Stub Handlebars.compile to throw an error.

src/lib/github/v3/getPullRequest/getTitle.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Handlebars from 'handlebars';
12
import { Commit } from '../../../../entrypoint.api';
23
import { ValidConfigOptions } from '../../../../options/options';
34
import { getTitle } from './getTitle';
@@ -114,4 +115,26 @@ describe('getTitle', () => {
114115
getTitle({ options, commits, targetBranch: '7.x' }),
115116
).not.toThrow();
116117
});
118+
119+
it('should return default description if handlebars compilation fails', () => {
120+
const compileError = new Error('Simulated compile error');
121+
122+
// Stub Handlebars.compile to throw an error.
123+
const compileSpy = jest
124+
.spyOn(Handlebars, 'compile')
125+
.mockImplementation(() => {
126+
throw compileError;
127+
});
128+
129+
const options = {} as ValidConfigOptions;
130+
131+
expect(
132+
getTitle({ options, commits, targetBranch: '7.x' }),
133+
).toMatchInlineSnapshot(
134+
`"[7.x] My commit message (#55) | Another commit message (#56)"`,
135+
);
136+
137+
// Restore the stubs.
138+
compileSpy.mockRestore();
139+
});
117140
});

0 commit comments

Comments
 (0)