Skip to content

Commit

Permalink
add askedQuestions (#218)
Browse files Browse the repository at this point in the history
* add askedQuestions

* recreate package-lock.json

* lint adjusts

* test adjusts
  • Loading branch information
mshima authored May 9, 2024
1 parent f3b7e41 commit 30127fa
Show file tree
Hide file tree
Showing 7 changed files with 3,796 additions and 3,352 deletions.
7,092 changes: 3,747 additions & 3,345 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line n/file-extension-in-import
import { TestAdapter as BaseTestAdapter, type TestAdapterOptions } from '@yeoman/adapter/testing';
import { spy as sinonSpy, stub as sinonStub } from 'sinon';

Expand All @@ -11,5 +10,6 @@ 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 }>;
17 changes: 14 additions & 3 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import assert from 'node:assert';
import { EventEmitter } from 'node:events';
import process from 'node:process';
import { camelCase, kebabCase, merge as lodashMerge, set as lodashSet } from 'lodash-es';
// eslint-disable-next-line n/file-extension-in-import
import { resetFileCommitStates } from 'mem-fs-editor/state';
import { create as createMemFs, type Store } from 'mem-fs';
import tempDirectory from 'temp-dir';
Expand All @@ -22,7 +21,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 +75,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 +664,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 +737,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
4 changes: 2 additions & 2 deletions test/fixtures/generator-simple/composing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import Generator from 'yeoman-generator';
// in several methods.

export default class SimpleGenerator extends Generator {
exec(toCompose) {
async exec(toCompose) {
console.log(toCompose);
this.composeWith(toCompose);
await this.composeWith(toCompose);
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/run-context-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ describe('RunContext running environment', function () {

describe('with composing generator', () => {
before(() => {
lookups = [{ packagePaths: [path.resolve('./fixtures/generator-simple')] }];
gen = 'simple:composing';
build = false;
});
after(() => {
lookups = [];
gen = undefined;
build = true;
});
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 30127fa

Please sign in to comment.