From 819a6d587e44b92036ea82c19280a24c5c54edbe Mon Sep 17 00:00:00 2001 From: Bert De Block Date: Tue, 10 Dec 2024 19:06:56 +0100 Subject: [PATCH] Remove use of `core-object` for tasks --- lib/commands/reset.js | 1 - lib/tasks/reset.js | 19 ++++++++++------- lib/tasks/try-each.js | 42 ++++++++++++++++++++++--------------- test/tasks/try-each-test.js | 41 +++++++++++++++++++++++------------- 4 files changed, 64 insertions(+), 39 deletions(-) diff --git a/lib/commands/reset.js b/lib/commands/reset.js index 83ce3b08..892c0f64 100644 --- a/lib/commands/reset.js +++ b/lib/commands/reset.js @@ -10,7 +10,6 @@ module.exports = { let ResetTask = require('../tasks/reset'); let resetTask = new ResetTask({ - ui: this.ui, project: this.project, config, }); diff --git a/lib/tasks/reset.js b/lib/tasks/reset.js index 9d867b3b..c258da88 100644 --- a/lib/tasks/reset.js +++ b/lib/tasks/reset.js @@ -1,15 +1,20 @@ '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 || - DependencyManagerAdapterFactory.generateFromConfig(this.config, this.project.root); + let dependencyAdapters = DependencyManagerAdapterFactory.generateFromConfig( + this.config, + this.project.root, + ); debug( 'DependencyManagerAdapters: %s', dependencyAdapters.map((item) => { @@ -17,5 +22,5 @@ module.exports = CoreObject.extend({ }), ); return new ScenarioManager({ dependencyManagerAdapters: dependencyAdapters }).cleanup(); - }, -}); + } +}; diff --git a/lib/tasks/try-each.js b/lib/tasks/try-each.js index a52b1147..ce5269a3 100644 --- a/lib/tasks/try-each.js +++ b/lib/tasks/try-each.js @@ -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'); @@ -58,7 +66,7 @@ module.exports = CoreObject.extend({ return 1; // Signifies exit code } - }, + } async _runCommandForThisScenario(scenario) { if (this._canceling) { @@ -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) { @@ -125,11 +133,11 @@ module.exports = CoreObject.extend({ } return this._defaultCommandArgs(); - }, + } _runCommand(options) { return runCommand(this.project.root, options.commandArgs, options.commandOptions); - }, + } _commandOptions(env) { let options = this.commandOptions || {}; @@ -137,15 +145,15 @@ module.exports = CoreObject.extend({ 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) => { @@ -153,7 +161,7 @@ module.exports = CoreObject.extend({ }); return this._exitBasedOnCondition(outcomes.indexOf(false) > -1); - }, + } async _optionallyCleanup(options) { debug('Cleanup'); @@ -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); - }, -}); + } +}; diff --git a/test/tasks/try-each-test.js b/test/tasks/try-each-test.js index daebe457..66e8de0d 100644 --- a/test/tasks/try-each-test.js +++ b/test/tasks/try-each-test.js @@ -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'); @@ -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) => { @@ -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'); @@ -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'); @@ -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)'); @@ -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)'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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( @@ -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'); @@ -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']);