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/run-result.js DELETED
@@ -1,372 +0,0 @@
1
- 'use strict';
2
- const assert = require('assert');
3
- const fs = require('fs');
4
- const MemFsEditor = require('mem-fs-editor');
5
- const path = require('path');
6
-
7
- const helpers = require('.');
8
-
9
- const isObject = (object) =>
10
- typeof object === 'object' && object !== null && object !== undefined;
11
-
12
- function convertArgs(args) {
13
- if (args.length > 1) {
14
- return [[...args]];
15
- }
16
-
17
- const arg = args[0];
18
- return Array.isArray(arg) ? arg : [arg];
19
- }
20
-
21
- /**
22
- * This class provides utilities for testing generated content.
23
- */
24
-
25
- class RunResult {
26
- constructor(options = {cwd: process.cwd()}) {
27
- this.env = options.env;
28
- this.generator = options.generator;
29
- this.cwd = options.cwd;
30
- this.oldCwd = options.oldCwd;
31
- this.memFs = options.memFs;
32
- this.fs = this.memFs && MemFsEditor.create(this.memFs);
33
- this.mockedGenerators = options.mockedGenerators || {};
34
- this.options = options;
35
- if (this.memFs && !this.cwd) {
36
- throw new Error('CWD option is required for mem-fs tests');
37
- }
38
- }
39
-
40
- /**
41
- * Create another RunContext reusing the settings.
42
- * See helpers.create api
43
- */
44
- create(GeneratorOrNamespace, settings, envOptions) {
45
- return helpers.create(
46
- GeneratorOrNamespace,
47
- {
48
- ...this.options.settings,
49
- cwd: this.cwd,
50
- oldCwd: this.oldCwd,
51
- ...settings
52
- },
53
- {...this.options.envOptions, memFs: this.memFs, ...envOptions}
54
- );
55
- }
56
-
57
- /**
58
- * Return an object with fs changes.
59
- * @param {Function} filter - parameter forwarded to mem-fs-editor#dump
60
- * @returns {Object}
61
- */
62
- getSnapshot(filter) {
63
- return this.fs.dump(this.cwd, filter);
64
- }
65
-
66
- /**
67
- * Return an object with filenames with state.
68
- * @param {Function} filter - parameter forwarded to mem-fs-editor#dump
69
- * @returns {Object}
70
- */
71
- getStateSnapshot(filter) {
72
- const snapshot = this.getSnapshot(filter);
73
- Object.values(snapshot).forEach((dump) => {
74
- delete dump.contents;
75
- });
76
- return snapshot;
77
- }
78
-
79
- /**
80
- * Prints files names and contents from mem-fs
81
- * @param {...string} files - Files to print or empty for entire mem-fs
82
- * @returns {RunResult} this
83
- */
84
- dumpFiles(...files) {
85
- if (files.length === 0) {
86
- this.memFs.each((file) => {
87
- console.log(file.path);
88
- if (file.contents) {
89
- console.log(file.contents.toString('utf8'));
90
- }
91
- });
92
- return this;
93
- }
94
-
95
- files.forEach((file) => {
96
- console.log(this.fs.read(this._fileName(file)));
97
- });
98
- return this;
99
- }
100
-
101
- /**
102
- * Prints every file from mem-fs
103
- * @returns {RunResult} this
104
- */
105
- dumpFilenames() {
106
- this.memFs.each((file) => {
107
- console.log(file.path);
108
- });
109
- return this;
110
- }
111
-
112
- /**
113
- * Reverts to old cwd.
114
- * @returns {RunResult} this
115
- */
116
- restore() {
117
- process.chdir(this.oldCwd);
118
- return this;
119
- }
120
-
121
- /**
122
- * Deletes the test directory recursively.
123
- * @returns {RunResult} this
124
- */
125
- cleanup() {
126
- process.chdir(this.oldCwd);
127
- fs.rmdirSync(this.cwd, {recursive: true});
128
- return this;
129
- }
130
-
131
- _fileName(filename) {
132
- if (path.isAbsolute(filename)) {
133
- return filename;
134
- }
135
-
136
- return path.join(this.cwd, filename);
137
- }
138
-
139
- _readFile(filename, json) {
140
- filename = this._fileName(filename);
141
- let file;
142
- if (this.fs) {
143
- file = this.fs.read(filename, 'utf8');
144
- } else {
145
- file = fs.readFileSync(filename, 'utf8');
146
- }
147
-
148
- return json ? JSON.parse(file) : file;
149
- }
150
-
151
- _exists(filename) {
152
- filename = this._fileName(filename);
153
- if (this.fs) {
154
- return this.fs.exists(filename);
155
- }
156
-
157
- return fs.existsSync(filename);
158
- }
159
-
160
- /**
161
- * Assert that a file exists
162
- * @param {String} path - path to a file
163
- * @example
164
- * result.assertFile('templates/user.hbs');
165
- *
166
- * @also
167
- *
168
- * Assert that each files in the array exists
169
- * @param {Array} paths - an array of paths to files
170
- * @example
171
- * result.assertFile(['templates/user.hbs', 'templates/user/edit.hbs']);
172
- */
173
- assertFile() {
174
- convertArgs(arguments).forEach((file) => {
175
- const here = this._exists(file);
176
- assert.ok(here, `${file}, no such file or directory`);
177
- });
178
- }
179
-
180
- /**
181
- * Assert that a file doesn't exist
182
- * @param {String} file - path to a file
183
- * @example
184
- * result.assertNoFile('templates/user.hbs');
185
- *
186
- * @also
187
- *
188
- * Assert that each of an array of files doesn't exist
189
- * @param {Array} pairs - an array of paths to files
190
- * @example
191
- * result.assertNoFile(['templates/user.hbs', 'templates/user/edit.hbs']);
192
- */
193
- assertNoFile() {
194
- convertArgs(arguments).forEach((file) => {
195
- const here = this._exists(file);
196
- assert.ok(!here, `${file} exists`);
197
- });
198
- }
199
-
200
- /**
201
- * Assert that a file's content matches a regex or string
202
- * @param {String} file - path to a file
203
- * @param {Regex|String} reg - regex / string that will be used to search the file
204
- * @example
205
- * result.assertFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
206
- * result.assertFileContent('models/user.js', 'App.User = DS.Model.extend');
207
- *
208
- * @also
209
- *
210
- * Assert that each file in an array of file-regex pairs matches its corresponding regex
211
- * @param {Array} pairs - an array of arrays, where each subarray is a [String, RegExp] pair
212
- * @example
213
- * var arg = [
214
- * [ 'models/user.js', /App\.User = DS\.Model\.extend/ ],
215
- * [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
216
- * ]
217
- * result.assertFileContent(arg);
218
- */
219
-
220
- assertFileContent() {
221
- convertArgs(arguments).forEach((pair) => {
222
- const file = pair[0];
223
- const regex = pair[1];
224
- this.assertFile(file);
225
- const body = this._readFile(file);
226
-
227
- let match = false;
228
- if (typeof regex === 'string') {
229
- match = body.includes(regex);
230
- } else {
231
- match = regex.test(body);
232
- }
233
-
234
- assert(match, `${file} did not match '${regex}'. Contained:\n\n${body}`);
235
- });
236
- }
237
-
238
- /**
239
- * Assert that a file's content is the same as the given string
240
- * @param {String} file - path to a file
241
- * @param {String} expectedContent - the expected content of the file
242
- * @example
243
- * result.assertEqualsFileContent(
244
- * 'data.js',
245
- * 'const greeting = "Hello";\nexport default { greeting }'
246
- * );
247
- *
248
- * @also
249
- *
250
- * Assert that each file in an array of file-string pairs equals its corresponding string
251
- * @param {Array} pairs - an array of arrays, where each subarray is a [String, String] pair
252
- * @example
253
- * result.assertEqualsFileContent([
254
- * ['data.js', 'const greeting = "Hello";\nexport default { greeting }'],
255
- * ['user.js', 'export default {\n name: 'Coleman',\n age: 0\n}']
256
- * ]);
257
- */
258
-
259
- assertEqualsFileContent() {
260
- convertArgs(arguments).forEach((pair) => {
261
- const file = pair[0];
262
- const expectedContent = pair[1];
263
- this.assertFile(file);
264
- this.assertTextEqual(this._readFile(file), expectedContent);
265
- });
266
- }
267
-
268
- /**
269
- * Assert that a file's content does not match a regex / string
270
- * @param {String} file - path to a file
271
- * @param {Regex|String} reg - regex / string that will be used to search the file
272
- * @example
273
- * result.assertNoFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
274
- * result.assertNoFileContent('models/user.js', 'App.User = DS.Model.extend');
275
- *
276
- * @also
277
- *
278
- * Assert that each file in an array of file-regex pairs does not match its corresponding regex
279
- * @param {Array} pairs - an array of arrays, where each subarray is a [String, RegExp] pair
280
- * var arg = [
281
- * [ 'models/user.js', /App\.User \ DS\.Model\.extend/ ],
282
- * [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
283
- * ]
284
- * result.assertNoFileContent(arg);
285
- */
286
-
287
- assertNoFileContent() {
288
- convertArgs(arguments).forEach((pair) => {
289
- const file = pair[0];
290
- const regex = pair[1];
291
- this.assertFile(file);
292
- const body = this._readFile(file);
293
-
294
- if (typeof regex === 'string') {
295
- assert.ok(!body.includes(regex), `${file} matched '${regex}'.`);
296
- return;
297
- }
298
-
299
- assert.ok(!regex.test(body), `${file} matched '${regex}'.`);
300
- });
301
- }
302
-
303
- /**
304
- * Assert that two strings are equal after standardization of newlines
305
- * @param {String} value - a string
306
- * @param {String} expected - the expected value of the string
307
- * @example
308
- * result.assertTextEqual('I have a yellow cat', 'I have a yellow cat');
309
- */
310
-
311
- assertTextEqual(value, expected) {
312
- const eol = (string) => string.replace(/\r\n/g, '\n');
313
-
314
- assert.equal(eol(value), eol(expected));
315
- }
316
-
317
- /**
318
- * Assert an object contains the provided keys
319
- * @param {Object} obj Object that should match the given pattern
320
- * @param {Object} content An object of key/values the object should contains
321
- */
322
-
323
- assertObjectContent(object, content) {
324
- Object.keys(content).forEach((key) => {
325
- if (isObject(content[key])) {
326
- this.assertObjectContent(object[key], content[key]);
327
- return;
328
- }
329
-
330
- assert.equal(object[key], content[key]);
331
- });
332
- }
333
-
334
- /**
335
- * Assert an object does not contain the provided keys
336
- * @param {Object} obj Object that should not match the given pattern
337
- * @param {Object} content An object of key/values the object should not contain
338
- */
339
-
340
- assertNoObjectContent(object, content) {
341
- Object.keys(content).forEach((key) => {
342
- if (isObject(content[key])) {
343
- this.assertNoObjectContent(object[key], content[key]);
344
- return;
345
- }
346
-
347
- assert.notEqual(object[key], content[key]);
348
- });
349
- }
350
-
351
- /**
352
- * Assert a JSON file contains the provided keys
353
- * @param {String} filename
354
- * @param {Object} content An object of key/values the file should contains
355
- */
356
-
357
- assertJsonFileContent(filename, content) {
358
- this.assertObjectContent(this._readFile(filename, true), content);
359
- }
360
-
361
- /**
362
- * Assert a JSON file does not contain the provided keys
363
- * @param {String} filename
364
- * @param {Object} content An object of key/values the file should not contain
365
- */
366
-
367
- assertNoJsonFileContent(filename, content) {
368
- this.assertNoObjectContent(this._readFile(filename, true), content);
369
- }
370
- }
371
-
372
- module.exports = RunResult;