| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 |
1
1
1
1
1
1
1
1
1
1
1
5
5
5
1
1
2
2
2
2
2
48
48
12
48
1
1
1
1
2
1
5
5
15
5
5
1
1
1
1
1
1
1
1
5
5
5
5
5
5
5
5
1
1
54
6
1
4
4
4
4
4
4
4
4
4
4
4
| 'use strict';
var fs = require('fs');
var path = require('path');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var util = require('util');
var assert = require('./assert');
var _ = require('lodash');
var generators = require('../..');
// Mocha helpers
var helpers = module.exports;
helpers.decorated = [];
/**
* Create a function that will clean up the test directory,
* cd into it, and create a dummy gruntfile inside. Intended for use
* as a callback for the mocha `before` hook.
*
* @param {String} dir - path to the test directory
* @returns {Function} mocha callback
*/
helpers.setUpTestDirectory = function before(dir) {
return function (done) {
helpers.testDirectory(dir, function () {
helpers.gruntfile({ dummy: true }, done);
});
};
};
/**
* Create a function that will clean up the test directory,
* cd into it, and create a dummy gruntfile inside. Intended for use
* as a callback for the mocha `before` hook.
*
* @deprecated
* @param {String} dir - path to the test directory
* @returns {Function} mocha callback
*/
helpers.before = function (dir) {
console.log('before is deprecated. Use setUpTestDirectory instead');
return helpers.setUpTestDirectory(dir);
};
/**
* Wrap a method with custom functionality.
*
* @param {Object} context - context to find the original method
* @param {String} method - name of the method to wrap
* @param {Function} replacement - executes before the original method
* @param {Object} options - config settings
*/
helpers.decorate = function decorate(context, method, replacement, options) {
options = options || {};
replacement = replacement || function () {};
var naturalMethod = context[method];
helpers.decorated.push({
context: context,
method: method,
naturalMethod: naturalMethod
});
context[method] = function () {
var rep = replacement.apply(context, arguments);
if (!options.stub) {
naturalMethod.apply(context, arguments);
}
return rep;
};
};
/**
* Override a method with custom functionality.
* @param {Object} context - context to find the original method
* @param {String} method - name of the method to wrap
* @param {Function} replacement - executes before the original method
*/
helpers.stub = function stub(context, method, replacement) {
helpers.decorate(context, method, replacement, { stub: true });
};
/**
* Restore the original behavior of all decorated and stubbed methods
*/
helpers.restore = function restore() {
helpers.decorated.forEach(function (dec) {
dec.context[dec.method] = dec.naturalMethod;
});
};
/**
*
* Generates a new Gruntfile.js in the current working directory based on
* options hash passed in.
*
* @param {Object} options - Grunt configuration
* @param {Function} done - callback to call on completion
* @example
* before(helpers.gruntfile({
* foo: {
* bar: '<config.baz>'
* }
* }));
*
*/
helpers.gruntfile = function (options, done) {
var config = 'grunt.initConfig(' + JSON.stringify(options, null, 2) + ');';
config = config.split('\n').map(function (line) {
return ' ' + line;
}).join('\n');
var out = [
'module.exports = function (grunt) {',
config,
'};'
];
fs.writeFile('Gruntfile.js', out.join('\n'), done);
};
// Façade assert module for backward compatibility
helpers.assertFile = assert.file;
helpers.assertNoFile = assert.noFile;
helpers.assertFiles = assert.files;
helpers.assertFileContent = assert.fileContent;
helpers.assertNoFileContent = assert.noFileContent;
helpers.assertTextEqual = assert.textEqual;
helpers.assertImplement = assert.implement;
/**
* Clean-up the test directory and cd into it.
* Call given callback after entering the test directory.
* @param {String} dir - path to the test directory
* @param {Function} cb - callback executed after setting working directory to dir
* @example
* testDirectory(path.join(__dirname, './temp'), function () {
* fs.writeFileSync('testfile', 'Roses are red.');
* );
*/
helpers.testDirectory = function (dir, cb) {
Iif (!dir) {
throw new Error('Missing directory');
}
dir = path.resolve(dir);
// Make sure we're not deleting CWD by moving to top level folder. As we `cd` in the
// test dir after cleaning up, this shouldn't be perceivable.
process.chdir('/');
rimraf(dir, function (err) {
Iif (err) {
return cb(err);
}
mkdirp.sync(dir);
process.chdir(dir);
cb();
});
};
/**
* Answer prompt questions for the passed-in generator
* @param {Generator} generator - a Yeoman generator
* @param {Object} answers - an object where keys are the
* generators prompt names and values are the answers to
* the prompt questions
* @example
* mockPrompt(angular, {'bootstrap': 'Y', 'compassBoostrap': 'Y'});
*/
helpers.mockPrompt = function (generator, answers) {
var origPrompt = generator.prompt;
generator.prompt = function (prompts, done) {
done(answers);
};
generator.origPrompt = origPrompt;
};
/**
* Create a simple, dummy generator
*/
helpers.createDummyGenerator = function () {
return generators.Base.extend({
test: function () {
this.shouldRun = true;
}
});
};
/**
* Create a generator, using the given dependencies and controller arguments
* Dependecies can be path (autodiscovery) or an array [<generator>, <name>]
*
* @param {String} name - the name of the generator
* @param {Array} dependencies - paths to the generators dependencies
* @param {Array|String} args - arguments to the generator;
* if String, will be split on spaces to create an Array
* @param {Object} options - configuration for the generator
* @example
* var deps = ['../../app',
* '../../common',
* '../../controller',
* '../../main',
* [createDummyGenerator(), 'testacular:app']
* ];
* var angular = createGenerator('angular:app', deps);
*/
helpers.createGenerator = function (name, dependencies, args, options) {
var env = generators();
dependencies.forEach(function (d) {
Eif (d instanceof Array) {
env.registerStub(d[0], d[1]);
} else {
env.register(d);
}
});
var generator = env.create(name, { arguments: args, options: options });
generator.on('start', env.emit.bind(this, 'generators:start'));
generator.on('start', env.emit.bind(this, name + ':start'));
generator.on('method', function (method) {
env.emit(name + ':' + method);
});
generator.on('end', env.emit.bind(this, name + ':end'));
generator.on('end', env.emit.bind(this, 'generators:end'));
return generator;
};
|