Skip to content

Commit

Permalink
Api improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 22, 2020
1 parent eeb3dd8 commit 2d51f81
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 33 deletions.
10 changes: 6 additions & 4 deletions lib/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ RunContext.prototype.async = function () {
* Build the generator and the environment.
* @return {RunContext|false} this
*/
RunContext.prototype.build = function (async = false) {
RunContext.prototype.build = function (callback = () => {}) {
if (!this.inDirSet && this.settings.tmpdir !== false) {
this.inTmpDir();
}

if (this._asyncHolds !== 0 || this.ran || this.completed) {
if (async) {
if (this.buildAsync) {
return false;
}

Expand Down Expand Up @@ -156,6 +156,7 @@ RunContext.prototype.build = function (async = false) {
helpers.mockLocalConfig(this.generator, this.localConfig);
}

callback(this);
return this;
};

Expand All @@ -165,7 +166,8 @@ RunContext.prototype.build = function (async = false) {
*/

RunContext.prototype._run = function () {
if (this.build(true) === false) return false;
this.buildAsync = true;
if (this.build() === false) return false;

const endCallback = callbackWrapper((envOrGenerator) => {
helpers.restorePrompt(envOrGenerator);
Expand Down Expand Up @@ -209,7 +211,7 @@ RunContext.prototype.run = function () {
() =>
new RunResult({
env: this.env,
fs: this.env.fs,
memFs: this.env.sharedFs,
settings: {
...this.settings
},
Expand Down
43 changes: 37 additions & 6 deletions lib/run-result.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const MemFsEditor = require('mem-fs-editor');
const path = require('path');

const helpers = require('.');
Expand All @@ -22,17 +23,19 @@ module.exports = class RunResult {
this.env = options.env;
this.cwd = options.cwd;
this.oldCwd = options.oldCwd;
this.fs = options.fs;
this.memFs = options.memFs;
this.fs = this.memFs && MemFsEditor.create(this.memFs);
this.options = options;
if (this.fs && !this.cwd) {
if (this.memFs && !this.cwd) {
throw new Error('CWD option is required for mem-fs tests');
}
}

/**
* Create another RunContext reusing the settings.
* See helpers.create api
*/
createContext(GeneratorOrNamespace, settings, envOptions) {
create(GeneratorOrNamespace, settings, envOptions) {
return helpers.create(
GeneratorOrNamespace,
{
Expand All @@ -41,13 +44,18 @@ module.exports = class RunResult {
oldCwd: this.oldCwd,
...settings
},
{...this.options.envOptions, fs: this.fs, ...envOptions}
{...this.options.envOptions, memFs: this.memFs, ...envOptions}
);
}

/**
* Prints files names and contents from mem-fs
* @param {...string} files - Files to print or empty for entire mem-fs
* @returns {RunResult} this
*/
dumpFiles(...files) {
if (files.length === 0) {
this.env.sharedFs.each((file) => {
this.memFs.each((file) => {
console.log(file.path);
if (file.contents) {
console.log(file.contents.toString('utf8'));
Expand All @@ -62,13 +70,36 @@ module.exports = class RunResult {
return this;
}

/**
* Prints every file from mem-fs
* @returns {RunResult} this
*/
dumpFilenames() {
this.env.sharedFs.each((file) => {
this.memFs.each((file) => {
console.log(file.path);
});
return this;
}

/**
* Reverts to old cwd.
* @returns {RunResult} this
*/
restore() {
process.chdir(this.oldCwd);
return this;
}

/**
* Deletes the test directory recursively.
* @returns {RunResult} this
*/
cleanup() {
process.chdir(this.oldCwd);
fs.rmdirSync(this.cwd, {recursive: true});
return this;
}

_fileName(filename) {
if (path.isAbsolute(filename)) {
return filename;
Expand Down
27 changes: 19 additions & 8 deletions test/run-result-assertions.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
/* eslint-disable max-nested-callbacks */
'use strict';
const path = require('path');
const assert = require('assert');
const memFsEditor = require('mem-fs-editor');
const memFs = require('mem-fs');
const MemFs = require('mem-fs');

const RunResult = require('../lib/run-result');

describe('run-result-assertions', () => {
const sharedFs = memFs.create();
const fs = memFsEditor.create(sharedFs);
const memFs = MemFs.create();

[
{description: 'using memory fs', fs},
{description: 'using node fs'}
{
description: 'using memory fs',
options: {memFs},
verify: (runResult) => {
assert(runResult.fs);
}
},
{
description: 'using node fs',
verify: (runResult) => {
assert(!runResult.fs);
}
}
].forEach((testFs) => {
const yoAssert = new RunResult({
fs: testFs.fs,
...testFs.options,
cwd: path.join(__dirname, 'fixtures/assert')
});

describe(testFs.description, () => {
it('fs is correct', () => {
testFs.verify(yoAssert);
});
describe('.assertFile()', () => {
it('accept a file that exists', () => {
assert.doesNotThrow(yoAssert.assertFile.bind(yoAssert, 'testFile'));
Expand Down
52 changes: 37 additions & 15 deletions test/run-result.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-nested-callbacks */
'use strict';
const assert = require('assert');
const fs = require('fs');
const MemFs = require('mem-fs');
const MemFsEditor = require('mem-fs-editor');
const path = require('path');
Expand All @@ -18,19 +19,19 @@ describe('run-result', () => {
});
describe('with fs option', () => {
it('throws error without cwd', () => {
assert.throws(() => new RunResult({fs: {}}));
assert.throws(() => new RunResult({memFs: {}}));
});
});
describe('with fs and cwd options', () => {
const fs = {};
const memFs = {};
const cwd = {};
const options = {fs, cwd};
const options = {memFs, cwd};
let runResult;
before(() => {
runResult = new RunResult(options);
});
it('loads fs option', () => {
assert.equal(runResult.fs, fs);
it('loads memFs option', () => {
assert.equal(runResult.memFs, memFs);
});
it('loads cwd option', () => {
assert.equal(runResult.cwd, cwd);
Expand Down Expand Up @@ -61,9 +62,9 @@ describe('run-result', () => {
const memFs = MemFs.create();
const memFsEditor = MemFsEditor.create(memFs);
runResult = new RunResult({
memFs,
fs: memFsEditor,
cwd: process.cwd(),
env: {sharedFs: memFs}
cwd: process.cwd()
});
consoleMock = sinon.stub(console, 'log');
runResult.fs.write(path.resolve('test.txt'), 'test content');
Expand Down Expand Up @@ -96,9 +97,9 @@ describe('run-result', () => {
const memFs = MemFs.create();
const memFsEditor = MemFsEditor.create(memFs);
runResult = new RunResult({
memFs,
fs: memFsEditor,
cwd: process.cwd(),
env: {sharedFs: memFs}
cwd: process.cwd()
});
consoleMock = sinon.stub(console, 'log');
runResult.fs.write(path.resolve('test.txt'), 'test content');
Expand All @@ -114,7 +115,28 @@ describe('run-result', () => {
assert.equal(consoleMock.getCall(1).args[0], path.resolve('test2.txt'));
});
});
describe('#createContext', () => {
describe('#cleanup', () => {
let cwd;
let runResult;
beforeEach(() => {
cwd = path.join(process.cwd(), 'fixtures', 'tmp');
if (!fs.existsSync(cwd)) {
fs.mkdirSync(cwd);
}

runResult = new RunResult({
cwd,
oldCwd: path.join(process.cwd(), 'fixtures')
});
});
afterEach(() => {});
it('removes cwd', () => {
assert.ok(fs.existsSync(runResult.cwd));
runResult.cleanup();
assert.ok(!fs.existsSync(runResult.cwd));
});
});
describe('#create', () => {
const newSettings = {newOnly: 'foo', overrided: 'newOverrided'};
const newEnvOptions = {newOnlyEnv: 'bar', overridedEnv: 'newOverridedEnv'};
const originalEnvOptions = {
Expand All @@ -125,19 +147,19 @@ describe('run-result', () => {
originalOnly: 'originalOnly',
overrided: 'originalOverrided'
};
const fs = {};
const memFs = {};
let cwd;
const oldCwd = {};
let runContext;
before(() => {
cwd = process.cwd();
runContext = new RunResult({
fs,
memFs,
cwd,
oldCwd,
envOptions: originalEnvOptions,
settings: originalSetting
}).createContext('foo', newSettings, newEnvOptions);
}).create('foo', newSettings, newEnvOptions);
});
it('returns a RunContext instance', () => {
assert.ok(runContext instanceof RunContext);
Expand All @@ -160,8 +182,8 @@ describe('run-result', () => {
it('forwards oldCwd from the original RunResult', () => {
assert.equal(runContext.oldCwd, oldCwd);
});
it('forwards fs from the original RunResult to new envOptions', () => {
assert.equal(runContext.envOptions.fs, fs);
it('forwards memFs from the original RunResult to new envOptions', () => {
assert.equal(runContext.envOptions.memFs, memFs);
});
it('prefers settings passed to the method', () => {
assert.equal(runContext.settings.overrided, 'newOverrided');
Expand Down

0 comments on commit 2d51f81

Please sign in to comment.