Skip to content

Commit

Permalink
Refactor edition detection for emberjs/rfcs#558. (#4)
Browse files Browse the repository at this point in the history
Refactor edition detection for emberjs/rfcs#558.
  • Loading branch information
rwjblue authored Dec 11, 2019
2 parents 7a78306 + 8257925 commit e67e639
Show file tree
Hide file tree
Showing 4 changed files with 1,283 additions and 55 deletions.
47 changes: 38 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const fs = require('fs');
const path = require('path');

/**
Sets the Edition that the application should be considered
a part of.
Sets the Edition that the application should be considered a part of. This
method is deprecated, and will be phased out in the next major release.
@public
@deprecated
@param {string} editionName the Edition name that your application is authored in
*/
function setEdition(editionName) {
Expand All @@ -12,9 +16,11 @@ function setEdition(editionName) {
/**
Resets the local state _as if_ no edition were specified. In general, this
will be used by various addons' own local blueprint tests when testing
generators.
generators. This method is deprecated, and will be phased out in the next
major release.
@public
@deprecated
*/
function clearEdition() {
delete process.env.EMBER_EDITION;
Expand All @@ -26,8 +32,32 @@ function clearEdition() {
@private
@returns {boolean}
*/
function _getEdition() {
let edition = process.env.EMBER_EDITION;
function _getEdition(projectRoot = process.cwd()) {
let pkgPath = path.join(projectRoot, 'package.json');
let hasPackageJson = false;

try {
require.resolve(pkgPath);
hasPackageJson = true;
} catch (e) {
// ignore errors, this signifies that there is no package.json in the
// projectRoot so we _only_ check for the legacy environment variables
}

let edition;

if (hasPackageJson) {
let pkgContents = fs.readFileSync(pkgPath, { encoding: 'utf8' });
let pkg = JSON.parse(pkgContents);

if ('ember' in pkg && 'edition' in pkg.ember) {
edition = pkg.ember.edition;
}
}

if (edition === undefined) {
edition = process.env.EMBER_EDITION;
}

if (edition === undefined) {
// check fallback "old" location
Expand All @@ -49,13 +79,12 @@ function _getEdition() {
will return `true`, and if the edition in use by the application is _older_
than the requested edition it will return `false`.
@param {string} requestedEditionName the Edition name that the application/addon is requesting
@param {string} [projectRoot=process.cwd()] the base directory of the project
*/
function has(_requestedEditionName) {
function has(_requestedEditionName, projectRoot) {
let requestedEditionName = _requestedEditionName.toLowerCase();
let edition = _getEdition();
let edition = _getEdition(projectRoot);

if (requestedEditionName === 'classic') {
// everything is classic :)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "qunit test.js"
},
"devDependencies": {
"broccoli-test-helper": "^2.0.0",
"qunit": "^2.9.2",
"release-it": "^12.2.1",
"release-it-lerna-changelog": "^1.0.3"
Expand Down
116 changes: 85 additions & 31 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,126 @@
const { createTempDir } = require('broccoli-test-helper');
const { setEdition, has, _getEdition } = require('./index');

const { test } = QUnit;

const OriginalConsole = Object.assign({}, console);
const ROOT = process.cwd();

QUnit.module('@ember/edition-utils', function(hooks) {
// only a test helper **because** we don't want folks to
// use this in actual shared packages, they should instead
// use `has`
function getEdition() {
return process.env.EMBER_EDITION;
}

hooks.afterEach(function() {
let project;

hooks.beforeEach(async function() {
project = await createTempDir();
process.chdir(project.path());
});

hooks.afterEach(async function() {
delete process.env.EMBER_EDITION;
delete process.env.EMBER_VERSION;
Object.assign(console, OriginalConsole);

process.chdir(ROOT);
await project.dispose();
});

QUnit.module('setEdition', function() {
test('the edition name that is passed, sets the edition', function(assert) {
assert.notOk(has('octane'), 'precond');

setEdition('octane');

assert.strictEqual(getEdition(), 'octane');
assert.ok(has('octane'));
});

test('normalizes the edition name that is passed in (lowercasing)', function(assert) {
assert.notOk(has('octane'), 'precond');

setEdition('OCTANE');

assert.strictEqual(getEdition(), 'octane');
assert.ok(has('octane'));
});
});

QUnit.module('has', function() {
test('should be considered "classic" without an edition set', function(assert) {
assert.ok(has('classic'));
});
function setupProject(edition) {
let pkg = {
name: 'dummy',
version: '0.0.0',
};

if (edition) {
pkg.ember = { edition };
}

test('should be considered "octane" when passing octane', function(assert) {
setEdition('octane');
project.write({
'package.json': JSON.stringify(pkg, null, 2),
});
}

test('should be octane if project package.json is setup with edition: octane', function(assert) {
setupProject('octane');

assert.ok(has('octane'));
});

test('should match case insensitively', function(assert) {
setEdition('octane');
test('should be octane if project package.json in custom root is setup with edition: octane', function(assert) {
process.chdir(ROOT);

assert.ok(has('OCTANE'));
});
setupProject('octane');

assert.ok(has('octane', project.path()));
});

test('should not "have" octane, when edition is classic', function(assert) {
setEdition('classic');
test('has("classic") should be true when octane is set', function(assert) {
setupProject('octane');

assert.notOk(has('octane'));
assert.ok(has('classic'));
});

test('should infer edition from process.env.EMBER_VERSION with a warning', function(assert) {
assert.expect(2);
QUnit.module('deprecated setEdition fallback', function() {
test('project package.json "wins" over setEdition', function(assert) {
setupProject('classic');

process.env.EMBER_VERSION = 'octane';
console.log = (...args) => {
assert.deepEqual(args, [
'Please update to using @ember/edition-utils. Using process.env.EMBER_VERSION to declare your application / addon as "octane" ready is deprecated.',
]);
}
setEdition('octane');

assert.ok(has('classic'));
});

test('should be considered "classic" without an edition set', function(assert) {
assert.ok(has('classic'));
});

test('should be considered "octane" when passing octane', function(assert) {
setEdition('octane');

assert.ok(has('octane'));
});

test('should match case insensitively', function(assert) {
setEdition('octane');

assert.ok(has('OCTANE'));
});


test('should not be octane, when edition is setEdition("classic") [deprecated]', function(assert) {
setEdition('classic');

assert.notOk(has('octane'));
});

test('should infer edition from process.env.EMBER_VERSION with a warning', function(assert) {
assert.expect(2);

process.env.EMBER_VERSION = 'octane';
console.log = (...args) => {
assert.deepEqual(args, [
'Please update to using @ember/edition-utils. Using process.env.EMBER_VERSION to declare your application / addon as "octane" ready is deprecated.',
]);
}

assert.ok(has('octane'), 'finds process.env.EMBER_VERSION');
assert.ok(has('octane'), 'finds process.env.EMBER_VERSION');
});
});
});
});
Loading

0 comments on commit e67e639

Please sign in to comment.