yeoman-test 5.1.0 → 6.3.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/README.md CHANGED
@@ -22,7 +22,7 @@ $ npm install --save-dev yeoman-generator@xxx yeoman-environment@xxx
22
22
 
23
23
  Usage:
24
24
 
25
- ```
25
+ ```js
26
26
  describe('generator test', () => {
27
27
  describe('test', () => {
28
28
  let runResult;
@@ -66,7 +66,10 @@ describe('generator test', () => {
66
66
  });
67
67
  ```
68
68
 
69
- [See our api documentation](https://yeoman.github.io/yeoman-test)
69
+ [See our api documentation](https://yeoman.github.io/yeoman-test) for latest yeoman-test release.
70
+
71
+ [See our api documentation](https://yeoman.github.io/yeoman-test/5.0.1) for yeoman-test 5.0.1. Use 5.x for yeoman-environment 2.x support.
72
+
70
73
  [See our api documentation](https://yeoman.github.io/yeoman-test/2.x) for yeoman-test 2.x.
71
74
 
72
75
  [See our documentation](http://yeoman.io/authoring/testing.html) for yeoman-test 2.x.
package/lib/index.js CHANGED
@@ -1,7 +1,13 @@
1
1
  /* eslint-disable max-params */
2
2
 
3
3
  'use strict';
4
- const fs = require('fs');
4
+ const {
5
+ writeFile,
6
+ mkdirSync,
7
+ existsSync,
8
+ rmdirSync,
9
+ rmSync = rmdirSync
10
+ } = require('fs');
5
11
  const _ = require('lodash');
6
12
  const path = require('path');
7
13
  const sinon = require('sinon');
@@ -59,7 +65,7 @@ YeomanTest.prototype.gruntfile = function (options, done) {
59
65
 
60
66
  const out = ['module.exports = function (grunt) {', config, '};'];
61
67
 
62
- fs.writeFile('Gruntfile.js', out.join('\n'), done);
68
+ writeFile('Gruntfile.js', out.join('\n'), done);
63
69
  };
64
70
 
65
71
  /**
@@ -85,11 +91,11 @@ YeomanTest.prototype.testDirectory = function (dir, cb) {
85
91
  process.chdir('/');
86
92
 
87
93
  try {
88
- if (fs.existsSync(dir)) {
89
- fs.rmdirSync(dir, {recursive: true});
94
+ if (existsSync(dir)) {
95
+ rmSync(dir, {recursive: true});
90
96
  }
91
97
 
92
- fs.mkdirSync(dir, {recursive: true});
98
+ mkdirSync(dir, {recursive: true});
93
99
  process.chdir(dir);
94
100
  cb();
95
101
  } catch (error) {
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
  const crypto = require('crypto');
3
- const fs = require('fs');
3
+ const {existsSync, rmdirSync, rmSync = rmdirSync} = require('fs');
4
4
  const path = require('path');
5
- const os = require('os');
6
5
  const assert = require('assert');
7
6
  const _ = require('lodash/string');
8
7
  const util = require('util');
9
8
  const {EventEmitter} = require('events');
9
+ const tempDirectory = require('temp-dir');
10
+
10
11
  const RunResult = require('./run-result');
11
12
 
12
13
  /**
@@ -137,16 +138,25 @@ RunContext.prototype.build = function (callback = () => {}) {
137
138
  this.env.registerStub(this.Generator, namespace, this.settings.resolved);
138
139
  }
139
140
 
140
- this.generator = this.env.create(namespace, {
141
- arguments: this.args,
142
- options: this.options
141
+ this._generatorPromise = Promise.resolve(
142
+ this.env.create(namespace, {
143
+ arguments: this.args,
144
+ options: this.options
145
+ })
146
+ );
147
+
148
+ this._generatorPromise.then((generator) => {
149
+ this.generator = generator;
143
150
  });
144
151
 
145
152
  this.helpers.mockPrompt(this.env, this.answers, this.promptOptions);
146
153
 
147
154
  if (this.localConfig) {
148
155
  // Only mock local config when withLocalConfig was called
149
- this.helpers.mockLocalConfig(this.generator, this.localConfig);
156
+ this._generatorPromise = this._generatorPromise.then((generator) => {
157
+ this.helpers.mockLocalConfig(generator, this.localConfig);
158
+ return generator;
159
+ });
150
160
  }
151
161
 
152
162
  callback(this);
@@ -162,7 +172,8 @@ RunContext.prototype._run = function () {
162
172
  this.buildAsync = true;
163
173
  if (this.build() === false) return false;
164
174
 
165
- this.emit('ready', this.generator);
175
+ this._generatorPromise.then((generator) => this.emit('ready', generator));
176
+
166
177
  this.run()
167
178
  .catch((error) => {
168
179
  if (
@@ -187,8 +198,8 @@ RunContext.prototype._run = function () {
187
198
  };
188
199
 
189
200
  /**
190
- * Run the generator on the environment and returns the promise.
191
- * @return {Promise} Promise
201
+ * Run the generator on the environment and promises a RunResult instance.
202
+ * @return {Promise<RunResult>} Promise a RunResult instance.
192
203
  */
193
204
  RunContext.prototype.run = function () {
194
205
  if (!this.settings.runEnvironment && this.buildAsync === undefined) {
@@ -199,17 +210,20 @@ RunContext.prototype.run = function () {
199
210
  this.build();
200
211
  }
201
212
 
202
- return this.env
203
- .runGenerator(this.generator)
204
- .then(() => new RunResult(this._createRunResultOptions()))
205
- .finally(() => {
206
- this.helpers.restorePrompt(this.env);
207
- });
213
+ return this._generatorPromise.then((generator) =>
214
+ this.env
215
+ .runGenerator(generator)
216
+ .then(() => new RunResult(this._createRunResultOptions()))
217
+ .finally(() => {
218
+ this.helpers.restorePrompt(this.env);
219
+ })
220
+ );
208
221
  };
209
222
 
210
223
  RunContext.prototype._createRunResultOptions = function () {
211
224
  return {
212
225
  env: this.env,
226
+ generator: this.generator,
213
227
  memFs: this.env.sharedFs,
214
228
  settings: {
215
229
  ...this.settings
@@ -344,7 +358,7 @@ RunContext.prototype.cd = function (dirPath) {
344
358
  */
345
359
  RunContext.prototype.inTmpDir = function (cb) {
346
360
  return this.inDir(
347
- path.join(os.tmpdir(), crypto.randomBytes(20).toString('hex')),
361
+ path.join(tempDirectory, crypto.randomBytes(20).toString('hex')),
348
362
  cb
349
363
  );
350
364
  };
@@ -408,8 +422,8 @@ RunContext.prototype.cleanTestDirectory = function (force = false) {
408
422
  throw new Error('Cleanup test dir called with false tmpdir option.');
409
423
  }
410
424
 
411
- if (this.targetDirectory && fs.existsSync(this.targetDirectory)) {
412
- fs.rmdirSync(this.targetDirectory, {recursive: true});
425
+ if (this.targetDirectory && existsSync(this.targetDirectory)) {
426
+ rmSync(this.targetDirectory, {recursive: true});
413
427
  }
414
428
  };
415
429
 
package/lib/run-result.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
  const assert = require('assert');
3
- const fs = require('fs');
3
+ const {existsSync, readFileSync, rmdirSync, rmSync = rmdirSync} = require('fs');
4
4
  const MemFsEditor = require('mem-fs-editor');
5
5
  const path = require('path');
6
6
 
@@ -18,9 +18,14 @@ function convertArgs(args) {
18
18
  return Array.isArray(arg) ? arg : [arg];
19
19
  }
20
20
 
21
- module.exports = class RunResult {
21
+ /**
22
+ * This class provides utilities for testing generated content.
23
+ */
24
+
25
+ class RunResult {
22
26
  constructor(options = {cwd: process.cwd()}) {
23
27
  this.env = options.env;
28
+ this.generator = options.generator;
24
29
  this.cwd = options.cwd;
25
30
  this.oldCwd = options.oldCwd;
26
31
  this.memFs = options.memFs;
@@ -49,6 +54,28 @@ module.exports = class RunResult {
49
54
  );
50
55
  }
51
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
+
52
79
  /**
53
80
  * Prints files names and contents from mem-fs
54
81
  * @param {...string} files - Files to print or empty for entire mem-fs
@@ -97,7 +124,7 @@ module.exports = class RunResult {
97
124
  */
98
125
  cleanup() {
99
126
  process.chdir(this.oldCwd);
100
- fs.rmdirSync(this.cwd, {recursive: true});
127
+ rmSync(this.cwd, {recursive: true});
101
128
  return this;
102
129
  }
103
130
 
@@ -115,7 +142,7 @@ module.exports = class RunResult {
115
142
  if (this.fs) {
116
143
  file = this.fs.read(filename, 'utf8');
117
144
  } else {
118
- file = fs.readFileSync(filename, 'utf8');
145
+ file = readFileSync(filename, 'utf8');
119
146
  }
120
147
 
121
148
  return json ? JSON.parse(file) : file;
@@ -127,7 +154,7 @@ module.exports = class RunResult {
127
154
  return this.fs.exists(filename);
128
155
  }
129
156
 
130
- return fs.existsSync(filename);
157
+ return existsSync(filename);
131
158
  }
132
159
 
133
160
  /**
@@ -340,4 +367,6 @@ module.exports = class RunResult {
340
367
  assertNoJsonFileContent(filename, content) {
341
368
  this.assertNoObjectContent(this._readFile(filename, true), content);
342
369
  }
343
- };
370
+ }
371
+
372
+ module.exports = RunResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeoman-test",
3
- "version": "5.1.0",
3
+ "version": "6.3.0",
4
4
  "description": "Test utilities for Yeoman generators",
5
5
  "homepage": "http://yeoman.io/authoring/testing.html",
6
6
  "author": "The Yeoman Team",
@@ -23,25 +23,27 @@
23
23
  "devDependencies": {
24
24
  "coveralls": "^3.1.0",
25
25
  "husky": "^4.2.5",
26
- "jsdoc": "^3.6.6",
26
+ "jsdoc": "^3.6.10",
27
27
  "lint-staged": "^10.2.11",
28
- "mem-fs": "^1.2.0",
29
- "mocha": "^8.0.1",
28
+ "mem-fs": "^2.1.0",
29
+ "mocha": "^9.2.0",
30
30
  "nyc": "^15.1.0",
31
31
  "prettier": "^2.2.1",
32
32
  "tui-jsdoc-template": "^1.2.2",
33
33
  "xo": "^0.32.1",
34
- "yeoman-environment": "^3.0.0",
34
+ "yeoman-environment": "^3.3.0",
35
35
  "yeoman-generator": "^5.0.0"
36
36
  },
37
37
  "dependencies": {
38
- "inquirer": "^7.3.3",
39
- "lodash": "^4.17.19",
40
- "mem-fs-editor": "^8.0.0",
41
- "sinon": "^9.0.2"
38
+ "inquirer": "^8.0.0",
39
+ "lodash": "^4.17.21",
40
+ "mem-fs-editor": "^9.0.0",
41
+ "sinon": "^10.0.0",
42
+ "temp-dir": "^2.0.0"
42
43
  },
43
44
  "peerDependencies": {
44
- "yeoman-environment": "^3.0.0 || ^2.10.3",
45
+ "mem-fs": "^2.1.0",
46
+ "yeoman-environment": "^3.3.0",
45
47
  "yeoman-generator": "*"
46
48
  },
47
49
  "scripts": {