Skip to content

Commit 09c0e2e

Browse files
authored
add prepareTemporaryDir for tests unrelated to generators (#168)
1 parent 739ed7e commit 09c0e2e

3 files changed

Lines changed: 64 additions & 31 deletions

File tree

‎src/helpers.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import type { Options, createEnv } from 'yeoman-environment';
1111
import type { SinonSpiedInstance } from 'sinon';
1212

1313
import { DummyPrompt, type DummyPromptOptions, TestAdapter } from './adapter.js';
14-
import RunContext from './run-context.js';
14+
import RunContext, { BasicRunContext, type RunContextSettings } from './run-context.js';
1515
import testContext from './test-context.js';
16-
import type { RunContextSettings } from './run-context.js';
1716

1817
const { cloneDeep } = _;
1918

@@ -340,6 +339,19 @@ export class YeomanTest {
340339
) {
341340
return this.run<GeneratorType>(GeneratorOrNamespace, settings, envOptions);
342341
}
342+
343+
/**
344+
* Prepare temporary dir without generator support.
345+
* Generator and environment will be undefined.
346+
*/
347+
prepareTemporaryDir(settings?: RunContextSettings) {
348+
const context = new BasicRunContext(undefined, settings);
349+
if (settings?.autoCleanup !== false) {
350+
testContext.startNewContext(context);
351+
}
352+
353+
return context;
354+
}
343355
}
344356

345357
export default new YeomanTest();

‎src/run-context.ts‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
7878
private readonly onEnvironmentCallbacks: Array<(this: this, env: Environment) => any> = [];
7979

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

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

104104
constructor(
105-
generatorType: string | GeneratorConstructor<GeneratorType> | typeof Generator,
105+
generatorType?: string | GeneratorConstructor<GeneratorType> | typeof Generator,
106106
settings?: RunContextSettings,
107107
envOptions: Options = {},
108108
helpers = defaultHelpers,
@@ -530,17 +530,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
530530
return this;
531531
}
532532

533-
protected assertNotBuild() {
534-
if (this.built || this.completed) {
535-
throw new Error('The context is already built');
536-
}
537-
}
538-
539-
/**
540-
* Build the generator and the environment.
541-
* @return {RunContext|false} this
542-
*/
543-
protected async build(): Promise<void> {
533+
async prepare() {
544534
this.assertNotBuild();
545535

546536
this.built = true;
@@ -576,6 +566,20 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
576566
// eslint-disable-next-line no-await-in-loop
577567
await onTargetDirectory.call(this, this.targetDirectory);
578568
}
569+
}
570+
571+
protected assertNotBuild() {
572+
if (this.built || this.completed) {
573+
throw new Error('The context is already built');
574+
}
575+
}
576+
577+
/**
578+
* Build the generator and the environment.
579+
* @return {RunContext|false} this
580+
*/
581+
protected async build(): Promise<void> {
582+
await this.prepare();
579583

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

623+
protected _createRunResultOptions(): RunResultOptions<GeneratorType> {
624+
return {
625+
env: this.env,
626+
generator: this.generator,
627+
memFs: this.env?.sharedFs ?? this.memFs,
628+
settings: {
629+
...this.settings,
630+
},
631+
oldCwd: this.oldCwd!,
632+
cwd: this.targetDirectory!,
633+
envOptions: this.envOptions,
634+
mockedGenerators: this.mockedGenerators,
635+
helpers: this.helpers,
636+
};
637+
}
638+
619639
/**
620640
* Keeps compatibility with events
621641
*/
@@ -672,22 +692,6 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
672692
this.targetDirectory = dirPath;
673693
return this;
674694
}
675-
676-
private _createRunResultOptions(): RunResultOptions<GeneratorType> {
677-
return {
678-
env: this.env,
679-
generator: this.generator,
680-
memFs: this.env.sharedFs,
681-
settings: {
682-
...this.settings,
683-
},
684-
oldCwd: this.oldCwd!,
685-
cwd: this.targetDirectory!,
686-
envOptions: this.envOptions,
687-
mockedGenerators: this.mockedGenerators,
688-
helpers: this.helpers,
689-
};
690-
}
691695
}
692696

693697
export default class RunContext<GeneratorType extends Generator = Generator>
@@ -716,3 +720,10 @@ export default class RunContext<GeneratorType extends Generator = Generator>
716720
return `RunContext`;
717721
}
718722
}
723+
724+
export class BasicRunContext extends RunContext {
725+
async run(): PromiseRunResult<any> {
726+
await this.prepare();
727+
return new RunResult(this._createRunResultOptions());
728+
}
729+
}

‎test/helpers.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,14 @@ describe('yeoman-test', function () {
429429
assert.equal(mockedCreateEnv.getCall(0).args[1].sharedOptions.localConfigOnly, true);
430430
});
431431
});
432+
describe('.prepareTemporaryFolder', () => {
433+
it('should create a temporaryFolder', async () => {
434+
const oldCwd = process.cwd();
435+
const context = await helpers.prepareTemporaryDir();
436+
assert.notEqual(process.cwd(), oldCwd);
437+
assert.equal(context.oldCwd, oldCwd);
438+
context.cleanup();
439+
assert.equal(process.cwd(), oldCwd);
440+
});
441+
});
432442
});

0 commit comments

Comments
 (0)