Skip to content

Commit

Permalink
Convert helpers into a class for easy customization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 26, 2021
1 parent a5bd580 commit d870dd3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 33 deletions.
104 changes: 71 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/* eslint-disable max-params */
/**
* Collection of unit test helpers. (mostly related to Mocha syntax)
* @module test/helpers
*/

'use strict';
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const sinon = require('sinon');
const Generator = require('yeoman-generator');
const adapter = require('./adapter');

/**
* Collection of unit test helpers. (mostly related to Mocha syntax)
* @class YeomanTest
*/

class YeomanTest {}

/**
* Create a function that will clean up the test directory,
* cd into it, and create a dummy gruntfile inside. Intended for use
Expand All @@ -20,10 +23,10 @@ const adapter = require('./adapter');
* @returns {Function} mocha callback
*/

exports.setUpTestDirectory = function (dir) {
YeomanTest.prototype.setUpTestDirectory = function (dir) {
return function (done) {
exports.testDirectory(dir, function () {
exports.gruntfile({dummy: true}, done);
this.testDirectory(dir, function () {
this.gruntfile({dummy: true}, done);
});
};
};
Expand All @@ -44,7 +47,7 @@ exports.setUpTestDirectory = function (dir) {
*
*/

exports.gruntfile = function (options, done) {
YeomanTest.prototype.gruntfile = function (options, done) {
let config = 'grunt.initConfig(' + JSON.stringify(options, null, 2) + ');';

config = config
Expand All @@ -70,7 +73,7 @@ exports.gruntfile = function (options, done) {
* });
*/

exports.testDirectory = function (dir, cb) {
YeomanTest.prototype.testDirectory = function (dir, cb) {
if (!dir) {
throw new Error('Missing directory');
}
Expand Down Expand Up @@ -105,7 +108,11 @@ exports.testDirectory = function (dir, cb) {
* mockPrompt(angular, {'bootstrap': 'Y', 'compassBoostrap': 'Y'});
*/

exports.mockPrompt = function (envOrGenerator, mockedAnswers, options) {
YeomanTest.prototype.mockPrompt = function (
envOrGenerator,
mockedAnswers,
options
) {
envOrGenerator = envOrGenerator.env || envOrGenerator;
const {promptModule} = envOrGenerator.adapter;
const {DummyPrompt} = adapter;
Expand All @@ -126,7 +133,7 @@ exports.mockPrompt = function (envOrGenerator, mockedAnswers, options) {
* Restore defaults prompts on a generator.
* @param {Generator|Environment} generator or environment
*/
exports.restorePrompt = function (envOrGenerator) {
YeomanTest.prototype.restorePrompt = function (envOrGenerator) {
envOrGenerator = envOrGenerator.env || envOrGenerator;
envOrGenerator.adapter.promptModule.restoreDefaultPrompts();
};
Expand All @@ -136,29 +143,35 @@ exports.restorePrompt = function (envOrGenerator) {
* @param {Generator} generator - a Yeoman generator
* @param {Object} localConfig - localConfig - should look just like if called config.getAll()
*/
exports.mockLocalConfig = function (generator, localConfig) {
YeomanTest.prototype.mockLocalConfig = function (generator, localConfig) {
generator.config.defaults(localConfig);
};

/**
* Create a mocked generator
*/

exports.createMockedGenerator = (constructor = Generator) => {
const generator = sinon.spy(constructor);
['run', 'queueTasks', 'runWithOptions', 'queueOwnTasks'].forEach(methodName => {
if (Generator.prototype[methodName]) {
generator.prototype[methodName] = sinon.stub();
YeomanTest.prototype.createMockedGenerator = (
Generator = require('yeoman-generator')
) => {
const generator = sinon.spy(Generator);
['run', 'queueTasks', 'runWithOptions', 'queueOwnTasks'].forEach(
(methodName) => {
if (Generator.prototype[methodName]) {
generator.prototype[methodName] = sinon.stub();
}
}
});
);
return generator;
};

/**
* Create a simple, dummy generator
*/

exports.createDummyGenerator = (Generator = require('yeoman-generator')) =>
YeomanTest.prototype.createDummyGenerator = (
Generator = require('yeoman-generator')
) =>
class extends Generator {
test() {
this.shouldRun = true;
Expand All @@ -185,7 +198,7 @@ exports.createDummyGenerator = (Generator = require('yeoman-generator')) =>
* var angular = createGenerator('angular:app', deps);
*/

exports.createGenerator = function (
YeomanTest.prototype.createGenerator = function (
name,
dependencies,
args,
Expand All @@ -205,7 +218,7 @@ exports.createGenerator = function (
* @param {Array} dependencies - paths to the generators dependencies
*/

exports.registerDependencies = function (env, dependencies) {
YeomanTest.prototype.registerDependencies = function (env, dependencies) {
dependencies.forEach(function (dependency) {
if (Array.isArray(dependency)) {
env.registerStub(...dependency);
Expand All @@ -231,7 +244,7 @@ exports.registerDependencies = function (env, dependencies) {
* });
*/

exports.createEnv = (...args) => {
YeomanTest.prototype.createEnv = (...args) => {
return require('yeoman-environment').createEnv(...args);
};

Expand All @@ -244,23 +257,29 @@ exports.createEnv = (...args) => {
* const env = createTestEnv(require('yeoman-environment').createEnv);
*/

exports.createTestEnv = function (
YeomanTest.prototype.createTestEnv = function (
envContructor = this.createEnv,
options = {localConfigOnly: true}
) {
const envOptions = _.cloneDeep(this.environmentOptions || {});
if (typeof options === 'boolean') {
options = {
sharedOptions: {localConfigOnly: options},
newErrorHandler: true
newErrorHandler: true,
...envOptions,
sharedOptions: {
localConfigOnly: options,
...envOptions.sharedOptions
}
};
} else {
options = {
sharedOptions: {localConfigOnly: true},
newErrorHandler: true,
...envOptions,
...options
};
options.sharedOptions = {
localConfigOnly: true,
...envOptions.sharedOptions,
...options.sharedOptions
};
}
Expand All @@ -274,9 +293,19 @@ exports.createTestEnv = function (
* @return {RunContext}
*/

exports.run = function (GeneratorOrNamespace, settings, envOptions) {
YeomanTest.prototype.run = function (
GeneratorOrNamespace,
settings,
envOptions
) {
const RunContext = require('./run-context');
return new RunContext(GeneratorOrNamespace, settings, envOptions);
const contextSettings = _.cloneDeep(this.settings || {});
const generatorOptions = _.cloneDeep(this.generatorOptions || {});
return new RunContext(
GeneratorOrNamespace,
{...contextSettings, ...settings},
envOptions
).withOptions(generatorOptions);
};

/**
Expand All @@ -285,13 +314,22 @@ exports.run = function (GeneratorOrNamespace, settings, envOptions) {
* @return {RunContext}
*/

exports.create = function (GeneratorOrNamespace, settings, envOptions) {
const RunContext = require('./run-context');
const context = new RunContext(
YeomanTest.prototype.create = function (
GeneratorOrNamespace,
settings,
envOptions
) {
return this.run(
GeneratorOrNamespace,
{...settings, runEnvironment: true},
envOptions
);
};

module.exports = new YeomanTest();

module.exports.YeomanTest = YeomanTest;

return context;
module.exports.createHelpers = (options) => {
return Object.assign(new YeomanTest(), options);
};
4 changes: 4 additions & 0 deletions lib/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ RunContext.prototype.withArguments = function (args) {
*/

RunContext.prototype.withOptions = function (options) {
if (!options) {
return this;
}

// Add options as both kebab and camel case. This is to stay backward compatibles with
// the switch we made to meow for options parsing.
Object.keys(options).forEach(function (key) {
Expand Down

0 comments on commit d870dd3

Please sign in to comment.