Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MU addon component invocation #8431

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/broccoli/default-packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,25 +826,25 @@ module.exports = class DefaultPackager {
});

if (this.isModuleUnificationEnabled) {
let destDir, testSrcTree, srcDir;
let destDir, testSrcTree;

// ember-addon
if (this.name === 'dummy') {
testSrcTree = 'src';
// Search the addon folder to find inline tests (src/components/hola/component-test.js)
testSrcTree = this.project.root;
destDir = `${this.project.name()}/src`;
} 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, {
srcDir: 'src',
// allowEmpty in order a MU app can run inside a Classic addon
allowEmpty: true,
include: ['**/*-test.{js,ts}'],
destDir,
annotation: 'Module Unification Tests',
});

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

/**
Expand Down Expand Up @@ -831,7 +831,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 @@ -872,7 +872,7 @@ let addonProto = {
@return
*/
treeForSrc(rawSrcTree) {
if (!isExperimentEnabled('MODULE_UNIFICATION')) {
if (!this.isModuleUnification()) {
return null;
}
if (!rawSrcTree) {
Expand Down Expand Up @@ -1447,6 +1447,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
100 changes: 99 additions & 1 deletion 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 @@ -147,6 +148,59 @@ 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*() {
Expand All @@ -167,10 +221,54 @@ describe('Acceptance: addon-smoke-test', function() {
let appFileContents = fs.readFileSync(path.join(addonRoot, 'dist', 'assets', 'tests.js'), {
encoding: 'utf8',
});

expect(appFileContents).to.include('Unit | Utility | string');

result = yield runCommand('node_modules/ember-cli/bin/ember', 'test');
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}}