Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function mockTool<
P extends ToolPluginRegistry = TestToolPlugins,
C extends ToolConfig = TestToolConfig
>(options?: Partial, config?: Partial, injectPlugin: boolean = true): Tool {
const tool = new Tool({
appName: 'test-boost',
// Match fixtures path
appPath: new Path(process.cwd(), 'tests/__fixtures__/app').path(),
...options,
});
// Register default plugins
if (injectPlugin) {
// @ts-ignore Ignore this for convenience
tool.registerPlugin('plugin', Plugin);
}
// Stub out standard objects
tool.args = stubArgs();
tool.config = stubToolConfig(config);
tool.package = stubPackageJson();
// Stub out methods
tool.debug = mockDebugger();
async execute(oldContext: ScriptContext, script: Script): Promise {
const context = oldContext.clone();
// Set the context so tasks inherit it
this.setContext(context);
// Update the cwd to point to the package root
if (this.options.packageRoot) {
context.cwd = new Path(this.options.packageRoot);
}
const { argv } = context;
this.debug('Executing script with args "%s"', argv.join(' '));
await script.onBeforeExecute.emit([context, argv]);
const args = parseArgs(argv, script.args());
let result = null;
try {
result = await script.execute(context, args);
// Queue and run sub-tasks
const queue = result as ExecuteQueue;
}
const { configName } = this.options;
const { module } = this.config;
this.debug('Locating configuration module root');
if (!module) {
throw new Error(this.msg('errors:moduleConfigMissing', { configName }));
}
// Allow for local development
if (module === '@local') {
this.debug('Using %s configuration module', chalk.yellow('@local'));
this.moduleRoot = new Path(process.cwd());
return this.moduleRoot;
}
// Reference a node module
const rootPath = Path.resolve(`node_modules/${module}`);
if (!rootPath.exists()) {
throw new Error(this.msg('errors:moduleMissing', { configName, module }));
}
this.debug('Found configuration module root path: %s', chalk.cyan(rootPath));
this.moduleRoot = rootPath;
return rootPath;
context.configPaths.forEach(config => {
fs.copyFileSync(config.path.path(), new Path(packageRoot, config.path.name()).path());
});
const relPath = `configs/${configName}.{js,json,json5}`;
const configPaths = glob.sync(relPath, {
absolute: true,
cwd: String(root),
onlyFiles: true,
});
this.debug.invariant(
configPaths.length === 1,
`Looking for local config file: ${relPath}`,
'Found',
'Not found',
);
if (configPaths.length === 1) {
const localPath = new Path(configPaths[0]);
this.debug('Found %s', color.filePath(localPath.name()));
return localPath;
}
if (configPaths.length > 1) {
throw new Error(this.tool.msg('errors:multipleConfigFiles', { configName }));
}
return null;
}
.map(path => new Path(path).path());