forked from renovatebot/renovate
/
repository-statistics.spec.ts
190 lines (173 loc) · 5.1 KB
/
repository-statistics.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
import { Fixtures } from '../../../../test/fixtures';
import {
RenovateConfig,
getConfig,
mockedFunction,
partial,
} from '../../../../test/util';
import { logger } from '../../../logger';
import { platform } from '../../../modules/platform';
import * as cache from '../../../util/cache/repository';
import type {
BaseBranchCache,
BranchCache,
BranchUpgradeCache,
RepoCacheData,
} from '../../../util/cache/repository/types';
import {
runBranchSummary,
runRenovateRepoStats,
} from './repository-statistics';
jest.mock('../../../modules/platform/github/pr');
jest.mock('../../../util/http/github');
const prJson = Fixtures.getJson('./pr-list.json');
const result = Object.keys(prJson).map((key) => {
return prJson[key];
});
describe('workers/repository/finalize/repository-statistics', () => {
let config: RenovateConfig;
describe('runRenovateRepoStats', () => {
beforeEach(() => {
config = getConfig();
mockedFunction(platform.getPrList).mockReturnValue(prJson);
config.repository = 'owner/repo';
});
it('Calls runRenovateRepoStats', () => {
runRenovateRepoStats(config, result);
expect(logger.debug).toHaveBeenCalledWith(
{
stats: {
total: 4,
open: 1,
closed: 1,
merged: 1,
},
},
`Renovate repository PR statistics`
);
});
});
describe('runBranchSummary', () => {
const getCacheSpy = jest.spyOn(cache, 'getCache');
const isCacheModifiedSpy = jest.spyOn(cache, 'isCacheModified');
const config: RenovateConfig = {};
it('processes cache with baseBranches only', () => {
const sha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseCache = partial<BaseBranchCache>({ sha });
const cache = partial<RepoCacheData>({
scan: { main: baseCache, dev: baseCache },
});
getCacheSpy.mockReturnValueOnce(cache);
isCacheModifiedSpy.mockReturnValueOnce(true);
runBranchSummary(config);
expect(logger.debug).toHaveBeenCalledWith(
{
cacheModified: true,
baseBranches: [
{
branchName: 'main',
sha,
},
{
branchName: 'dev',
sha,
},
],
branches: [],
inactiveBranches: [],
},
`Branch summary`
);
});
it('processes cache with baseBranches and branches', () => {
const sha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseBranchSha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseBranch = 'base-branch';
const defaultBranch = 'main';
const config: RenovateConfig = { defaultBranch };
const baseCache = partial<BaseBranchCache>({ sha });
const branchCache = partial<BranchCache>({
sha,
baseBranch,
baseBranchSha,
isModified: false,
automerge: false,
pristine: false,
upgrades: [],
});
const expectedMeta = {
automerge: branchCache.automerge,
baseBranch,
baseBranchSha,
branchSha: sha,
isModified: branchCache.isModified,
isPristine: branchCache.pristine,
};
const branches: BranchCache[] = [
{ ...branchCache, branchName: 'b1' },
{
...branchCache,
branchName: 'b2',
},
partial<BranchCache>({ branchName: 'b3' }),
];
const cache = partial<RepoCacheData>({
scan: { main: baseCache, dev: baseCache },
branches,
});
getCacheSpy.mockReturnValueOnce(cache);
isCacheModifiedSpy.mockReturnValueOnce(false);
runBranchSummary(config);
expect(logger.debug).toHaveBeenCalledWith(
{
baseBranches: [
{
branchName: 'main',
sha,
},
{
branchName: 'dev',
sha,
},
],
branches: [
{ ...expectedMeta, branchName: 'b1' },
{ ...expectedMeta, branchName: 'b2' },
],
cacheModified: false,
defaultBranch,
inactiveBranches: ['b3'],
},
`Branch summary`
);
});
it('logs extended branch info if branchSummaryExtended', () => {
const defaultBranch = 'main';
const config: RenovateConfig = {
defaultBranch,
branchSummaryExtended: true,
};
const branchCache = partial<BranchCache>({
result: 'done',
upgrades: partial<BranchUpgradeCache[]>([
{
datasource: 'npm',
depName: 'minimist',
currentValue: '1.2.3',
sourceUrl: 'someUrl',
depType: 'dependencies',
},
]),
});
const branches: BranchCache[] = [{ ...branchCache, branchName: 'b1' }];
const cache = partial<RepoCacheData>({
scan: {},
branches,
});
getCacheSpy.mockReturnValueOnce(cache);
isCacheModifiedSpy.mockReturnValueOnce(false);
runBranchSummary(config);
expect(logger.debug).toHaveBeenCalledTimes(2);
});
});
});