Skip to content

Commit

Permalink
fix addon MU detection
Browse files Browse the repository at this point in the history
add tests for addon merging in different scenarios
  • Loading branch information
patricklx committed Feb 21, 2019
1 parent 114f881 commit 97c5b70
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 16 deletions.
37 changes: 25 additions & 12 deletions lib/broccoli/default-packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const p = require('ember-cli-preprocess-registry/preprocessors');
const path = require('path');
const fs = require('fs');
const concat = require('broccoli-concat');
const Funnel = require('broccoli-funnel');
const BroccoliDebug = require('broccoli-debug');
Expand Down Expand Up @@ -844,34 +845,46 @@ module.exports = class DefaultPackager {
annotation: 'Tests To Process',
});


if (this.isModuleUnificationEnabled) {

let destDir,
testSrcTree,
srcDir;
srcDir,
dirNotFound;

// ember-addon
if (this.name === 'dummy') {
testSrcTree = 'src';
destDir = `${this.project.name()}/src`;
// Classic Addon with MU dummy app `kitchen-sink-with-mu-dummy-app` will throw: `Dir not found: src` exception
// `dirNotFound` will skip this error. Find better way to avoid this error.

// also, in tests we pass root path to this function
// in default-packager/tests-test.js
const srcPath = typeof tree === 'string' ? `${tree}/src` : 'src';
dirNotFound = !fs.existsSync(srcPath);
} else {
testSrcTree = tree;
destDir = `${this.name}/src`;
srcDir = 'src';
}
testSrcTree = new Funnel(testSrcTree, {
srcDir,
include: ['**/*-test.{js,ts}'],
annotation: 'Module Unification Tests',
});

testSrcTree = new Funnel(testSrcTree, {
destDir,
});
if (!dirNotFound) {
testSrcTree = new Funnel(testSrcTree, {
srcDir,
include: ['**/*-test.{js,ts}'],
annotation: 'Module Unification Tests',
});

treeToCompile = mergeTrees([treeToCompile, testSrcTree], {
annotation: 'Merge MU Tests',
});
testSrcTree = new Funnel(testSrcTree, {
destDir,
});

treeToCompile = mergeTrees([treeToCompile, testSrcTree], {
annotation: 'Merge MU Tests',
});
}
}

