-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.js
134 lines (104 loc) · 3.96 KB
/
test.js
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
import test from 'ava';
import axios from 'axios';
import execa from 'execa';
import semver from 'semver';
import modules from './modules';
import fn from '.';
const moduleNames = Object.keys(modules);
test('basic', t => {
t.deepEqual(fn('react', '15.0.0', {env: 'development'}), {
name: 'react',
var: 'React',
url: 'https://unpkg.com/react@15.0.0/dist/react.js',
version: '15.0.0'
});
});
test('unknown module', t => {
t.is(fn('qwerty', '1.0.0'), null);
});
test('default to development', t => {
t.deepEqual(fn('react', '15.0.0', {env: 'development'}), fn('react', '15.0.0'));
t.notDeepEqual(fn('react', '15.0.0', {env: 'production'}), fn('react', '15.0.0'));
});
test('out of range module', t => {
t.is(fn('react', '0.10.0'), null);
});
for (const moduleName of moduleNames) {
const versionRanges = Object.keys(modules[moduleName].versions);
test.serial(`prod: ${moduleName}@next`, testNextModule, moduleName, 'production');
test.serial(`dev: ${moduleName}@next`, testNextModule, moduleName, 'development');
const allVersions = getAllVersions(moduleName);
const testVersions = [].concat(...versionRanges.map(getRangeEdgeVersions(allVersions)));
testVersions.forEach(version => {
test.serial(`prod: ${moduleName}@${version}`, testModule, moduleName, version, 'production');
test.serial(`dev: ${moduleName}@${version}`, testModule, moduleName, version, 'development');
});
}
async function testModule(t, moduleName, version, env) {
const cdnConfig = fn(moduleName, version, {env});
await testCdnConfig(t, cdnConfig, moduleName, version);
}
async function testNextModule(t, moduleName, env) {
const tags = getModuleInfo(moduleName)['dist-tags'];
if (!tags.next) {
return t.pass();
}
const nextVersion = tags.next;
const futureVersion = removePrereleaseItentifiers(nextVersion);
const cdnConfig = fn(moduleName, futureVersion, {env});
cdnConfig.url = cdnConfig.url.replace(futureVersion, nextVersion);
await testCdnConfig(t, cdnConfig, moduleName, nextVersion);
}
async function testCdnConfig(t, cdnConfig, moduleName, version) {
t.notDeepEqual(cdnConfig, null);
t.is(cdnConfig.name, moduleName);
t.truthy(cdnConfig.url);
t.true(cdnConfig.url.includes(version));
await t.notThrowsAsync(async () => {
const {data} = await axios.get(cdnConfig.url);
if (cdnConfig.var != null) {
t.true(isValidVarName(cdnConfig.var));
const content = data.replace(/ /g, '');
t.true(
content.includes(`.${cdnConfig.var}=`) ||
content.includes(`["${cdnConfig.var}"]=`) ||
content.includes(`['${cdnConfig.var}']=`),
);
}
}, cdnConfig.url);
}
function getModuleInfo(moduleName) {
return JSON.parse(execa.sync('npm', ['info', '--json', `${moduleName}`]).stdout);
}
function getAllVersions(moduleName) {
return getModuleInfo(moduleName).versions;
}
function getRangeEdgeVersions(allVersions) {
return function (range) {
const result = [];
const values = allVersions.filter(version => semver.satisfies(version, range));
if (values.length > 0) {
result.push(values[0]);
}
if (values.length > 1) {
result.push(values[values.length - 1]);
}
return result;
};
}
// https://stackoverflow.com/a/31625466/3052444
function isValidVarName(name) {
try {
if (name.indexOf('.') > -1) {
// E.g. ng.core would cause errors otherwise:
name = name.split('.').join('_');
}
// eslint-disable-next-line no-eval
return name.indexOf('}') === -1 && eval('(function() { a = {' + name + ':1}; a.' + name + '; var ' + name + '; }); true');
} catch (error) {
return false;
}
}
function removePrereleaseItentifiers(version) {
return `${semver.major(version)}.${semver.minor(version)}.${semver.patch(version)}`;
}