-
Notifications
You must be signed in to change notification settings - Fork 137
/
stage1-test.ts
472 lines (414 loc) · 17.4 KB
/
stage1-test.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import resolve from 'resolve';
import { join } from 'path';
import merge from 'lodash/merge';
import fs from 'fs-extra';
import { loadFromFixtureData } from './helpers';
import { dummyAppScenarios, baseAddon, appScenarios } from './scenarios';
import { PreparedApp } from 'scenario-tester';
import QUnit from 'qunit';
const { module: Qmodule, test } = QUnit;
appScenarios
.only('release')
.map('stage-1-max-compat', project => {
let addon = baseAddon();
merge(addon.files, loadFromFixtureData('hello-world-addon'));
addon.pkg.name = 'my-addon';
addon.linkDependency('@embroider/sample-transforms', { baseDir: __dirname });
addon.linkDependency('@embroider/macros', { baseDir: __dirname });
project.addDependency(addon);
// our app will include an in-repo addon
project.pkg['ember-addon'] = { paths: ['lib/in-repo-addon'] };
merge(project.files, loadFromFixtureData('basic-in-repo-addon'));
})
.forEachScenario(async scenario => {
Qmodule(`${scenario.name}`, function (hooks) {
let app: PreparedApp;
let workspaceDir: string;
hooks.before(async () => {
process.env.THROW_UNLESS_PARALLELIZABLE = '1'; // see https://github.com/embroider-build/embroider/pull/924
app = await scenario.prepare();
await app.execute('cross-env STAGE1_ONLY=true node ./node_modules/ember-cli/bin/ember b');
workspaceDir = fs.readFileSync(join(app.dir, 'dist', '.stage1-output'), 'utf8');
});
hooks.after(async () => {
delete process.env.THROW_UNLESS_PARALLELIZABLE;
});
test('component in app tree', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'node_modules/my-addon/_app_/components/hello-world.js')));
});
test('addon metadata', function (assert) {
let assertMeta = fs.readJsonSync(join(workspaceDir, 'node_modules/my-addon/package.json'))['ember-addon'];
assert.deepEqual(assertMeta['app-js'], { './components/hello-world.js': './_app_/components/hello-world.js' });
assert.ok(
JSON.stringify(assertMeta['implicit-modules']).includes('./components/hello-world'),
'staticAddonTrees is off so we should include the component implicitly'
);
assert.ok(
JSON.stringify(assertMeta['implicit-modules']).includes('./templates/components/hello-world.hbs'),
'staticAddonTrees is off so we should include the template implicitly'
);
assert.equal(assertMeta.version, 2);
});
test('component in addon tree', function (assert) {
let fileContents = fs.readFileSync(join(workspaceDir, 'node_modules/my-addon/components/hello-world.js'));
assert.ok(fileContents.includes('getOwnConfig()'), 'JS macros have not run yet');
assert.ok(fileContents.includes('embroider-sample-transforms-result'), 'custom babel plugins have run');
});
test('component template in addon tree', function (assert) {
let fileContents = fs.readFileSync(
join(workspaceDir, 'node_modules/my-addon/templates/components/hello-world.hbs')
);
assert.ok(
fileContents.includes('<div class={{embroider-sample-transforms-result}}>hello world</div>'),
'template is still hbs and custom transforms have run'
);
assert.ok(
fileContents.includes('<span>{{macroDependencySatisfies "ember-source" ">3"}}</span>'),
'template macros have not run'
);
});
test('test module name added', function (assert) {
let fileContents = fs.readFileSync(
join(workspaceDir, 'node_modules/my-addon/templates/components/module-name.hbs')
);
let searchRegExp = /\\/gi;
let replaceWith = '\\\\';
assert.ok(
fileContents.includes(
`<div class={{embroider-sample-transforms-module "${join(
'my-addon',
'templates',
'components',
'module-name.hbs'
).replace(searchRegExp, replaceWith)}"}}>hello world</div>`
),
'template is still hbs and module name transforms have run'
);
});
test('component with inline template', function (assert) {
let fileContents = fs.readFileSync(
join(workspaceDir, 'node_modules/my-addon/components/has-inline-template.js')
);
assert.ok(
fileContents.includes('hbs`<div class={{embroider-sample-transforms-result}}>Inline</div>'),
'tagged template is still hbs and custom transforms have run'
);
assert.ok(
/hbs\(["']<div class={{embroider-sample-transforms-result}}>Extra<\/div>["']\)/.test(fileContents.toString()),
'called template is still hbs and custom transforms have run'
);
assert.ok(
/<span>{{macroDependencySatisfies ['"]ember-source['"] ['"]>3['"]}}<\/span>/.test(fileContents.toString()),
'template macros have not run'
);
});
test('in-repo-addon is available', function (assert) {
assert.ok(resolve.sync('in-repo-addon/helpers/helper-from-in-repo-addon', { basedir: workspaceDir }));
});
test('dynamic import is preserved', function (assert) {
let fileContents = fs.readFileSync(
join(workspaceDir, 'node_modules/my-addon/components/does-dynamic-import.js')
);
assert.ok(/return import\(['"]some-library['"]\)/.test(fileContents.toString()));
});
});
});
appScenarios
.only('release')
.map('stage-1-inline-hbs', project => {
let addon = baseAddon();
merge(addon.files, {
addon: {
components: {
'has-inline-template.js': `
import Component from '@ember/component';
import hbs from 'htmlbars-inline-precompile';
export default Component.extend({
// tagged template form:
layout: ${"hbs`<div class={{embroider-sample-transforms-target}}>Inline</div><span>{{macroDependencySatisfies 'ember-source' '>3'}}</span>`"},
// call expression form:
extra: hbs("<div class={{embroider-sample-transforms-target}}>Extra</div>")
});
`,
},
},
});
addon.pkg.name = 'my-addon';
addon.linkDependency('@embroider/sample-transforms', { baseDir: __dirname });
addon.linkDependency('@embroider/macros', { baseDir: __dirname });
addon.linkDependency('ember-cli-htmlbars-inline-precompile', { baseDir: __dirname });
addon.linkDependency('ember-cli-htmlbars-3', { baseDir: __dirname, resolveName: 'ember-cli-htmlbars' });
project.addDependency(addon);
})
.forEachScenario(async scenario => {
Qmodule(`${scenario.name}`, function (hooks) {
let app: PreparedApp;
let workspaceDir: string;
hooks.before(async () => {
app = await scenario.prepare();
await app.execute('cross-env STAGE1_ONLY=true node ./node_modules/ember-cli/bin/ember b');
workspaceDir = fs.readFileSync(join(app.dir, 'dist', '.stage1-output'), 'utf8');
});
test('component with inline template', function (assert) {
let fileContents = fs.readFileSync(
join(workspaceDir, 'node_modules/my-addon/components/has-inline-template.js')
);
assert.ok(
fileContents.includes('hbs`<div class={{embroider-sample-transforms-result}}>Inline</div>'),
'tagged template is still hbs and custom transforms have run'
);
assert.ok(
/hbs\(["']<div class={{embroider-sample-transforms-result}}>Extra<\/div>["']\)/.test(fileContents.toString()),
'called template is still hbs and custom transforms have run'
);
assert.ok(
/<span>{{macroDependencySatisfies ['"]ember-source['"] ['"]>3['"]}}<\/span>/.test(fileContents.toString()),
'template macros have not run'
);
});
});
});
appScenarios
.only('release')
.map('stage-1-problematic-addon-zoo', project => {
let addon = baseAddon();
// an addon that emits a package.json file from its treeForAddon
merge(addon.files, {
index: `module.exports = {
name: require('./package').name,
treeForAddon() {
return require('path').join(__dirname, 'alpha-addon-tree');
}
};`,
'alpha-addon-tree': {
'package.json': '{}',
},
});
addon.pkg.name = 'alpha';
let hasCustomBase = baseAddon();
// an addon that manually extends the Addon base class
merge(hasCustomBase.files, {
'index.js': `
const { join } = require('path');
const Addon = require('ember-cli/lib/models/addon');
module.exports = Addon.extend({
name: 'has-custom-base',
treeForAddon() {
return join(__dirname, 'weird-addon-path');
}
});`,
'weird-addon-path': {
'has-custom-base': {
'file.js': '// weird-addon-path/file.js',
},
},
});
hasCustomBase.pkg.name = 'has-custom-base';
let undefinedFastboot = baseAddon();
// an addon that nullifies its custom fastboot tree with a custom fastboot hook
merge(undefinedFastboot.files, {
'index.js': `module.exports = {
name: 'undefined-fastboot',
treeForFastBoot() {}
}`,
fastboot: {
'something.js': '',
},
});
undefinedFastboot.pkg.name = 'undefined-fastboot';
// Use one addon to patch the hook on another (yes, this happens in the
// wild...), and ensure we still detect the customized hook
let externallyCustomized = baseAddon();
let troubleMaker = baseAddon();
merge(troubleMaker.files, {
injected: {
hello: { 'world.js': '// hello' },
},
'index.js': `
const { join } = require('path');
module.exports = {
name: 'trouble-maker',
included() {
let instance = this.project.addons.find(a => a.name === "externally-customized");
let root = this.root;
instance.treeForPublic = function() {
return join(root, 'injected');
}
}
}`,
});
externallyCustomized.pkg.name = 'externally-customized';
troubleMaker.pkg.name = 'trouble-maker';
// an addon that customizes a tree by mutating treeForMethods
let patchesMethodName = baseAddon();
merge(patchesMethodName.files, {
injected: {
hello: { 'world.js': '// hello' },
},
'index.js': `
const { join } = require('path');
module.exports = {
name: 'patches-method-name',
included() {
this.treeForMethods['addon'] = 'notTheUsual';
},
notTheUsual() {
return join(this.root, 'injected');
}
}`,
});
patchesMethodName.pkg.name = 'patches-method-name';
// an addon that customizes packageJSON['ember-addon'].main and then uses
// stock trees. Setting the main actually changes the root for *all* stock
// trees.
let movedMain = baseAddon();
merge(movedMain.files, {
custom: {
'index.js': `module.exports = { name: 'moved-main'};`,
addon: { helpers: { 'hello.js': '// hello-world' } },
},
});
merge(movedMain.pkg, { 'ember-addon': { main: 'custom/index.js' }, name: 'moved-main' });
// an addon that uses treeFor() to sometimes suppress its stock trees
let suppressed = baseAddon();
merge(suppressed.files, {
'index.js': `module.exports = {
name: require('./package').name,
treeFor(name) {
if (name !== 'app') {
return this._super.treeFor(name);
} else {
return undefined;
}
}
}`,
addon: {
'addon-example.js': '// example',
},
app: {
'app-example.js': '// example',
},
});
suppressed.pkg.name = 'suppressed';
// an addon that uses treeFor() to sometimes suppress its custom trees
let suppressedCustom = baseAddon();
merge(suppressedCustom.files, {
'index.js': `module.exports = {
name: require('./package').name,
treeFor(name) {
if (name !== 'app') {
return this._super(name);
} else {
return undefined;
}
},
treeForApp() {
return require('path').join(__dirname, 'app-custom');
},
treeForAddon() {
return require('path').join(__dirname, 'addon-custom');
},
}`,
'addon-custom': {
'suppressed-custom': {
'addon-example.js': '// example',
},
},
'app-custom': {
'app-example.js': '// example',
},
});
suppressedCustom.pkg.name = 'suppressed-custom';
project.addDependency(addon);
project.addDependency(hasCustomBase);
project.addDependency(undefinedFastboot);
project.addDependency(externallyCustomized);
project.addDependency(troubleMaker);
project.addDependency(movedMain);
project.addDependency(suppressed);
project.addDependency(suppressedCustom);
project.addDependency(patchesMethodName);
project.pkg['ember-addon'] = { paths: ['lib/disabled-in-repo-addon', 'lib/blacklisted-in-repo-addon'] };
merge(project.files, loadFromFixtureData('blacklisted-addon-build-options'));
})
.forEachScenario(async scenario => {
Qmodule(`${scenario.name}`, function (hooks) {
let app: PreparedApp;
let workspaceDir: string;
hooks.before(async () => {
app = await scenario.prepare();
await app.execute('cross-env STAGE1_ONLY=true node ./node_modules/ember-cli/bin/ember b');
workspaceDir = fs.readFileSync(join(app.dir, 'dist', '.stage1-output'), 'utf8');
});
test('real package.json wins', function (assert) {
let fileContents = fs.readFileSync(join(workspaceDir, 'node_modules/alpha/package.json'));
assert.ok(fileContents.includes('alpha'));
});
test('custom tree hooks are detected in addons that manually extend from Addon', function (assert) {
let fileContents = fs.readFileSync(join(workspaceDir, 'node_modules/has-custom-base/file.js'));
assert.ok(/weird-addon-path\/file\.js/.test(fileContents.toString()));
});
test('no fastboot-js is emitted', function (assert) {
let fileContents = fs.readJsonSync(join(workspaceDir, 'node_modules/undefined-fastboot/package.json'));
assert.equal(fileContents['ember-addon']['fastboot-js'], null);
});
test('custom tree hooks are detected when they have been patched into the addon instance', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'node_modules/externally-customized/public/hello/world.js')));
});
test('custom tree hooks are detected when they have been customized via treeForMethod names', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'node_modules/patches-method-name/hello/world.js')));
});
test('addon with customized ember-addon.main can still use stock trees', function (assert) {
let fileContents = fs.readFileSync(join(workspaceDir, 'node_modules/moved-main/helpers/hello.js'));
assert.ok(/hello-world/.test(fileContents.toString()));
});
test('addon with customized treeFor can suppress a stock tree', function (assert) {
assert.notOk(fs.existsSync(join(workspaceDir, 'node_modules/suppressed/_app_/app-example.js')));
});
test('addon with customized treeFor can pass through a stock tree', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'node_modules/suppressed/addon-example.js')));
});
test('addon with customized treeFor can suppress a customized tree', function (assert) {
assert.notOk(fs.existsSync(join(workspaceDir, 'node_modules/suppressed-custom/_app_/app-example.js')));
});
test('addon with customized treeFor can pass through a customized tree', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'node_modules/suppressed-custom/addon-example.js')));
});
test('blacklisted in-repo addon is present but empty', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'lib/blacklisted-in-repo-addon/package.json')));
assert.notOk(fs.existsSync(join(workspaceDir, 'lib/blacklisted-in-repo-addon/example.js')));
});
test('disabled in-repo addon is present but empty', function (assert) {
assert.ok(fs.existsSync(join(workspaceDir, 'lib/disabled-in-repo-addon/package.json')));
assert.notOk(fs.existsSync(join(workspaceDir, 'lib/disabled-in-repo-addon/example.js')));
});
});
});
dummyAppScenarios
.map('stage-1-dummy-addon', project => {
project.pkg.name = 'my-addon';
project.linkDependency('@embroider/webpack', { baseDir: __dirname });
project.linkDependency('@embroider/core', { baseDir: __dirname });
project.linkDependency('@embroider/compat', { baseDir: __dirname });
merge(project.files, {
addon: {
components: {
'hello-world.js': '',
},
},
});
})
.forEachScenario(async scenario => {
Qmodule(`${scenario.name}`, function (hooks) {
let app: PreparedApp;
let workspaceDir: string;
hooks.before(async () => {
app = await scenario.prepare();
await app.execute('cross-env STAGE1_ONLY=true node ./node_modules/ember-cli/bin/ember b');
workspaceDir = fs.readFileSync(join(app.dir, 'dist', '.stage1-output'), 'utf8');
});
test('dummy app can resolve own addon', function (assert) {
assert.ok(resolve.sync('my-addon/components/hello-world.js', { basedir: workspaceDir }));
});
});
});