treeToCompile = callAddonsPreprocessTreeHook(this.project, 'test', treeToCompile);
Expand Down
18 changes: 15 additions & 3 deletions lib/models/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ let addonProto = {
@return {Boolean} Whether this is using a module unification format.
*/
isModuleUnification() {
return false;
return this.has('src');
},

/**
Expand Down Expand Up @@ -814,7 +814,7 @@ let addonProto = {
@return {Tree} App file tree
*/
treeForApp(tree) {
if (!isExperimentEnabled('MODULE_UNIFICATION')) {
if (!this.project.isModuleUnification() && !this.isModuleUnification()) {
return tree;
} else if (this.project.isModuleUnification() && this.isModuleUnification()) {
return null;
Expand Down Expand Up @@ -855,7 +855,7 @@ let addonProto = {
@return
*/
treeForSrc(rawSrcTree) {
if (!isExperimentEnabled('MODULE_UNIFICATION')) {
if (!this.isModuleUnification()) {
return null;
}
if (!rawSrcTree) { return null; }
Expand Down Expand Up @@ -1424,6 +1424,18 @@ let addonProto = {
}
},

/**
Returns whether or not the given file name is present in this project.
@private
@method has
@param {String} file File name
@return {Boolean} Whether or not the file is present
*/
has(file) {
return fs.existsSync(path.join(this.root, file)) || fs.existsSync(path.join(this.root, `${file}.js`));
},

/**
Augments the applications configuration settings.
Expand Down
92 changes: 92 additions & 0 deletions tests/acceptance/addon-smoke-test-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const fs = require('fs-extra');
const spawn = require('child_process').spawn;
const chalk = require('chalk');
const rimraf = require("rimraf");

const { isExperimentEnabled } = require('../../lib/experiments');
const runCommand = require('../helpers/run-command');
Expand Down Expand Up @@ -145,6 +146,56 @@ describe('Acceptance: addon-smoke-test', function() {

if (isExperimentEnabled('MODULE_UNIFICATION')) {

if (!process.env.EMBER_CLI_ENABLE_ALL_EXPERIMENTS) {
/*
We have to skip the test if `EMBER_CLI_ENABLE_ALL_EXPERIMENTS` is enabled,
because in this case, `isExperimentEnabled('MODULE_UNIFICATION')` returns true
and `env.EMBER_CLI_MODULE_UNIFICATION` is disabled, but `env.EMBER_CLI_MODULE_UNIFICATION` must be enabled,
in order for `ember-resolver` to work correctly.
*/
it('works with classic addon and MU dummy app', co.wrap(function *() {
let fixtureFile = 'kitchen-sink-classic-mu-dummy-app';
yield copyFixtureFiles(`addon/${fixtureFile}`);

// prevent MU detection
if (yield fs.exists('src')) {
rimraf.sync('src');
}

let packageJsonPath = path.join(addonRoot, 'package.json');
let packageJson = fs.readJsonSync(packageJsonPath);

expect(packageJson.devDependencies['ember-source']).to.not.be.empty;

packageJson.dependencies = packageJson.dependencies || {};
// add HTMLBars for templates (generators do this automatically when components/templates are added)
packageJson.dependencies['ember-cli-htmlbars'] = 'latest';

fs.writeJsonSync(packageJsonPath, packageJson);

let result = yield runCommand('node_modules/ember-cli/bin/ember', 'build');

expect(result.code).to.eql(0);
let contents;

let indexPath = path.join(addonRoot, 'dist', 'index.html');
contents = fs.readFileSync(indexPath, { encoding: 'utf8' });
expect(contents).to.contain('"SOME AWESOME STUFF"');

let cssPath = path.join(addonRoot, 'dist', 'assets', 'vendor.css');
contents = fs.readFileSync(cssPath, { encoding: 'utf8' });
expect(contents).to.contain('addon/styles/app.css is present');
//
let robotsPath = path.join(addonRoot, 'dist', 'robots.txt');
contents = fs.readFileSync(robotsPath, { encoding: 'utf8' });
expect(contents).to.contain('tests/dummy/public/robots.txt is present');

result = yield runCommand('node_modules/ember-cli/bin/ember', 'test');

expect(result.code).to.eql(0);
}));
}

it('can run a MU unit test with a relative import', co.wrap(function *() {
yield copyFixtureFiles('brocfile-tests/mu-unit-test-with-relative-import');

Expand All @@ -170,7 +221,48 @@ describe('Acceptance: addon-smoke-test', function() {
expect(result.code).to.eql(0);

}));
} else {
it('works with MU addon and classic dummy app', co.wrap(function *() {
let fixtureFile = 'kitchen-sink-mu-classic-dummy-app';
yield copyFixtureFiles(`addon/${fixtureFile}`);

// remove MU specific things from base addon
if (yield fs.exists('tests/dummy/src')) {
rimraf.sync('tests/dummy/src');
}

let packageJsonPath = path.join(addonRoot, 'package.json');
let packageJson = fs.readJsonSync(packageJsonPath);

expect(packageJson.devDependencies['ember-source']).to.not.be.empty;

packageJson.dependencies = packageJson.dependencies || {};
// add HTMLBars for templates (generators do this automatically when components/templates are added)
packageJson.dependencies['ember-cli-htmlbars'] = 'latest';

fs.writeJsonSync(packageJsonPath, packageJson);

let result = yield runCommand('node_modules/ember-cli/bin/ember', 'build');

expect(result.code).to.eql(0);
let contents;

let indexPath = path.join(addonRoot, 'dist', 'index.html');
contents = fs.readFileSync(indexPath, { encoding: 'utf8' });
expect(contents).to.contain('"SOME AWESOME STUFF"');

let cssPath = path.join(addonRoot, 'dist', 'assets', 'vendor.css');
contents = fs.readFileSync(cssPath, { encoding: 'utf8' });
expect(contents).to.contain('addon/styles/app.css is present');
//
let robotsPath = path.join(addonRoot, 'dist', 'robots.txt');
contents = fs.readFileSync(robotsPath, { encoding: 'utf8' });
expect(contents).to.contain('tests/dummy/public/robots.txt is present');

result = yield runCommand('node_modules/ember-cli/bin/ember', 'test');

expect(result.code).to.eql(0);
}));
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function truthyHelper() {
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import template from '../templates/components/basic-thing';
import Component from '@ember/component';

export default Component.extend({
layout: template
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* addon/styles/app.css is present */
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="basic-thing">
{{yield}}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import BasicThing from 'some-cool-addon/components/basic-thing';
export default BasicThing;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
name: require('./package').name,

contentFor(type, config) {
if (type === 'head') {
return '"SOME AWESOME STUFF"';
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { setupApplicationTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import truthyHelper from 'some-cool-addon/test-support/helper';
import { module, test } from 'qunit';

module('Acceptance', function(hooks) {
setupApplicationTest(hooks);

test('renders properly', async function(assert) {
await visit('/');

var element = this.element.querySelector('.basic-thing');
assert.equal(element.textContent.trim(), 'WOOT!!');
assert.ok(truthyHelper(), 'addon-test-support helper');
});

test('renders imported component', async function(assert) {
await visit('/');

var element = this.element.querySelector('.second-thing');
assert.equal(element.textContent.trim(), 'SECOND!!');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tests/dummy/public/robots.txt is present
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import BasicThing from 'some-cool-addon/components/basic-thing';

export default BasicThing.extend({
classNames: ['second-thing']
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{#basic-thing}}WOOT!!{{/basic-thing}}
{{#second-thing}}SECOND!!{{/second-thing}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function truthyHelper() {
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
name: require('./package').name,

contentFor(type, config) {
if (type === 'head') {
return '"SOME AWESOME STUFF"';
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import template from './template';
import Component from '@ember/component';

export default Component.extend({
layout: template
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="basic-thing">
{{yield}}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* addon/styles/app.css is present */
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { setupApplicationTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import truthyHelper from 'some-cool-addon/test-support/helper';
import { module, test } from 'qunit';

module('Acceptance', function(hooks) {
setupApplicationTest(hooks);

test('renders properly', async function(assert) {
await visit('/');

var element = this.element.querySelector('.basic-thing');
assert.equal(element.textContent.trim(), 'WOOT!!');
assert.ok(truthyHelper(), 'addon-test-support helper');
});

test('renders imported component', async function(assert) {
await visit('/');

var element = this.element.querySelector('.second-thing');
assert.equal(element.textContent.trim(), 'SECOND!!');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import BasicThing from 'some-cool-addon/src/ui/components/basic-thing/component';

export default BasicThing.extend({
classNames: ['second-thing']
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{#basic-thing}}WOOT!!{{/basic-thing}}
{{#second-thing}}SECOND!!{{/second-thing}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# tests/dummy/public/robots.txt is present
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{#basic-thing}}WOOT!!{{/basic-thing}}
<SomeCoolAddon::BasicThing>WOOT!!</SomeCoolAddon::BasicThing>
{{#second-thing}}SECOND!!{{/second-thing}}

0 comments on commit 97c5b70

Please sign in to comment.