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

Restore backup files before changing to new dependency set for new scenarios. #451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dependency-manager-adapters/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = CoreObject.extend({

async cleanup() {
try {
await this._restoreOriginalDependencies();
await this.restoreOriginalDependencies();

debug('Remove backup package.json and node_modules');

Expand Down Expand Up @@ -204,7 +204,7 @@ module.exports = CoreObject.extend({
});
},

_restoreOriginalDependencies() {
restoreOriginalDependencies() {
debug('Restoring original package.json and node_modules');

let restoreTasks = [
Expand Down
6 changes: 6 additions & 0 deletions lib/dependency-manager-adapters/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ module.exports = CoreObject.extend({
return currentDeps;
},

restoreOriginalDependencies() {
return Promise.all(
this._packageAdapters.map((adapter) => adapter.restoreOriginalDependencies())
);
},

cleanup() {
return Promise.all(this._packageAdapters.map((adapter) => adapter.cleanup()));
},
Expand Down
4 changes: 4 additions & 0 deletions lib/tasks/try-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module.exports = CoreObject.extend({
let results = [];
for (let scenario of scenarios) {
results.push(await this._runCommandForThisScenario(scenario));

if (scenarios.length > 1) {
await this.ScenarioManager.restoreOriginalDependencies();
}
}

await this._optionallyCleanup(options);
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/scenario-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ module.exports = CoreObject.extend({
await depManager.cleanup();
}
},

async restoreOriginalDependencies() {
for (let depManager of this.dependencyManagerAdapters) {
await depManager.restoreOriginalDependencies();
}
},
});
6 changes: 3 additions & 3 deletions test/dependency-manager-adapters/npm-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,13 @@ describe('npmAdapter', () => {
});
});

describe('#_restoreOriginalDependencies', () => {
describe('#restoreOriginalDependencies', () => {
it('replaces the package.json with the backed up version', () => {
writeJSONFile('package.json.ember-try', { originalPackageJSON: true });
writeJSONFile('package.json', { originalPackageJSON: false });
fs.mkdirSync('.node_modules.ember-try');
writeJSONFile('.node_modules.ember-try/prove-it.json', { originalNodeModules: true });
return new NpmAdapter({ cwd: tmpdir })._restoreOriginalDependencies().then(() => {
return new NpmAdapter({ cwd: tmpdir }).restoreOriginalDependencies().then(() => {
assertFileContainsJSON(path.join(tmpdir, 'package.json'), { originalPackageJSON: true });
assertFileContainsJSON(path.join(tmpdir, 'node_modules/prove-it.json'), {
originalNodeModules: true,
Expand All @@ -379,7 +379,7 @@ describe('npmAdapter', () => {
writeJSONFile('npm-shrinkwrap.json', { originalNpmShrinkWrap: false });
writeJSONFile('package-lock.json.ember-try', { originalPackageLock: true });
writeJSONFile('package-lock.json', { originalPackageLock: false });
return new NpmAdapter({ cwd: tmpdir })._restoreOriginalDependencies().then(() => {
return new NpmAdapter({ cwd: tmpdir }).restoreOriginalDependencies().then(() => {
assertFileContainsJSON(path.join(tmpdir, 'package.json'), { originalPackageJSON: true });
assertFileContainsJSON(path.join(tmpdir, 'node_modules/prove-it.json'), {
originalNodeModules: true,
Expand Down
71 changes: 71 additions & 0 deletions test/dependency-manager-adapters/workspace-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,77 @@ describe('workspaceAdapter', () => {
});
});

describe('#restoreOriginalDependencies', () => {
it('replaces the package.json with the backed up version', () => {
fs.ensureDirSync('packages/test/node_modules');
writeJSONFile('packages/test/package.json', { originalPackageJSON: false });
writeJSONFile('packages/test/node_modules/prove-it.json', { originalNodeModules: false });

fs.ensureDirSync('packages/test/.node_modules.ember-try');
writeJSONFile('packages/test/package.json.ember-try', { originalPackageJSON: true });
writeJSONFile('packages/test/.node_modules.ember-try/prove-it.json', {
originalNodeModules: true,
});

let workspaceAdapter = new WorkspaceAdapter({
cwd: tmpdir,
useYarnCommand: true,
run: () => Promise.resolve(),
});

return workspaceAdapter.restoreOriginalDependencies().then(() => {
assertFileContainsJSON(path.join(tmpdir, 'packages/test/package.json'), {
originalPackageJSON: true,
});
assertFileContainsJSON(path.join(tmpdir, 'packages/test/node_modules/prove-it.json'), {
originalNodeModules: true,
});
});
});

it('replaces the yarn.lock, npm-shrinkwrap.json and package-lock.json with the backed up version if they exist', () => {
fs.ensureDirSync('packages/test/node_modules');
writeJSONFile('packages/test/package.json', { originalPackageJSON: false });
writeJSONFile('packages/test/node_modules/prove-it.json', { originalNodeModules: false });
writeJSONFile('packages/test/.yarn.lock', { originalYarnLock: false });
writeJSONFile('packages/test/npm-shrinkwrap.json', { originalNpmShrinkWrap: false });
writeJSONFile('packages/test/package-lock.json', { originalPackageLock: false });

fs.ensureDirSync('packages/test/.node_modules.ember-try');
writeJSONFile('packages/test/package.json.ember-try', { originalPackageJSON: true });
writeJSONFile('packages/test/.node_modules.ember-try/prove-it.json', {
originalNodeModules: true,
});
writeJSONFile('packages/test/yarn.lock.ember-try', { originalYarnLock: true });
writeJSONFile('packages/test/npm-shrinkwrap.json.ember-try', { originalNpmShrinkWrap: true });
writeJSONFile('packages/test/package-lock.json.ember-try', { originalPackageLock: true });

let workspaceAdapter = new WorkspaceAdapter({
cwd: tmpdir,
useYarnCommand: true,
run: () => Promise.resolve(),
});

return workspaceAdapter.restoreOriginalDependencies().then(() => {
assertFileContainsJSON(path.join(tmpdir, 'packages/test/package.json'), {
originalPackageJSON: true,
});
assertFileContainsJSON(path.join(tmpdir, 'packages/test/node_modules/prove-it.json'), {
originalNodeModules: true,
});
assertFileContainsJSON(path.join(tmpdir, 'packages/test/yarn.lock'), {
originalYarnLock: true,
});
assertFileContainsJSON(path.join(tmpdir, 'packages/test/npm-shrinkwrap.json'), {
originalNpmShrinkWrap: true,
});
assertFileContainsJSON(path.join(tmpdir, 'packages/test/package-lock.json'), {
originalPackageLock: true,
});
});
});
});

describe('#cleanup', () => {
it('works without having called #setup first', () => {
fs.ensureDirSync('packages/test/node_modules');
Expand Down
19 changes: 9 additions & 10 deletions test/helpers/stub-dependency-manager-adapter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
'use strict';

const CoreObject = require('core-object');
const RSVP = require('rsvp');

module.exports = CoreObject.extend({
setup() {
return RSVP.resolve();
},
changeToDependencySet() {
return RSVP.resolve([
async setup() {},

async changeToDependencySet() {
return [
{
name: 'testDep',
versionExpected: '2.0.0',
versionSeen: '2.1.0',
},
]);
},
cleanup() {
return RSVP.resolve();
];
},

async restoreOriginalDependencies() {},

async cleanup() {},
});