/
reuse.spec.ts
194 lines (177 loc) · 7.32 KB
/
reuse.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { platform, scm } from '../../../../../test/util';
import type { Pr } from '../../../../modules/platform';
import type { BranchConfig } from '../../../types';
import { shouldReuseExistingBranch } from './reuse';
describe('workers/repository/update/branch/reuse', () => {
describe('shouldReuseExistingBranch(config)', () => {
const pr: Pr = {
number: 42,
sourceBranch: 'master',
state: 'open',
title: 'any',
};
let config: BranchConfig;
beforeEach(() => {
config = {
manager: 'some-manager',
branchName: 'renovate/some-branch',
baseBranch: 'base',
rebaseLabel: 'rebase',
rebaseWhen: 'behind-base-branch',
upgrades: [],
};
jest.resetAllMocks();
});
it('returns false if branch does not exist', async () => {
scm.branchExists.mockResolvedValueOnce(false);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeFalse();
});
it('returns true if no PR', async () => {
scm.branchExists.mockResolvedValueOnce(true);
platform.getBranchPr.mockResolvedValue(null);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
it('returns true if does not need rebasing', async () => {
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(false);
platform.getBranchPr.mockResolvedValueOnce(pr);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
it('returns false if does not need rebasing but has upgrades that need lockfile maintenance along with upgrades that do not', async () => {
config.upgrades = [
{
manager: 'some-manager',
packageFile: 'package.json',
rangeStrategy: 'replace',
branchName: 'current',
},
{
manager: 'some-manager',
packageFile: 'package.json',
rangeStrategy: 'update-lockfile',
branchName: 'current',
},
{
manager: 'some-manager',
packageFile: 'package.json',
rangeStrategy: 'in-range-only',
branchName: 'current',
},
];
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(false);
platform.getBranchPr.mockResolvedValueOnce(pr);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBe(false);
});
it('returns true if does not need rebasing and lockfile update is on different packages', async () => {
config.upgrades = [
{
manager: 'some-manager',
packageFile: 'package.json',
rangeStrategy: 'replace',
branchName: 'current',
},
{
manager: 'some-manager',
packageFile: 'subpackage/package.json',
rangeStrategy: 'update-lockfile',
branchName: 'current',
},
];
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(false);
platform.getBranchPr.mockResolvedValueOnce(pr);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBe(true);
});
it('returns true if unmergeable and cannot rebase', async () => {
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(true);
platform.getBranchPr.mockResolvedValueOnce(pr);
scm.isBranchModified.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
it('returns true if unmergeable and can rebase, but rebaseWhen is never', async () => {
config.rebaseWhen = 'never';
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(true);
platform.getBranchPr.mockResolvedValueOnce(pr);
scm.isBranchModified.mockResolvedValueOnce(false);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
it('returns false if unmergeable and can rebase', async () => {
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(true);
platform.getBranchPr.mockResolvedValueOnce(pr);
scm.isBranchModified.mockResolvedValueOnce(false);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeFalse();
});
it('returns true if automerge branch and not stale', async () => {
config.automerge = true;
config.automergeType = 'branch';
scm.branchExists.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
it('returns false if automerge branch and stale', async () => {
config.rebaseWhen = 'auto';
config.automerge = true;
config.automergeType = 'branch';
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchBehindBase.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeFalse();
});
it('returns true if rebaseWhen=behind-base-branch but cannot rebase', async () => {
config.rebaseWhen = 'behind-base-branch';
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchBehindBase.mockResolvedValueOnce(true);
scm.isBranchConflicted.mockResolvedValueOnce(true);
platform.getBranchPr.mockResolvedValueOnce(pr);
scm.isBranchModified.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
it('returns false if automerge pr and stale', async () => {
config.rebaseWhen = 'auto';
config.automerge = true;
config.automergeType = 'pr';
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchBehindBase.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeFalse();
});
it('returns false if getRepoForceRebase and stale', async () => {
config.rebaseWhen = 'auto';
platform.getRepoForceRebase.mockResolvedValueOnce(true);
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchBehindBase.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeFalse();
});
it('returns true if automerge, rebaseWhen=never and stale', async () => {
config.rebaseWhen = 'never';
config.automerge = true;
scm.branchExists.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
expect(scm.isBranchBehindBase).not.toHaveBeenCalled();
expect(scm.isBranchModified).not.toHaveBeenCalled();
});
it('returns true if automerge, rebaseWhen=conflicted and stale', async () => {
config.rebaseWhen = 'conflicted';
config.automerge = true;
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchBehindBase.mockResolvedValueOnce(true);
const res = await shouldReuseExistingBranch(config);
expect(res.reuseExistingBranch).toBeTrue();
});
});
});