Skip to content

Commit

Permalink
Add helpers instance to RunContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 4, 2021
1 parent fefe12e commit 161076f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
14 changes: 9 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ YeomanTest.prototype.run = function (
return new RunContext(
GeneratorOrNamespace,
{...contextSettings, ...settings},
envOptions
envOptions,
this
).withOptions(generatorOptions);
};

Expand All @@ -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;
};
52 changes: 41 additions & 11 deletions lib/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
});
Expand All @@ -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') {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
};

Expand Down Expand Up @@ -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))
);
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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,
Expand Down

0 comments on commit 161076f

Please sign in to comment.