yeoman-test
Version:
Test utilities for Yeoman generators
44 lines (43 loc) • 1.34 kB
JavaScript
import process from 'node:process';
import { onExit } from 'signal-exit';
class TestContext {
beforeCwd;
autoRestore = true;
autoCleanup;
runResult;
runContext;
startNewContext(runContext, autoCleanup = true) {
if (this.beforeCwd !== process.cwd()) {
if (this.autoCleanup) {
this.runContext?.cleanupTemporaryDir();
}
else if (this.autoRestore) {
this.runContext?.restore();
}
}
if (this.beforeCwd && this.beforeCwd !== process.cwd()) {
console.log('Test failed to restore context', this.beforeCwd, process.cwd());
}
this.autoCleanup = autoCleanup;
this.beforeCwd = runContext ? process.cwd() : undefined;
this.runContext = runContext;
this.runResult = undefined;
}
}
const testContext = new TestContext();
onExit(() => {
testContext.startNewContext();
});
export default testContext;
const handler2 = {
get(_target, property, receiver) {
if (testContext.runResult === undefined) {
throw new Error('Last result is missing.');
}
return Reflect.get(testContext.runResult, property, receiver);
},
};
/**
* Provides a proxy for last executed context result.
*/
export const result = new Proxy({}, handler2);