Skip to content

Commit

Permalink
detecting dependency version did not worked reliable in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan committed Nov 16, 2020
1 parent 758f05f commit 8244266
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 120 deletions.
25 changes: 10 additions & 15 deletions node-tests/e2e/test-support-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ const denodeify = require('denodeify');
const request = denodeify(require('request'));
const {
CSP_META_TAG_REG_EXP,
getInstalledVersionOfDependency,
patchPackageJson,
removeConfig,
setConfig
setConfig,
setResolutionForDependency,
} = require('../utils');
const path = require('path');
const semver = require('semver');

describe('e2e: provides test support', function() {
this.timeout(300000);
Expand Down Expand Up @@ -44,12 +42,10 @@ describe('e2e: provides test support', function() {
// default blueprints until Ember 3.4. We ask consumer to upgrade QUnit to
// a more recent QUnit version in a warning that we log in that case.
// We need to simulate this in our tests as well.
const qunitVersion = await getInstalledVersionOfDependency(testProject, 'qunit');
if (!semver.satisfies(qunitVersion, '>= 2.9.2')) {
// enforce newer version using yarn resolutions feature
await patchPackageJson(testProject, { resolutions: { qunit: '2.9.2' }});
await testProject.runCommand('yarn', 'install');
}
//
// TODO: Only add resolution if running for old Ember CLI version.
await setResolutionForDependency(testProject, { qunit: '>= 2.9.2' });
await testProject.runCommand('yarn', 'install');

await testProject.addOwnPackageAsDevDependency('ember-cli-content-security-policy');
});
Expand Down Expand Up @@ -83,11 +79,10 @@ describe('e2e: provides test support', function() {

// Enforce recent enought qunit version.
// See global `before` hook of this test file for more context.
const qunitVersion = await getInstalledVersionOfDependency(testProject, 'qunit');
if (!semver.satisfies(qunitVersion, '>= 2.9.2')) {
await patchPackageJson(testProject, { resolutions: { qunit: '2.9.2' }});
await testProject.runCommand('yarn', 'install');
}
//
// TODO: Only add resolution if running for old Ember CLI version.
await setResolutionForDependency(testProject, { qunit: '>= 2.9.2' });
await testProject.runCommand('yarn', 'install');

await testProject.runEmberCommand('test');

Expand Down
33 changes: 15 additions & 18 deletions node-tests/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { merge } = require('lodash');
const path = require('path');
const fs = require('fs');

const CONFIG_PATH = 'config/content-security-policy.js';
const CSP_META_TAG_REG_EXP = /<meta http-equiv="Content-Security-Policy" content="(.*)">/i;
Expand All @@ -25,30 +26,26 @@ function extractRunTimeConfig(html) {
return JSON.parse(decodeURIComponent(encodedConfig));
}

async function getInstalledVersionOfDependency(testProject, dependency) {
const { stdout: yarnWhyOutput } = await testProject.runCommand('yarn', 'why', dependency);
const matches = yarnWhyOutput.match(
new RegExp(
`=> Found "${dependency}@(\\d+.\\d+.\\d+)"`
)
async function setResolutionForDependency(testProject, resolutions) {
// resolutions must be defined in package.json at workspace root
const workspaceRoot = path.join(testProject.path, '..', '..');
const packageJsonFile = path.join(workspaceRoot, 'package.json');
const packageJsonContent = JSON.parse(
fs.readFileSync(packageJsonFile, { encoding: 'utf-8' })
);
const [, version] = matches;
return version;
}

async function patchPackageJson(testProject, patch) {
const packageJson = JSON.parse(
await testProject.readFile('package.json')
);
merge(packageJson, patch);
await testProject.writeFile('package.json', JSON.stringify(packageJson));
if (!packageJsonContent.resolutions) {
packageJsonContent.resolutions = {};
}
Object.assign(packageJsonContent.resolutions, resolutions);

fs.writeFileSync(packageJsonFile, JSON.stringify(packageJsonContent));
}

module.exports = {
CSP_META_TAG_REG_EXP,
extractRunTimeConfig,
getInstalledVersionOfDependency,
patchPackageJson,
removeConfig,
setConfig,
setResolutionForDependency,
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"execa": "^4.1.0",
"fs-extra": "^9.0.0",
"loader.js": "^4.7.0",
"lodash": "^4.17.20",
"mocha": "^7.0.0",
"qunit-dom": "^1.0.0",
"release-it": "^13.0.2",
Expand Down
Loading

0 comments on commit 8244266

Please sign in to comment.