-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
common.spec.ts
267 lines (240 loc) · 8.25 KB
/
common.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { logger } from '../../../test/util';
import { defaultVersioning } from '../versioning';
import {
applyConstraintsFiltering,
applyExtractVersion,
applyVersionCompatibility,
filterValidVersions,
getDatasourceFor,
getDefaultVersioning,
isGetPkgReleasesConfig,
sortAndRemoveDuplicates,
} from './common';
import { CustomDatasource } from './custom';
import { NpmDatasource } from './npm';
import type { ReleaseResult } from './types';
describe('modules/datasource/common', () => {
describe('getDatasourceFor', () => {
it('returns null for unknown datasource', () => {
expect(getDatasourceFor('foobar')).toBeNull();
});
it('supports custom datasource', () => {
expect(getDatasourceFor('custom.foobar')).toEqual(
getDatasourceFor(CustomDatasource.id)
);
});
it('returns datasource for known datasource', () => {
expect(getDatasourceFor('npm')).toMatchObject({
id: NpmDatasource.id,
});
});
});
describe('getDefaultVersioning', () => {
it('returns default versioning for undefined datasource', () => {
expect(getDefaultVersioning(undefined)).toBe(defaultVersioning.id);
});
it('returns default versioning for unknown datasource', () => {
expect(getDefaultVersioning('foobar')).toBe(defaultVersioning.id);
expect(logger.logger.warn).toHaveBeenCalledWith(
{ datasourceName: 'foobar' },
'Missing datasource!'
);
});
it('returns default versioning for datasource with missing default versioning configuration', () => {
expect(getDefaultVersioning('artifactory')).toBe(defaultVersioning.id);
});
it('returns datasource-defined default versioning', () => {
expect(getDefaultVersioning('crate')).toBe('cargo');
});
});
describe('isGetPkgReleasesConfig', () => {
it('returns true for valid input', () => {
const input = {
datasource: 'npm',
packageName: 'lodash',
};
expect(isGetPkgReleasesConfig(input)).toBe(true);
});
it('returns false for invalid input', () => {
const input = {
datasource: '',
packageName: 'lodash',
};
expect(isGetPkgReleasesConfig(input)).toBe(false);
});
it('returns false for input with missing properties', () => {
const input = {
datasource: 'npm',
};
expect(isGetPkgReleasesConfig(input)).toBe(false);
});
it('returns false for input with non-string properties', () => {
const input = {
datasource: 123,
packageName: 'lodash',
};
expect(isGetPkgReleasesConfig(input)).toBe(false);
});
});
describe('applyExtractVersion', () => {
it('should return the same release result if extractVersion is not defined', () => {
const releaseResult: ReleaseResult = {
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
};
const res = applyExtractVersion(releaseResult, undefined);
expect(res).toBe(releaseResult);
});
it('should extract version from release using provided regex', () => {
const releaseResult: ReleaseResult = {
releases: [{ version: 'v1.0.0' }, { version: 'v2.0.0' }],
};
const res = applyExtractVersion(releaseResult, '^v(?<version>.+)$');
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
it('should return null for releases with invalid version', () => {
const releaseResult: ReleaseResult = {
releases: [{ version: 'v1.0.0' }, { version: 'invalid' }],
};
const result = applyExtractVersion(releaseResult, '^v(?<version>.+)$');
expect(result).toEqual({
releases: [{ version: '1.0.0' }],
});
});
});
describe('filterValidVersions', () => {
const releaseResult: ReleaseResult = {
releases: [
{ version: '1.0.0' },
{ version: '2.0.0' },
{ version: 'invalid' },
],
};
it('should filter out invalid versions', () => {
const config = { datasource: 'npm' };
const res = filterValidVersions(releaseResult, config);
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
it('should use default versioning if none is specified', () => {
const config = { datasource: 'foobar' };
const res = filterValidVersions(releaseResult, config);
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
it('should use specified versioning if provided', () => {
const config = { datasource: 'npm', versioning: 'semver' };
const res = filterValidVersions(releaseResult, config);
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
});
describe('sortAndRemoveDuplicates', () => {
it('sorts releases by version and removes duplicates', () => {
const config = { datasource: 'npm' };
const releaseResult: ReleaseResult = {
releases: [
{ version: '2.0.0' },
{ version: '1.0.0' },
{ version: '1.0.0' },
{ version: '3.0.0' },
],
};
const expected: ReleaseResult = {
releases: [
{ version: '1.0.0' },
{ version: '2.0.0' },
{ version: '3.0.0' },
],
};
const result = sortAndRemoveDuplicates(releaseResult, config);
expect(result).toEqual(expected);
});
it('uses default versioning if none is specified', () => {
const config = { datasource: 'foobar' };
const releaseResult: ReleaseResult = {
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
};
const result = sortAndRemoveDuplicates(releaseResult, config);
expect(result).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
expect(logger.logger.warn).toHaveBeenCalledWith(
{ datasourceName: 'foobar' },
'Missing datasource!'
);
});
});
describe('applyConstraintsFiltering', () => {
it('should remove constraints from releases if constraintsFiltering is not strict', () => {
const config = {
datasource: 'foo',
packageName: 'bar',
constraintsFiltering: 'none' as const,
};
const releaseResult: ReleaseResult = {
releases: [
{ version: '1.0.0', constraints: { foo: ['^1.0.0'] } },
{ version: '2.0.0', constraints: { foo: ['^2.0.0'] } },
],
};
expect(applyConstraintsFiltering(releaseResult, config)).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
it('should filter releases based on constraints if constraintsFiltering is strict', () => {
const config = {
datasource: 'foo',
packageName: 'bar',
constraintsFiltering: 'strict' as const,
constraints: { baz: '^1.0.0', qux: 'invalid' },
};
const releaseResult = {
releases: [
{ version: '1.0.0' },
{ version: '2.0.0', constraints: {} as never },
{ version: '3.0.0', constraints: { baz: ['^0.9.0'] } },
],
};
expect(applyConstraintsFiltering(releaseResult, config)).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
});
describe('applyVersionCompatibility', () => {
let input: ReleaseResult;
beforeEach(() => {
input = {
releases: [
{ version: '1.0.0' },
{ version: '2.0.0' },
{ version: '2.0.0-alpine' },
],
};
});
it('returns immediately if no versionCompatibility', () => {
const result = applyVersionCompatibility(input, undefined, undefined);
expect(result).toBe(input);
});
it('filters out non-matching', () => {
const versionCompatibility = '^(?<version>[^-]+)$';
expect(
applyVersionCompatibility(input, versionCompatibility, undefined)
).toMatchObject({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
it('filters out incompatible', () => {
const versionCompatibility = '^(?<version>[^-]+)(?<compatibility>.*)?$';
expect(
applyVersionCompatibility(input, versionCompatibility, '-alpine')
).toMatchObject({
releases: [{ version: '2.0.0' }],
});
});
});
});