/
index.spec.ts
135 lines (115 loc) · 3.9 KB
/
index.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
import { getOptions } from '../config/definitions';
import { loadModules } from '../util/modules';
import { isVersioningApiConstructor } from './common';
import { GenericVersion, GenericVersioningApi } from './loose/generic';
import * as semverVersioning from './semver';
import type { VersioningApi, VersioningApiConstructor } from './types';
import * as allVersioning from '.';
const supportedSchemes = getOptions().find(
(option) => option.name === 'versioning'
).allowedValues;
describe('allVersioning.get(versioning)', () => {
it('has api', () => {
expect(Object.keys(allVersioning.get('semver')).sort()).toMatchSnapshot();
});
it('validates', () => {
function validate(
module: VersioningApi | VersioningApiConstructor,
name: string
): boolean {
// eslint-disable-next-line new-cap
const mod = isVersioningApiConstructor(module) ? new module() : module;
// TODO: https://github.com/renovatebot/renovate/issues/9715
if (!mod.isValid || !mod.isVersion) {
throw Error(`Missing api on ${name}`);
}
return true;
}
const vers = allVersioning.getVersionings();
const loadedVers = loadModules(__dirname);
expect(Array.from(vers.keys())).toEqual(Object.keys(loadedVers));
for (const name of vers.keys()) {
const ver = vers.get(name);
expect(validate(ver, name)).toBe(true);
}
});
it('should fallback to semver', () => {
expect(allVersioning.get(undefined)).toBe(
allVersioning.get(semverVersioning.id)
);
expect(allVersioning.get('unknown')).toBe(
allVersioning.get(semverVersioning.id)
);
});
it('should accept config', () => {
expect(allVersioning.get('semver:test')).toBeDefined();
});
describe('should return the same interface', () => {
const optionalFunctions = [
'isLessThanRange',
'valueToVersion',
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'should',
'toLocaleString',
'toString',
'valueOf',
];
const npmApi = Object.keys(allVersioning.get(semverVersioning.id))
.filter((val) => !optionalFunctions.includes(val))
.sort();
function getAllPropertyNames(obj: any): string[] {
const props = [];
let o = obj;
do {
Object.getOwnPropertyNames(o).forEach((prop) => {
if (!props.includes(prop)) {
props.push(prop);
}
});
// eslint-disable-next-line no-cond-assign
} while ((o = Object.getPrototypeOf(o)));
return props;
}
for (const supportedScheme of supportedSchemes) {
it(supportedScheme, () => {
const schemeKeys = getAllPropertyNames(
allVersioning.get(supportedScheme)
)
.filter(
(val) => !optionalFunctions.includes(val) && !val.startsWith('_')
)
.sort();
expect(schemeKeys).toEqual(npmApi);
const apiOrCtor = require('./' + supportedScheme).api;
if (isVersioningApiConstructor(apiOrCtor)) {
return;
}
expect(Object.keys(apiOrCtor).sort()).toEqual(
Object.keys(allVersioning.get(supportedScheme)).sort()
);
});
}
it('dummy', () => {
class DummyScheme extends GenericVersioningApi {
// eslint-disable-next-line class-methods-use-this
protected _compare(_version: string, _other: string): number {
throw new Error('Method not implemented.');
}
// eslint-disable-next-line class-methods-use-this
protected _parse(_version: string): GenericVersion {
throw new Error('Method not implemented.');
}
}
const api = new DummyScheme();
const schemeKeys = getAllPropertyNames(api)
.filter(
(val) => !optionalFunctions.includes(val) && !val.startsWith('_')
)
.sort();
expect(schemeKeys).toEqual(npmApi);
});
});
});