Skip to content

Commit

Permalink
Fix test using promised generator. (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 17, 2021
1 parent 735a86c commit 46d71c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lib/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,25 @@ RunContext.prototype.build = function (callback = () => {}) {
this.env.registerStub(this.Generator, namespace, this.settings.resolved);
}

this.generator = this.env.create(namespace, {
arguments: this.args,
options: this.options
this._generatorPromise = Promise.resolve(
this.env.create(namespace, {
arguments: this.args,
options: this.options
})
);

this._generatorPromise.then((generator) => {
this.generator = generator;
});

this.helpers.mockPrompt(this.env, this.answers, this.promptOptions);

if (this.localConfig) {
// Only mock local config when withLocalConfig was called
this.helpers.mockLocalConfig(this.generator, this.localConfig);
this._generatorPromise = this._generatorPromise.then((generator) => {
this.helpers.mockLocalConfig(generator, this.localConfig);
return generator;
});
}

callback(this);
Expand All @@ -163,7 +172,8 @@ RunContext.prototype._run = function () {
this.buildAsync = true;
if (this.build() === false) return false;

this.emit('ready', this.generator);
this._generatorPromise.then((generator) => this.emit('ready', generator));

this.run()
.catch((error) => {
if (
Expand Down Expand Up @@ -200,12 +210,14 @@ RunContext.prototype.run = function () {
this.build();
}

return this.env
.runGenerator(this.generator)
.then(() => new RunResult(this._createRunResultOptions()))
.finally(() => {
this.helpers.restorePrompt(this.env);
});
return this._generatorPromise.then((generator) =>
this.env
.runGenerator(generator)
.then(() => new RunResult(this._createRunResultOptions()))
.finally(() => {
this.helpers.restorePrompt(this.env);
})
);
};

RunContext.prototype._createRunResultOptions = function () {
Expand Down
27 changes: 27 additions & 0 deletions test/run-context-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';
const assert = require('assert');
const path = require('path');
const sinon = require('sinon');

const helpers = require('../lib');
const RunContext = require('../lib/run-context');
Expand Down Expand Up @@ -93,6 +94,32 @@ describe('RunContext running environment', function () {
});
});

describe('with promised generator', () => {
before(() => {
gen = 'promised-generator';
build = false;
});
beforeEach(() => {
ctx.withEnvironment((env) => {
const FakeGenerator = helpers.createDummyGenerator();
const fake = sinon.fake.returns(
Promise.resolve(new FakeGenerator({env}))
);
sinon.replace(env, 'create', fake);
});
});
after(() => {
gen = undefined;
build = true;
});

it('runs the generator', () => {
return ctx.run().then(() => {
assert(ctx.generator.shouldRun);
});
});
});

describe('with path', () => {
before(() => {
gen = require.resolve('./fixtures/generator-simple/app');
Expand Down

0 comments on commit 46d71c2

Please sign in to comment.