Skip to content

Commit 5ea599b

Browse files
committed
feat: adds better test coverage for rollup plugin
1 parent b413945 commit 5ea599b

File tree

5 files changed

+421
-4
lines changed

5 files changed

+421
-4
lines changed

app-config-rollup/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"peerDependencies": {},
4141
"devDependencies": {
4242
"@app-config/test-utils": "*",
43+
"@rollup/plugin-commonjs": "21",
4344
"rollup": "^2"
4445
},
4546
"prettier": "@lcdev/prettier",
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Rollup Plugin processes app-config 1`] = `
4+
"var formats = {};
5+
6+
(function (exports) {
7+
Object.defineProperty(exports, \\"__esModule\\", { value: true });
8+
exports.formatNames = exports.fastFormats = exports.fullFormats = void 0;
9+
function fmtDef(validate, compare) {
10+
return { validate, compare };
11+
}
12+
exports.fullFormats = {
13+
// date: http://tools.ietf.org/html/rfc3339#section-5.6
14+
date: fmtDef(date, compareDate),
15+
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6
16+
time: fmtDef(time, compareTime),
17+
\\"date-time\\": fmtDef(date_time, compareDateTime),
18+
// duration: https://tools.ietf.org/html/rfc3339#appendix-A
19+
duration: /^P(?!$)((\\\\d+Y)?(\\\\d+M)?(\\\\d+D)?(T(?=\\\\d)(\\\\d+H)?(\\\\d+M)?(\\\\d+S)?)?|(\\\\d+W)?)$/,
20+
uri,
21+
\\"uri-reference\\": /^(?:[a-z][a-z0-9+\\\\-.]*:)?(?:\\\\/?\\\\/(?:(?:[a-z0-9\\\\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\\\\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0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fc6570
23+
\\"uri-template\\": /^(?:(?:[^\\\\x00-\\\\x20\\"'<>%\\\\\\\\^\`{|}]|%[0-9a-f]{2})|\\\\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\\\\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\\\\*)?)*\\\\})*$/i,
24+
// For the source: https://gist.github.com/dperini/729294
25+
// For test cases: https://mathiasbynens.be/demo/url-regex
26+
url: /^(?:https?|ftp):\\\\/\\\\/(?:\\\\S+(?::\\\\S*)?@)?(?:(?!(?:10|127)(?:\\\\.\\\\d{1,3}){3})(?!(?:169\\\\.254|192\\\\.168)(?:\\\\.\\\\d{1,3}){2})(?!172\\\\.(?:1[6-9]|2\\\\d|3[0-1])(?:\\\\.\\\\d{1,3}){2})(?:[1-9]\\\\d?|1\\\\d\\\\d|2[01]\\\\d|22[0-3])(?:\\\\.(?:1?\\\\d{1,2}|2[0-4]\\\\d|25[0-5])){2}(?:\\\\.(?:[1-9]\\\\d?|1\\\\d\\\\d|2[0-4]\\\\d|25[0-4]))|(?:(?:[a-z0-9\\\\u{00a1}-\\\\u{ffff}]+-)*[a-z0-9\\\\u{00a1}-\\\\u{ffff}]+)(?:\\\\.(?:[a-z0-9\\\\u{00a1}-\\\\u{ffff}]+-)*[a-z0-9\\\\u{00a1}-\\\\u{ffff}]+)*(?:\\\\.(?:[a-z\\\\u{00a1}-\\\\u{ffff}]{2,})))(?::\\\\d{2,5})?(?:\\\\/[^\\\\s]*)?$/iu,
27+
email: /^[a-z0-9!#$%&'*+/=?^_\`{|}~-]+(?:\\\\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
28+
hostname: /^(?=.{1,253}\\\\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\\\\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\\\\.?$/i,
29+
// optimized https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html
30+
ipv4: /^(?:(?:25[0-5]|2[0-4]\\\\d|[01]?\\\\d\\\\d?)\\\\.){3}(?:25[0-5]|2[0-4]\\\\d|[01]?\\\\d\\\\d?)$/,
31+
ipv6: /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)(\\\\.(25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3}))|:)))$/i,
32+
regex,
33+
// uuid: http://tools.ietf.org/html/rfc4122
34+
uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
35+
// JSON-pointer: https://tools.ietf.org/html/rfc6901
36+
// uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A
37+
\\"json-pointer\\": /^(?:\\\\/(?:[^~/]|~0|~1)*)*$/,
38+
\\"json-pointer-uri-fragment\\": /^#(?:\\\\/(?:[a-z0-9_\\\\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i,
39+
// relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
40+
\\"relative-json-pointer\\": /^(?:0|[1-9][0-9]*)(?:#|(?:\\\\/(?:[^~/]|~0|~1)*)*)$/,
41+
};
42+
exports.fastFormats = {
43+
...exports.fullFormats,
44+
date: fmtDef(/^\\\\d\\\\d\\\\d\\\\d-[0-1]\\\\d-[0-3]\\\\d$/, compareDate),
45+
time: fmtDef(/^(?:[0-2]\\\\d:[0-5]\\\\d:[0-5]\\\\d|23:59:60)(?:\\\\.\\\\d+)?(?:z|[+-]\\\\d\\\\d(?::?\\\\d\\\\d)?)?$/i, compareTime),
46+
\\"date-time\\": fmtDef(/^\\\\d\\\\d\\\\d\\\\d-[0-1]\\\\d-[0-3]\\\\d[t\\\\s](?:[0-2]\\\\d:[0-5]\\\\d:[0-5]\\\\d|23:59:60)(?:\\\\.\\\\d+)?(?:z|[+-]\\\\d\\\\d(?::?\\\\d\\\\d)?)$/i, compareDateTime),
47+
// uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js
48+
uri: /^(?:[a-z][a-z0-9+\\\\-.]*:)(?:\\\\/?\\\\/)?[^\\\\s]*$/i,
49+
\\"uri-reference\\": /^(?:(?:[a-z][a-z0-9+\\\\-.]*:)?\\\\/?\\\\/)?(?:[^\\\\\\\\\\\\s#][^\\\\s#]*)?(?:#[^\\\\\\\\\\\\s]*)?$/i,
50+
// email (sources from jsen validator):
51+
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363
52+
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'wilful violation')
53+
email: /^[a-z0-9.!#$%&'*+/=?^_\`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\\\\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i,
54+
};
55+
exports.formatNames = Object.keys(exports.fullFormats);
56+
function isLeapYear(year) {
57+
// https://tools.ietf.org/html/rfc3339#appendix-C
58+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
59+
}
60+
const DATE = /^(\\\\d\\\\d\\\\d\\\\d)-(\\\\d\\\\d)-(\\\\d\\\\d)$/;
61+
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
62+
function date(str) {
63+
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
64+
const matches = DATE.exec(str);
65+
if (!matches)
66+
return false;
67+
const year = +matches[1];
68+
const month = +matches[2];
69+
const day = +matches[3];
70+
return (month >= 1 &&
71+
month <= 12 &&
72+
day >= 1 &&
73+
day <= (month === 2 && isLeapYear(year) ? 29 : DAYS[month]));
74+
}
75+
function compareDate(d1, d2) {
76+
if (!(d1 && d2))
77+
return undefined;
78+
if (d1 > d2)
79+
return 1;
80+
if (d1 < d2)
81+
return -1;
82+
return 0;
83+
}
84+
const TIME = /^(\\\\d\\\\d):(\\\\d\\\\d):(\\\\d\\\\d)(\\\\.\\\\d+)?(z|[+-]\\\\d\\\\d(?::?\\\\d\\\\d)?)?$/i;
85+
function time(str, withTimeZone) {
86+
const matches = TIME.exec(str);
87+
if (!matches)
88+
return false;
89+
const hour = +matches[1];
90+
const minute = +matches[2];
91+
const second = +matches[3];
92+
const timeZone = matches[5];
93+
return (((hour <= 23 && minute <= 59 && second <= 59) ||
94+
(hour === 23 && minute === 59 && second === 60)) &&
95+
(!withTimeZone || timeZone !== \\"\\"));
96+
}
97+
function compareTime(t1, t2) {
98+
if (!(t1 && t2))
99+
return undefined;
100+
const a1 = TIME.exec(t1);
101+
const a2 = TIME.exec(t2);
102+
if (!(a1 && a2))
103+
return undefined;
104+
t1 = a1[1] + a1[2] + a1[3] + (a1[4] || \\"\\");
105+
t2 = a2[1] + a2[2] + a2[3] + (a2[4] || \\"\\");
106+
if (t1 > t2)
107+
return 1;
108+
if (t1 < t2)
109+
return -1;
110+
return 0;
111+
}
112+
const DATE_TIME_SEPARATOR = /t|\\\\s/i;
113+
function date_time(str) {
114+
// http://tools.ietf.org/html/rfc3339#section-5.6
115+
const dateTime = str.split(DATE_TIME_SEPARATOR);
116+
return dateTime.length === 2 && date(dateTime[0]) && time(dateTime[1], true);
117+
}
118+
function compareDateTime(dt1, dt2) {
119+
if (!(dt1 && dt2))
120+
return undefined;
121+
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR);
122+
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR);
123+
const res = compareDate(d1, d2);
124+
if (res === undefined)
125+
return undefined;
126+
return res || compareTime(t1, t2);
127+
}
128+
const NOT_URI_FRAGMENT = /\\\\/|:/;
129+
const URI = /^(?:[a-z][a-z0-9+\\\\-.]*:)(?:\\\\/?\\\\/(?:(?:[a-z0-9\\\\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\\\\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[0-9a-f]{2})*)?(?:#(?:[a-z0-9\\\\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
130+
function uri(str) {
131+
// http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required \\".\\"
132+
return NOT_URI_FRAGMENT.test(str) && URI.test(str);
133+
}
134+
const Z_ANCHOR = /[^\\\\\\\\]\\\\\\\\Z/;
135+
function regex(str) {
136+
if (Z_ANCHOR.test(str))
137+
return false;
138+
try {
139+
new RegExp(str);
140+
return true;
141+
}
142+
catch (e) {
143+
return false;
144+
}
145+
}
146+
147+
}(formats));
148+
149+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
150+
151+
const configValue = {\\"foo\\":\\"https://example.com\\",\\"bar\\":\\"baz\\"};
152+
153+
// if the global was already defined, use it
154+
const config = (globalNamespace._appConfig || configValue);
155+
156+
// if the global is frozen then it was set by electron and we can't change it, but we'll set it if we can
157+
if (
158+
typeof globalNamespace._appConfig === 'undefined' ||
159+
!Object.isFrozen(globalNamespace._appConfig)
160+
) {
161+
globalNamespace._appConfig = config;
162+
}
163+
var config$1 = config;
164+
const { uri: formats0 } = formats.fullFormats;
165+
166+
function genValidateConfig(){
167+
const validateConfigModule = {};
168+
(function(module){module.exports = validate10;
169+
module.exports.default = validate10;
170+
171+
172+
function validate10(data, valCxt){
173+
if(valCxt){
174+
var dataPath = valCxt.dataPath;
175+
valCxt.parentData;
176+
valCxt.parentDataProperty;
177+
valCxt.rootData;
178+
}
179+
else {
180+
var dataPath = \\"\\";
181+
}
182+
var vErrors = null;
183+
var errors = 0;
184+
if(data && typeof data == \\"object\\" && !Array.isArray(data)){
185+
if(data.foo !== undefined){
186+
var data0 = data.foo;
187+
if(typeof data0 === \\"string\\"){
188+
if(!(formats0(data0))){
189+
var err0 = {keyword:\\"format\\",dataPath:dataPath+\\"/foo\\",schemaPath:\\"#/properties/foo/format\\",params:{format: \\"uri\\"},message:\\"should match format \\\\\\"\\"+\\"uri\\"+\\"\\\\\\"\\"};
190+
if(vErrors === null){
191+
vErrors = [err0];
192+
}
193+
else {
194+
vErrors.push(err0);
195+
}
196+
errors++;
197+
}
198+
}
199+
else {
200+
var err1 = {keyword:\\"type\\",dataPath:dataPath+\\"/foo\\",schemaPath:\\"#/properties/foo/type\\",params:{type: \\"string\\"},message:\\"should be string\\"};
201+
if(vErrors === null){
202+
vErrors = [err1];
203+
}
204+
else {
205+
vErrors.push(err1);
206+
}
207+
errors++;
208+
}
209+
}
210+
}
211+
else {
212+
var err2 = {keyword:\\"type\\",dataPath:dataPath,schemaPath:\\"#/type\\",params:{type: \\"object\\"},message:\\"should be object\\"};
213+
if(vErrors === null){
214+
vErrors = [err2];
215+
}
216+
else {
217+
vErrors.push(err2);
218+
}
219+
errors++;
220+
}
221+
validate10.errors = vErrors;
222+
return errors === 0;
223+
}
224+
})(validateConfigModule);
225+
return validateConfigModule.exports;
226+
}
227+
228+
229+
const validateConfig = /*#__PURE__*/ genValidateConfig();
230+
231+
validateConfig(config$1);
232+
console.log(config$1);
233+
"
234+
`;
235+
236+
exports[`Rollup Plugin processes empty app-config 1`] = `
237+
"const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
238+
239+
const configValue = {};
240+
241+
// if the global was already defined, use it
242+
const config = (globalNamespace._appConfig || configValue);
243+
244+
// if the global is frozen then it was set by electron and we can't change it, but we'll set it if we can
245+
if (
246+
typeof globalNamespace._appConfig === 'undefined' ||
247+
!Object.isFrozen(globalNamespace._appConfig)
248+
) {
249+
globalNamespace._appConfig = config;
250+
}
251+
var config$1 = config;
252+
253+
console.log(config$1);
254+
"
255+
`;

app-config-rollup/src/index.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { rollup } from 'rollup';
2+
import { withTempFiles } from '@app-config/test-utils';
3+
import commonJsRollup from '@rollup/plugin-commonjs';
4+
import appConfigRollup from './index';
5+
6+
describe('Rollup Plugin', () => {
7+
let logSpy: jest.SpyInstance;
8+
beforeEach(() => {
9+
jest.clearAllMocks();
10+
11+
logSpy = jest.spyOn(console, 'log').mockImplementation();
12+
});
13+
14+
it('processes app-config', () =>
15+
withTempFiles(
16+
{
17+
'.app-config.yml': `
18+
foo: https://example.com
19+
bar: baz
20+
`,
21+
'.app-config.schema.yml': `
22+
type: object
23+
properties:
24+
foo:
25+
type: string
26+
format: uri
27+
`,
28+
'entry.js': `
29+
import config, { validateConfig } from '@app-config/main';
30+
31+
validateConfig(config);
32+
console.log(config);
33+
`,
34+
},
35+
async (inDir) => {
36+
const bundle = await rollup({
37+
input: inDir('entry.js'),
38+
plugins: [
39+
appConfigRollup({ loadingOptions: { directory: inDir('.') } }),
40+
// commonjs plugin is required when we use ajv-formats, but not otherwise
41+
commonJsRollup(),
42+
],
43+
});
44+
45+
const { output } = await bundle.generate({});
46+
expect(output).toHaveLength(1);
47+
expect(output[0].code).toMatchSnapshot();
48+
49+
// avoid polluting the global namespace
50+
const globalThis = {}; // eslint-disable-line
51+
// we specially want to `eval` the generated code to make sure it wasn't messed up
52+
eval(output[0].code); // eslint-disable-line no-eval
53+
54+
expect(logSpy).toHaveBeenLastCalledWith({ foo: 'https://example.com', bar: 'baz' });
55+
},
56+
));
57+
58+
it('processes empty app-config', () =>
59+
withTempFiles(
60+
{
61+
'.app-config.yml': ``,
62+
'.app-config.schema.yml': ``,
63+
'entry.js': `
64+
import config from '@app-config/main';
65+
66+
console.log(config);
67+
`,
68+
},
69+
async (inDir) => {
70+
const bundle = await rollup({
71+
input: inDir('entry.js'),
72+
plugins: [appConfigRollup({ loadingOptions: { directory: inDir('.') } })],
73+
});
74+
75+
const { output } = await bundle.generate({});
76+
expect(output).toHaveLength(1);
77+
expect(output[0].code).toMatchSnapshot();
78+
79+
// avoid polluting the global namespace
80+
const globalThis = {}; // eslint-disable-line
81+
// we specially want to `eval` the generated code to make sure it wasn't messed up
82+
eval(output[0].code); // eslint-disable-line no-eval
83+
84+
expect(logSpy).toHaveBeenLastCalledWith({});
85+
},
86+
));
87+
88+
it('fails without app-config', () =>
89+
withTempFiles(
90+
{
91+
'entry.js': `
92+
import config from '@app-config/main';
93+
console.log(config);
94+
`,
95+
},
96+
async (inDir) => {
97+
await expect(
98+
rollup({
99+
input: inDir('entry.js'),
100+
plugins: [appConfigRollup({ loadingOptions: { directory: inDir('.') } })],
101+
}),
102+
).rejects.toThrowError('Could not load @app-config/main');
103+
},
104+
));
105+
});

app-config-rollup/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ function shouldTransform(id: string) {
4949

5050
async function loadConfig(
5151
{
52-
useGlobalNamespace,
5352
loadingOptions,
5453
schemaLoadingOptions,
55-
injectValidationFunction,
54+
injectValidationFunction = true,
55+
useGlobalNamespace,
5656
readGlobal,
5757
}: Options,
5858
currentFilePaths: string[],

0 commit comments

Comments
 (0)