Skip to content

Commit

Permalink
add askedQuestions
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 9, 2024
1 parent f3b7e41 commit a29d903
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { create as createMemFsEditor, type MemFsEditorFile, type MemFsEditor } f
import type { DefaultGeneratorApi, DefaultEnvironmentApi } from '../types/type-helpers.js';
import RunResult, { type RunResultOptions } from './run-result.js';
import defaultHelpers, { type CreateEnv, type Dependency, type YeomanTest } from './helpers.js';
import { type DummyPromptOptions, type TestAdapterOptions } from './adapter.js';
import { type DummyPromptCallback, type DummyPromptOptions, type TestAdapterOptions } from './adapter.js';
import testContext from './test-context.js';

/**
Expand Down Expand Up @@ -76,6 +76,7 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
memFs: Store<MemFsEditorFile>;
spawnStub?: any;
mockedGeneratorFactory: MockedGeneratorFactory;
readonly askedQuestions: Array<{ name: string; answer: any }> = [];

protected environmentPromise?: PromiseRunResult<GeneratorType>;

Expand Down Expand Up @@ -664,13 +665,23 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
async build(): Promise<void> {
await this.prepare();

const { askedQuestions, adapterOptions } = this;
const promptCallback: DummyPromptCallback = function (this, answer: any, options) {
const { question } = options;
if (question.name) {
askedQuestions.push({ name: question.name, answer });
}

return adapterOptions?.callback ? adapterOptions.callback.call(this, answer, options) : answer;
};

const testEnv = await this.helpers.createTestEnv(this.envOptions.createEnv, {
cwd: this.settings.forwardCwd ? this.targetDirectory : undefined,
sharedFs: this.memFs,
force: true,
skipCache: true,
skipInstall: true,
adapter: this.helpers.createTestAdapter({ ...this.adapterOptions, mockedAnswers: this.answers }),
adapter: this.helpers.createTestAdapter({ ...this.adapterOptions, mockedAnswers: this.answers, callback: promptCallback }),
...this.envOptions,
} as any);
this.env = this.envCB ? (await this.envCB(testEnv)) ?? testEnv : testEnv;
Expand Down
24 changes: 24 additions & 0 deletions test/run-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,30 @@ describe('RunContext', function () {
assert.ok(promptSpy.getCall(0).thisValue instanceof DummyPrompt);
});
});

it.only('sets askedQuestions', async function () {
Dummy.prototype.askFor = function () {
return this.prompt([
{
name: 'yeoman',
type: 'input',
message: 'Hey!',
},
{
name: 'yeoman2',
type: 'input',
message: 'Hey!',
},
]);
};

await ctx.withAnswers({ yeoman: 'no please' }).toPromise();

assert.deepEqual(ctx.askedQuestions, [
{ name: 'yeoman', answer: 'no please' },
{ name: 'yeoman2', answer: undefined },
]);
});
});

describe('#withMockedGenerators()', function () {
Expand Down

0 comments on commit a29d903

Please sign in to comment.