Skip to content

Commit

Permalink
fix mem-fs reuse when creating a new context from result (#167)
Browse files Browse the repository at this point in the history
- add option to don't reset mem-fs files.
- execute commit before creating the environment.
  • Loading branch information
mshima committed Feb 24, 2023
1 parent 32aa425 commit 739ed7e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
37 changes: 30 additions & 7 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type Generator from 'yeoman-generator';
import type Environment from 'yeoman-environment';
import { type LookupOptions, type Options } from 'yeoman-environment';
import MemFsEditor from 'mem-fs-editor';
import MemFsEditorState from 'mem-fs-editor/lib/state.js';
import MemFs from 'mem-fs';

import RunResult, { type RunResultOptions } from './run-result.js';
import defaultHelpers, { type GeneratorConstructor, type Dependency, type YeomanTest } from './helpers.js';
Expand All @@ -35,6 +37,8 @@ export type RunContextSettings = {

autoCleanup?: boolean;

memFs?: MemFs.Store;

/**
* File path to the generator (only used if Generator is a constructor)
*/
Expand All @@ -58,12 +62,14 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
completed = false;
targetDirectory?: string;
editor!: MemFsEditor.Editor;
memFs: MemFs.Store;

protected environmentPromise?: PromiseRunResult<GeneratorType>;

private args: string[] = [];
private options: any = {};
private answers?: any;
private keepFsState?: boolean;

private readonly onGeneratorCallbacks: Array<(this: this, generator: GeneratorType) => any> = [];

Expand Down Expand Up @@ -120,6 +126,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
}

this.helpers = helpers;
this.memFs = settings?.memFs ?? MemFs.create();
}

/**
Expand Down Expand Up @@ -421,6 +428,14 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
return this.onGenerator(generator => generator.config.defaults(localConfig));
}

/**
* Don't reset mem-fs state cleared to aggregate snapshots from multiple runs.
*/
withKeepFsState(): this {
this.keepFsState = true;
return this;
}

/**
* Add files to mem-fs.
* Files will be resolved relative to targetDir.
Expand Down Expand Up @@ -548,20 +563,28 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
throw new Error('targetDirectory is required');
}

const testEnv = await this.helpers.createTestEnv(this.envOptions.createEnv, {
cwd: this.settings.forwardCwd ? this.targetDirectory : undefined,
...this.options,
...this.envOptions,
});
this.env = this.envCB ? (await this.envCB(testEnv)) ?? testEnv : testEnv;
if (!this.keepFsState) {
this.memFs.each(file => {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete file[MemFsEditorState.STATE_CLEARED];
});
}

this.editor = MemFsEditor.create(this.env.sharedFs);
this.editor = MemFsEditor.create(this.memFs);

for (const onTargetDirectory of this.onTargetDirectoryCallbacks) {
// eslint-disable-next-line no-await-in-loop
await onTargetDirectory.call(this, this.targetDirectory);
}

const testEnv = await this.helpers.createTestEnv(this.envOptions.createEnv, {
cwd: this.settings.forwardCwd ? this.targetDirectory : undefined,
sharedFs: this.memFs,
...this.options,
...this.envOptions,
});
this.env = this.envCB ? (await this.envCB(testEnv)) ?? testEnv : testEnv;

for (const onEnvironmentCallback of this.onEnvironmentCallbacks) {
// eslint-disable-next-line no-await-in-loop
await onEnvironmentCallback.call(this, this.env);
Expand Down
3 changes: 2 additions & 1 deletion src/run-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ export default class RunResult<GeneratorType extends Generator = Generator> {
...this.options.settings,
cwd: this.cwd,
oldCwd: this.oldCwd,
memFs: this.memFs,
...settings,
autoCleanup: false,
},
{ ...this.options.envOptions, memFs: this.memFs, ...envOptions },
{ ...this.options.envOptions, ...envOptions },
);
}

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

it('forwards the mem-fs to the environment', () => {
return ctx.run().then(() => {
assert.equal(ctx.memFs, ctx.env.sharedFs);
});
});

it('passes newErrorHandler to the environment', () => {
return ctx.run().then(() => {
assert(ctx.env.options.newErrorHandler);
Expand Down
4 changes: 2 additions & 2 deletions test/run-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ describe('run-result', () => {
it('forwards oldCwd from the original RunResult', () => {
assert.equal(runContext.oldCwd, oldCwd);
});
it('forwards memFs from the original RunResult to new envOptions', () => {
assert.equal(runContext.envOptions.memFs, memFs);
it('forwards memFs from the original RunResult to new RunContext', () => {
assert.equal(runContext.memFs, memFs);
});
it('prefers settings passed to the method', () => {
assert.equal(runContext.settings.overrided, 'newOverrided');
Expand Down

0 comments on commit 739ed7e

Please sign in to comment.