Skip to content

Commit

Permalink
add withEnvironmentRun helper (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Sep 25, 2024
1 parent 27aa61b commit 388ba2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
private ran = false;
private errored = false;
private readonly beforePrepareCallbacks: Array<(this: this) => void | Promise<void>> = [];
private environmentRun?: (this: this, env: DefaultEnvironmentApi, gen: GeneratorType) => void;

/**
* This class provide a run context object to façade the complexity involved in setting
Expand Down Expand Up @@ -151,7 +152,8 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
}

try {
await this.env.runGenerator(this.generator as any);
const environmentRun = this.environmentRun ?? ((env, generator) => env.runGenerator(generator));
await environmentRun.call(this, this.env, this.generator);
} finally {
this.helpers.restorePrompt(this.env);
this.completed = true;
Expand Down Expand Up @@ -301,6 +303,17 @@ export class RunContextBase<GeneratorType extends BaseGenerator = DefaultGenerat
return this;
}

/**
* Customize enviroment run method.
*
* @param callback
* @return {this} run context instance
*/
withEnvironmentRun(callback: (this: this, env: DefaultEnvironmentApi, gen: GeneratorType) => void) {
this.environmentRun = callback;
return this;
}

/**
* Run lookup on the environment.
*
Expand Down
34 changes: 32 additions & 2 deletions test/run-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import process from 'node:process';
import { createRequire } from 'node:module';
import { mock } from 'node:test';
import { promisify as promisify_ } from 'node:util';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import Generator from 'yeoman-generator';
import tempDirectory from 'temp-dir';
import { RunContextBase as RunContext } from '../src/run-context.js';
import helpers from '../src/helpers.js';
import { DummyPrompt } from '../src/adapter.js';
import { BaseEnvironmentOptions } from '@yeoman/types';

/* Remove argument from promisify return */
const promisify = function_ => () => promisify_(function_)();
Expand All @@ -21,7 +22,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const tmpdir = path.join(tempDirectory, 'yeoman-run-context');

describe('RunContext', () => {
const environmentOptions = { foo: 'bar' };
let environmentOptions: BaseEnvironmentOptions | undefined;
let context: RunContext;
let execSpy;
let Dummy;
Expand Down Expand Up @@ -61,6 +62,9 @@ describe('RunContext', () => {
);

describe('constructor', () => {
beforeAll(() => {
environmentOptions = { foo: 'bar' };
});
it(
'forwards envOptions to the environment',
promisify(done => {
Expand Down Expand Up @@ -856,6 +860,32 @@ describe('RunContext', () => {
);
});

describe('#withEnvironmentRun()', () => {
it('calls runGenerator by default', async () => {
let mockedRunGenerator: ReturnType<typeof mock.fn>;
await context
.withEnvironment(environment => {
mockedRunGenerator = mock.method(environment, 'runGenerator');
})
.toPromise();
expect(mockedRunGenerator!.mock.callCount()).toBe(1);
});

it('calls custom environment run method', async () => {
let mockedRunGenerator: ReturnType<typeof mock.fn>;
const mockedEnvironmentRun = mock.fn();
await context
.withEnvironment(environment => {
mockedRunGenerator = mock.method(environment, 'runGenerator');
})
.withEnvironmentRun(mockedEnvironmentRun)
.toPromise();

expect(mockedRunGenerator!.mock.callCount()).toBe(0);
expect(mockedEnvironmentRun!.mock.callCount()).toBe(1);
});
});

describe('#withLocalConfig()', () => {
it(
'provides config to the generator',
Expand Down

0 comments on commit 388ba2d

Please sign in to comment.