yeoman-test 6.2.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js DELETED
@@ -1,339 +0,0 @@
1
- /* eslint-disable max-params */
2
-
3
- 'use strict';
4
- const fs = require('fs');
5
- const _ = require('lodash');
6
- const path = require('path');
7
- const sinon = require('sinon');
8
- const adapter = require('./adapter');
9
-
10
- /**
11
- * Collection of unit test helpers. (mostly related to Mocha syntax)
12
- * @class YeomanTest
13
- */
14
-
15
- class YeomanTest {}
16
-
17
- /**
18
- * Create a function that will clean up the test directory,
19
- * cd into it, and create a dummy gruntfile inside. Intended for use
20
- * as a callback for the mocha `before` hook.
21
- *
22
- * @param {String} dir - path to the test directory
23
- * @returns {Function} mocha callback
24
- */
25
-
26
- YeomanTest.prototype.setUpTestDirectory = function (dir) {
27
- return (done) => {
28
- this.testDirectory(dir, () => {
29
- this.gruntfile({dummy: true}, done);
30
- });
31
- };
32
- };
33
-
34
- /**
35
- *
36
- * Generates a new Gruntfile.js in the current working directory based on
37
- * options hash passed in.
38
- *
39
- * @param {Object} options - Grunt configuration
40
- * @param {Function} done - callback to call on completion
41
- * @example
42
- * before(helpers.gruntfile({
43
- * foo: {
44
- * bar: '<config.baz>'
45
- * }
46
- * }));
47
- *
48
- */
49
-
50
- YeomanTest.prototype.gruntfile = function (options, done) {
51
- let config = 'grunt.initConfig(' + JSON.stringify(options, null, 2) + ');';
52
-
53
- config = config
54
- .split('\n')
55
- .map(function (line) {
56
- return ' ' + line;
57
- })
58
- .join('\n');
59
-
60
- const out = ['module.exports = function (grunt) {', config, '};'];
61
-
62
- fs.writeFile('Gruntfile.js', out.join('\n'), done);
63
- };
64
-
65
- /**
66
- * Clean-up the test directory and cd into it.
67
- * Call given callback after entering the test directory.
68
- * @param {String} dir - path to the test directory
69
- * @param {Function} cb - callback executed after setting working directory to dir
70
- * @example
71
- * testDirectory(path.join(__dirname, './temp'), function () {
72
- * fs.writeFileSync('testfile', 'Roses are red.');
73
- * });
74
- */
75
-
76
- YeomanTest.prototype.testDirectory = function (dir, cb) {
77
- if (!dir) {
78
- throw new Error('Missing directory');
79
- }
80
-
81
- dir = path.resolve(dir);
82
-
83
- // Make sure we're not deleting CWD by moving to top level folder. As we `cd` in the
84
- // test dir after cleaning up, this shouldn't be perceivable.
85
- process.chdir('/');
86
-
87
- try {
88
- if (fs.existsSync(dir)) {
89
- fs.rmdirSync(dir, {recursive: true});
90
- }
91
-
92
- fs.mkdirSync(dir, {recursive: true});
93
- process.chdir(dir);
94
- cb();
95
- } catch (error) {
96
- return cb(error);
97
- }
98
- };
99
-
100
- /**
101
- * Answer prompt questions for the passed-in generator
102
- * @param {Generator|Environment} generator - a Yeoman generator or environment
103
- * @param {Object} answers - an object where keys are the
104
- * generators prompt names and values are the answers to
105
- * the prompt questions
106
- * @param {Function|Object} options - Options or callback
107
- * @example
108
- * mockPrompt(angular, {'bootstrap': 'Y', 'compassBoostrap': 'Y'});
109
- */
110
-
111
- YeomanTest.prototype.mockPrompt = function (
112
- envOrGenerator,
113
- mockedAnswers,
114
- options
115
- ) {
116
- envOrGenerator = envOrGenerator.env || envOrGenerator;
117
- const {promptModule} = envOrGenerator.adapter;
118
- const {DummyPrompt} = adapter;
119
-
120
- Object.keys(promptModule.prompts).forEach(function (name) {
121
- promptModule.registerPrompt(
122
- name,
123
- class CustomDummyPrompt extends DummyPrompt {
124
- constructor(question, rl, answers) {
125
- super(mockedAnswers, options, question, rl, answers);
126
- }
127
- }
128
- );
129
- });
130
- };
131
-
132
- /**
133
- * Restore defaults prompts on a generator.
134
- * @param {Generator|Environment} generator or environment
135
- */
136
- YeomanTest.prototype.restorePrompt = function (envOrGenerator) {
137
- envOrGenerator = envOrGenerator.env || envOrGenerator;
138
- envOrGenerator.adapter.promptModule.restoreDefaultPrompts();
139
- };
140
-
141
- /**
142
- * Provide mocked values to the config
143
- * @param {Generator} generator - a Yeoman generator
144
- * @param {Object} localConfig - localConfig - should look just like if called config.getAll()
145
- */
146
- YeomanTest.prototype.mockLocalConfig = function (generator, localConfig) {
147
- generator.config.defaults(localConfig);
148
- };
149
-
150
- /**
151
- * Create a mocked generator
152
- */
153
-
154
- YeomanTest.prototype.createMockedGenerator = (
155
- Generator = class MockedGenerator extends require('yeoman-generator') {}
156
- ) => {
157
- const generator = sinon.spy(Generator);
158
- ['run', 'queueTasks', 'runWithOptions', 'queueOwnTasks'].forEach(
159
- (methodName) => {
160
- if (Generator.prototype[methodName]) {
161
- generator.prototype[methodName] = sinon.stub();
162
- }
163
- }
164
- );
165
- return generator;
166
- };
167
-
168
- /**
169
- * Create a simple, dummy generator
170
- */
171
-
172
- YeomanTest.prototype.createDummyGenerator = (
173
- Generator = require('yeoman-generator')
174
- ) =>
175
- class extends Generator {
176
- test() {
177
- this.shouldRun = true;
178
- }
179
- };
180
-
181
- /**
182
- * Create a generator, using the given dependencies and controller arguments
183
- * Dependecies can be path (autodiscovery) or an array [{generator}, {name}]
184
- *
185
- * @param {String} name - the name of the generator
186
- * @param {Array} dependencies - paths to the generators dependencies
187
- * @param {Array|String} args - arguments to the generator;
188
- * if String, will be split on spaces to create an Array
189
- * @param {Object} options - configuration for the generator
190
- * @param {Boolean} [localConfigOnly=true] - passes localConfigOnly to the generators
191
- * @example
192
- * var deps = ['../../app',
193
- * '../../common',
194
- * '../../controller',
195
- * '../../main',
196
- * [createDummyGenerator(), 'testacular:app']
197
- * ];
198
- * var angular = createGenerator('angular:app', deps);
199
- */
200
-
201
- YeomanTest.prototype.createGenerator = function (
202
- name,
203
- dependencies,
204
- args,
205
- options,
206
- localConfigOnly = true
207
- ) {
208
- const env = this.createEnv([], {sharedOptions: {localConfigOnly}});
209
- this.registerDependencies(env, dependencies);
210
-
211
- return env.create(name, {arguments: args, options});
212
- };
213
-
214
- /**
215
- * Register a list of dependent generators into the provided env.
216
- * Dependecies can be path (autodiscovery) or an array [{generator}, {name}]
217
- *
218
- * @param {Array} dependencies - paths to the generators dependencies
219
- */
220
-
221
- YeomanTest.prototype.registerDependencies = function (env, dependencies) {
222
- dependencies.forEach(function (dependency) {
223
- if (Array.isArray(dependency)) {
224
- env.registerStub(...dependency);
225
- } else {
226
- env.register(dependency);
227
- }
228
- });
229
- };
230
-
231
- /**
232
- * Shortcut to the Environment's createEnv.
233
- *
234
- * @param {...any} args - environment constructor arguments.
235
- * @returns {Object} environment instance
236
- *
237
- * Use to test with specific Environment version:
238
- * let createEnv;
239
- * before(() => {
240
- * createEnv = stub(helper, 'createEnv').callsFake(Environment.creatEnv);
241
- * });
242
- * after(() => {
243
- * createEnv.restore();
244
- * });
245
- */
246
-
247
- YeomanTest.prototype.createEnv = (...args) => {
248
- return require('yeoman-environment').createEnv(...args);
249
- };
250
-
251
- /**
252
- * Creates a test environment.
253
- *
254
- * @param {Function} envContructor - environment constructor method.
255
- * @param {Object} [options] - Options to be passed to the environment
256
- * @returns {Object} environment instance
257
- * const env = createTestEnv(require('yeoman-environment').createEnv);
258
- */
259
-
260
- YeomanTest.prototype.createTestEnv = function (
261
- envContructor = this.createEnv,
262
- options = {localConfigOnly: true}
263
- ) {
264
- const envOptions = _.cloneDeep(this.environmentOptions || {});
265
- if (typeof options === 'boolean') {
266
- options = {
267
- newErrorHandler: true,
268
- ...envOptions,
269
- sharedOptions: {
270
- localConfigOnly: options,
271
- ...envOptions.sharedOptions
272
- }
273
- };
274
- } else {
275
- options = {
276
- newErrorHandler: true,
277
- ...envOptions,
278
- ...options
279
- };
280
- options.sharedOptions = {
281
- localConfigOnly: true,
282
- ...envOptions.sharedOptions,
283
- ...options.sharedOptions
284
- };
285
- }
286
-
287
- return envContructor([], options, new adapter.TestAdapter());
288
- };
289
-
290
- /**
291
- * Run the provided Generator
292
- * @param {String|Function} GeneratorOrNamespace - Generator constructor or namespace
293
- * @return {RunContext}
294
- */
295
-
296
- YeomanTest.prototype.run = function (
297
- GeneratorOrNamespace,
298
- settings,
299
- envOptions
300
- ) {
301
- const RunContext = require('./run-context');
302
- const contextSettings = _.cloneDeep(this.settings || {});
303
- const generatorOptions = _.cloneDeep(this.generatorOptions || {});
304
- return new RunContext(
305
- GeneratorOrNamespace,
306
- {...contextSettings, ...settings},
307
- envOptions,
308
- this
309
- ).withOptions(generatorOptions);
310
- };
311
-
312
- /**
313
- * Prepare a run context
314
- * @param {String|Function} GeneratorOrNamespace - Generator constructor or namespace
315
- * @return {RunContext}
316
- */
317
-
318
- YeomanTest.prototype.create = function (
319
- GeneratorOrNamespace,
320
- settings,
321
- envOptions
322
- ) {
323
- return this.run(
324
- GeneratorOrNamespace,
325
- {...settings, runEnvironment: true},
326
- envOptions
327
- );
328
- };
329
-
330
- const helpers = new YeomanTest();
331
- helpers.YeomanTest = YeomanTest;
332
-
333
- module.exports = helpers;
334
-
335
- helpers.createHelpers = (options) => {
336
- const helpers = new YeomanTest();
337
- Object.assign(helpers, options);
338
- return helpers;
339
- };