Skip to content

Commit

Permalink
add prepareTemporaryDir for tests unrelated to generators (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 26, 2023
1 parent 739ed7e commit 09c0e2e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
16 changes: 14 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import type { Options, createEnv } from 'yeoman-environment';
import type { SinonSpiedInstance } from 'sinon';

import { DummyPrompt, type DummyPromptOptions, TestAdapter } from './adapter.js';
import RunContext from './run-context.js';
import RunContext, { BasicRunContext, type RunContextSettings } from './run-context.js';
import testContext from './test-context.js';
import type { RunContextSettings } from './run-context.js';

const { cloneDeep } = _;

Expand Down Expand Up @@ -340,6 +339,19 @@ export class YeomanTest {
) {
return this.run<GeneratorType>(GeneratorOrNamespace, settings, envOptions);
}

/**
* Prepare temporary dir without generator support.
* Generator and environment will be undefined.
*/
prepareTemporaryDir(settings?: RunContextSettings) {
const context = new BasicRunContext(undefined, settings);
if (settings?.autoCleanup !== false) {
testContext.startNewContext(context);
}

return context;
}
}

export default new YeomanTest();
Expand Down
69 changes: 40 additions & 29 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
private readonly onEnvironmentCallbacks: Array<(this: this, env: Environment) => any> = [];

private readonly inDirCallbacks: any[] = [];
private readonly Generator: string | GeneratorConstructor<GeneratorType> | typeof Generator;
private readonly Generator?: string | GeneratorConstructor<GeneratorType> | typeof Generator;
private readonly helpers: YeomanTest;
private readonly temporaryDir = path.join(tempDirectory, crypto.randomBytes(20).toString('hex'));

Expand All @@ -102,7 +102,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
*/

constructor(
generatorType: string | GeneratorConstructor<GeneratorType> | typeof Generator,
generatorType?: string | GeneratorConstructor<GeneratorType> | typeof Generator,
settings?: RunContextSettings,
envOptions: Options = {},
helpers = defaultHelpers,
Expand Down Expand Up @@ -530,17 +530,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
return this;
}

protected assertNotBuild() {
if (this.built || this.completed) {
throw new Error('The context is already built');
}
}

/**
* Build the generator and the environment.
* @return {RunContext|false} this
*/
protected async build(): Promise<void> {
async prepare() {
this.assertNotBuild();

this.built = true;
Expand Down Expand Up @@ -576,6 +566,20 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
// eslint-disable-next-line no-await-in-loop
await onTargetDirectory.call(this, this.targetDirectory);
}
}

protected assertNotBuild() {
if (this.built || this.completed) {
throw new Error('The context is already built');
}
}

/**
* Build the generator and the environment.
* @return {RunContext|false} this
*/
protected async build(): Promise<void> {
await this.prepare();

const testEnv = await this.helpers.createTestEnv(this.envOptions.createEnv, {
cwd: this.settings.forwardCwd ? this.targetDirectory : undefined,
Expand Down Expand Up @@ -616,6 +620,22 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
return this.environmentPromise ?? this.run();
}

protected _createRunResultOptions(): RunResultOptions<GeneratorType> {
return {
env: this.env,
generator: this.generator,
memFs: this.env?.sharedFs ?? this.memFs,
settings: {
...this.settings,
},
oldCwd: this.oldCwd!,
cwd: this.targetDirectory!,
envOptions: this.envOptions,
mockedGenerators: this.mockedGenerators,
helpers: this.helpers,
};
}

/**
* Keeps compatibility with events
*/
Expand Down Expand Up @@ -672,22 +692,6 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
this.targetDirectory = dirPath;
return this;
}

private _createRunResultOptions(): RunResultOptions<GeneratorType> {
return {
env: this.env,
generator: this.generator,
memFs: this.env.sharedFs,
settings: {
...this.settings,
},
oldCwd: this.oldCwd!,
cwd: this.targetDirectory!,
envOptions: this.envOptions,
mockedGenerators: this.mockedGenerators,
helpers: this.helpers,
};
}
}

export default class RunContext<GeneratorType extends Generator = Generator>
Expand Down Expand Up @@ -716,3 +720,10 @@ export default class RunContext<GeneratorType extends Generator = Generator>
return `RunContext`;
}
}

export class BasicRunContext extends RunContext {
async run(): PromiseRunResult<any> {
await this.prepare();
return new RunResult(this._createRunResultOptions());
}
}
10 changes: 10 additions & 0 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,14 @@ describe('yeoman-test', function () {
assert.equal(mockedCreateEnv.getCall(0).args[1].sharedOptions.localConfigOnly, true);
});
});
describe('.prepareTemporaryFolder', () => {
it('should create a temporaryFolder', async () => {
const oldCwd = process.cwd();
const context = await helpers.prepareTemporaryDir();
assert.notEqual(process.cwd(), oldCwd);
assert.equal(context.oldCwd, oldCwd);
context.cleanup();
assert.equal(process.cwd(), oldCwd);
});
});
});

0 comments on commit 09c0e2e

Please sign in to comment.