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 6ee9bcb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export class TestAdapter extends BaseTestAdapter {

// eslint-disable-next-line n/file-extension-in-import
export { DummyPrompt, type DummyPromptOptions, type DummyPromptCallback, type TestAdapterOptions } from '@yeoman/adapter/testing';

export type AskedQuestions = Array<{ name: string; answer: any }>;
16 changes: 14 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 AskedQuestions, 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: AskedQuestions = [];

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 Expand Up @@ -727,6 +738,7 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
envOptions: this.envOptions,
mockedGenerators: this.mockedGenerators,
helpers: this.helpers,
askedQuestions: this.askedQuestions,
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/run-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor } f
import type { DefaultEnvironmentApi, DefaultGeneratorApi } from '../types/type-helpers.js';
import { type RunContextSettings } from './run-context.js';
import { type YeomanTest } from './helpers.js';
import { type AskedQuestions } from './adapter.js';

const isObject = object => typeof object === 'object' && object !== null && object !== undefined;

Expand Down Expand Up @@ -60,6 +61,8 @@ export type RunResultOptions<GeneratorType extends BaseGenerator> = {
settings: RunContextSettings;

helpers: YeomanTest;

askedQuestions: AskedQuestions;
};

/**
Expand All @@ -76,6 +79,7 @@ export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerat
mockedGenerators: any;
options: RunResultOptions<GeneratorType>;
spawnStub?: any;
readonly askedQuestions: AskedQuestions;

constructor(options: RunResultOptions<GeneratorType>) {
if (options.memFs && !options.cwd) {
Expand All @@ -90,6 +94,7 @@ export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerat
this.fs = this.memFs && createMemFsEditor(this.memFs);
this.mockedGenerators = options.mockedGenerators || {};
this.spawnStub = options.spawnStub;
this.askedQuestions = options.askedQuestions;
this.options = options;
}

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('sets askedQuestions', async function () {
Dummy.prototype.askFor = function () {
return this.prompt([
{
name: 'yeoman',
type: 'input',
message: 'Hey!',
},
{
name: 'yeoman2',
type: 'input',
message: 'Hey!',
},
]);
};

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

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

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

0 comments on commit 6ee9bcb

Please sign in to comment.