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

Remove use of core-object for tasks #999

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
1 change: 0 additions & 1 deletion lib/commands/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
let ResetTask = require('../tasks/reset');

let resetTask = new ResetTask({
ui: this.ui,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used in ResetTask.

project: this.project,
config,
});
Expand Down
19 changes: 12 additions & 7 deletions lib/tasks/reset.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
'use strict';

const CoreObject = require('core-object');
const debug = require('debug')('ember-try:commands:reset');
const ScenarioManager = require('../utils/scenario-manager');
const DependencyManagerAdapterFactory = require('./../utils/dependency-manager-adapter-factory');

module.exports = CoreObject.extend({
module.exports = class ResetTask {
constructor(options) {
this.config = options.config;
this.project = options.project;
}

run() {
let dependencyAdapters =
this.dependencyManagerAdapters ||
Copy link
Member Author

@bertdeblock bertdeblock Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dependencyManagerAdapters is never passed in as an option for the reset task, so removed it here for now.

DependencyManagerAdapterFactory.generateFromConfig(this.config, this.project.root);
let dependencyAdapters = DependencyManagerAdapterFactory.generateFromConfig(
this.config,
this.project.root,
);
debug(
'DependencyManagerAdapters: %s',
dependencyAdapters.map((item) => {
return item.configKey;
}),
);
return new ScenarioManager({ dependencyManagerAdapters: dependencyAdapters }).cleanup();
},
});
}
};
42 changes: 25 additions & 17 deletions lib/tasks/try-each.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict';

const CoreObject = require('core-object');
const chalk = require('chalk');
const debug = require('debug')('ember-try:task:try-each');
const runCommand = require('./../utils/run-command');

module.exports = CoreObject.extend({
module.exports = class TryEachTask {
constructor(options) {
this.commandArgs = options.commandArgs;
this.commandOptions = options.commandOptions;
this.config = options.config;
this.dependencyManagerAdapters = options.dependencyManagerAdapters;
this.project = options.project;
this.ui = options.ui;
}

async run(scenarios, options) {
// Required lazily to improve startup speed.
let ScenarioManager = require('./../utils/scenario-manager');
Expand Down Expand Up @@ -58,7 +66,7 @@ module.exports = CoreObject.extend({

return 1; // Signifies exit code
}
},
}

async _runCommandForThisScenario(scenario) {
if (this._canceling) {
Expand Down Expand Up @@ -98,18 +106,18 @@ module.exports = CoreObject.extend({
this._writeFooter(`Result: ${result}`);

return runResults;
},
}

_writeHeader(text) {
let count = 75 - text.length;
let separator = new Array(count + 1).join('=');
this.ui.writeLine(chalk.blue(`\n=== ${text} ${separator}\n`));
},
}

_writeFooter(text) {
this.ui.writeLine(chalk.blue(`\n${text}`));
this.ui.writeLine(chalk.blue('---\n'));
},
}

_determineCommandFor(scenario) {
if (this.commandArgs && this.commandArgs.length) {
Expand All @@ -125,35 +133,35 @@ module.exports = CoreObject.extend({
}

return this._defaultCommandArgs();
},
}

_runCommand(options) {
return runCommand(this.project.root, options.commandArgs, options.commandOptions);
},
}

_commandOptions(env) {
let options = this.commandOptions || {};
if (env) {
options.env = Object.assign({}, process.env, env);
}
return options;
},
}

_defaultCommandArgs() {
return ['ember', 'test'];
},
}

_printResults(results) {
new this.ResultSummary({ ui: this.ui, results }).print();
},
}

_exitAsAppropriate(results) {
let outcomes = results.map((result) => {
return result.result || result.allowedToFail;
});

return this._exitBasedOnCondition(outcomes.indexOf(false) > -1);
},
}

async _optionallyCleanup(options) {
debug('Cleanup');
Expand All @@ -166,20 +174,20 @@ module.exports = CoreObject.extend({
debug('Cleanup ScenarioManager');
return await this.ScenarioManager.cleanup();
}
},
}

_exitBasedOnCondition(condition) {
let exitCode = condition ? 1 : 0;
debug('Exit %s', exitCode);
return exitCode;
},
}

_exit(code) {
debug('Exit %s', code);
process.exit(code);
},
}

_on(signal, fn) {
process.on(signal, fn);
},
});
}
};
41 changes: 27 additions & 14 deletions test/tasks/try-each-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ describe('tryEach', () => {
ui: { writeLine: outputFn },
project: { root: tmpdir },
config,
_on() {},
});

tryEachTask._on = () => {};

writeJSONFile('package.json', fixturePackage);
fs.writeFileSync('yarn.lock', '');
fs.mkdirSync('node_modules');
Expand Down Expand Up @@ -143,9 +144,10 @@ describe('tryEach', () => {
ui: { writeLine: outputFn },
project: { root: tmpdir },
config,
_on() {},
});

tryEachTask._on = () => {};

writeJSONFile('package.json', fixturePackage);
fs.mkdirSync('node_modules');
return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
Expand Down Expand Up @@ -207,9 +209,10 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [],
_on() {},
});

tryEachTask._on = () => {};

let exitCode = await tryEachTask.run(config.scenarios, {});

expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');
Expand Down Expand Up @@ -256,9 +259,10 @@ describe('tryEach', () => {
commandArgs: ['ember', 'serve'],
commandOptions: { timeout: { length: 20000, isSuccess: true } },
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');
expect(output).to.include('Scenario first: SUCCESS');
Expand Down Expand Up @@ -306,9 +310,10 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(output).to.include('Scenario first: FAIL (Allowed)');
expect(output).to.include('Scenario second: FAIL (Allowed)');
Expand Down Expand Up @@ -355,9 +360,10 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(output).to.include('Scenario first: FAIL');
expect(output).to.include('Scenario second: FAIL (Allowed)');
Expand Down Expand Up @@ -405,9 +411,10 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(output).to.include('Scenario first: SUCCESS');
expect(output).to.include('Scenario second: SUCCESS');
Expand Down Expand Up @@ -460,9 +467,10 @@ describe('tryEach', () => {
config,
commandArgs: [],
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');
expect(output).to.include('Scenario first: SUCCESS');
Expand Down Expand Up @@ -506,9 +514,10 @@ describe('tryEach', () => {
config,
commandArgs: ['ember', 'serve'],
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');
expect(output).to.include('Scenario first: SUCCESS');
Expand Down Expand Up @@ -576,9 +585,10 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');

Expand Down Expand Up @@ -624,9 +634,10 @@ describe('tryEach', () => {
config,
commandArgs: ['ember', 'help', '--json', 'true'],
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');
expect(output).to.include(
Expand Down Expand Up @@ -677,9 +688,10 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
});

tryEachTask._on = () => {};

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');

Expand Down Expand Up @@ -730,10 +742,11 @@ describe('tryEach', () => {
project: { root: tmpdir },
config,
dependencyManagerAdapters: [new StubDependencyAdapter()],
_on() {},
_runCommand: mockRunCommand,
});

tryEachTask._on = () => {};
tryEachTask._runCommand = mockRunCommand;

return tryEachTask.run(config.scenarios, {}).then((exitCode) => {
expect(exitCode).to.equal(0, 'exits 0 when all scenarios succeed');
expect(scenarios).to.eql(['first']);
Expand Down
Loading