From 161076fa9d66398111a7f73a7b81a6cff3525f73 Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Thu, 4 Mar 2021 16:05:05 -0300 Subject: [PATCH] Add helpers instance to RunContext. --- lib/index.js | 14 ++++++++----- lib/run-context.js | 52 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/lib/index.js b/lib/index.js index 77df98c..738a9ce 100644 --- a/lib/index.js +++ b/lib/index.js @@ -304,7 +304,8 @@ YeomanTest.prototype.run = function ( return new RunContext( GeneratorOrNamespace, {...contextSettings, ...settings}, - envOptions + envOptions, + this ).withOptions(generatorOptions); }; @@ -326,10 +327,13 @@ YeomanTest.prototype.create = function ( ); }; -module.exports = new YeomanTest(); +const helpers = new YeomanTest(); +helpers.YeomanTest = YeomanTest; -module.exports.YeomanTest = YeomanTest; +module.exports = helpers; -module.exports.createHelpers = (options) => { - return Object.assign(new YeomanTest(), options); +helpers.createHelpers = (options) => { + const helpers = new YeomanTest(); + Object.assign(helpers, options); + return helpers; }; diff --git a/lib/run-context.js b/lib/run-context.js index 91c1cc5..73391a2 100644 --- a/lib/run-context.js +++ b/lib/run-context.js @@ -7,7 +7,6 @@ const assert = require('assert'); const _ = require('lodash/string'); const util = require('util'); const {EventEmitter} = require('events'); -const helpers = require('.'); const RunResult = require('./run-result'); /** @@ -26,7 +25,12 @@ const RunResult = require('./run-result'); * @return {this} */ -function RunContext(Generator, settings, envOptions = {}) { +function RunContext( + Generator, + settings, + envOptions = {}, + helpers = require('.') +) { this._asyncHolds = 0; this.ran = false; this.inDirSet = false; @@ -59,6 +63,8 @@ function RunContext(Generator, settings, envOptions = {}) { if (!this.settings.runEnvironment) { setTimeout(this._run.bind(this), 10); } + + this.helpers = helpers; } util.inherits(RunContext, EventEmitter); @@ -102,7 +108,7 @@ RunContext.prototype.build = function (callback = () => {}) { this.targetDirectory = this.targetDirectory || process.cwd(); - const testEnv = helpers.createTestEnv(this.envOptions.createEnv, { + const testEnv = this.helpers.createTestEnv(this.envOptions.createEnv, { ...this.options, ...this.envOptions }); @@ -112,7 +118,7 @@ RunContext.prototype.build = function (callback = () => {}) { this.env.lookup(lookup); }); - helpers.registerDependencies(this.env, this.dependencies); + this.helpers.registerDependencies(this.env, this.dependencies); let namespace; if (typeof this.Generator === 'string') { @@ -135,11 +141,11 @@ RunContext.prototype.build = function (callback = () => {}) { options: this.options }); - helpers.mockPrompt(this.env, this.answers, this.promptOptions); + this.helpers.mockPrompt(this.env, this.answers, this.promptOptions); if (this.localConfig) { // Only mock local config when withLocalConfig was called - helpers.mockLocalConfig(this.generator, this.localConfig); + this.helpers.mockLocalConfig(this.generator, this.localConfig); } callback(this); @@ -196,7 +202,7 @@ RunContext.prototype.run = function () { .runGenerator(this.generator) .then(() => new RunResult(this._createRunResultOptions())) .finally(() => { - helpers.restorePrompt(this.env); + this.helpers.restorePrompt(this.env); }); }; @@ -289,7 +295,7 @@ RunContext.prototype.setDir = function (dirPath, tmpdir) { RunContext.prototype.inDir = function (dirPath, cb) { this.setDir(dirPath, true); - helpers.testDirectory( + this.helpers.testDirectory( dirPath, (cb || (() => {})).bind(this, path.resolve(dirPath)) ); @@ -342,6 +348,30 @@ RunContext.prototype.inTmpDir = function (cb) { ); }; +/** + * Restore cwd to initial cwd. + * @return {this} run context instance + */ + +RunContext.prototype.restore = function () { + if (this.oldCwd) { + process.chdir(this.oldCwd); + } + + return this; +}; + +/** + * Clean the directory used for tests inside inDir/inTmpDir + * @param {Boolean} force - force directory cleanup for not tmpdir + */ +RunContext.prototype.cleanup = function () { + this.restore(); + if (this.settings.tmpdir !== false) { + this.cleanTestDirectory(); + } +}; + /** * Create an environment * @@ -445,7 +475,7 @@ RunContext.prototype.withPrompts = function (answers, options) { * '../../common', * '../../controller', * '../../main', - * [helpers.createDummyGenerator(), 'testacular:app'] + * [this.helpers.createDummyGenerator(), 'testacular:app'] * ]); * angular.on('end', function () { * // assert something @@ -463,7 +493,7 @@ RunContext.prototype.withGenerators = function (dependencies) { * @param {Array} namespaces - namespaces of mocked generators * @return {this} * @example - * var angular = helpers + * var angular = this.helpers * .create('../../app') * .withMockedGenerators([ * 'foo:app', @@ -479,7 +509,7 @@ RunContext.prototype.withMockedGenerators = function (namespaces) { assert(Array.isArray(namespaces), 'namespaces should be an array'); const entries = namespaces.map((namespace) => [ namespace, - helpers.createMockedGenerator() + this.helpers.createMockedGenerator() ]); this.mockedGenerators = { ...this.mockedGenerators,