Tweak the plugin directory search (#5819)
* Update load-plugins.js * Update load-plugins.js * Adds a test * Fixes lintmaster
parent
ca43aad88a
commit
9793154833
|
@ -52,17 +52,23 @@ function loadPlugins(plugins, pluginSearchDirs) {
|
|||
pluginSearchDir
|
||||
);
|
||||
|
||||
if (!isDirectory(resolvedPluginSearchDir)) {
|
||||
throw new Error(
|
||||
`${pluginSearchDir} does not exist or is not a directory`
|
||||
);
|
||||
}
|
||||
|
||||
const nodeModulesDir = path.resolve(
|
||||
resolvedPluginSearchDir,
|
||||
"node_modules"
|
||||
);
|
||||
|
||||
// In some fringe cases (ex: files "mounted" as virtual directories), the
|
||||
// isDirectory(resolvedPluginSearchDir) check might be false even though
|
||||
// the node_modules actually exists.
|
||||
if (
|
||||
!isDirectory(nodeModulesDir) &&
|
||||
!isDirectory(resolvedPluginSearchDir)
|
||||
) {
|
||||
throw new Error(
|
||||
`${pluginSearchDir} does not exist or is not a directory`
|
||||
);
|
||||
}
|
||||
|
||||
return findPluginsInNodeModules(nodeModulesDir).map(pluginName => ({
|
||||
name: pluginName,
|
||||
requirePath: resolve.sync(pluginName, {
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
"use strict";
|
||||
|
||||
const runPrettier = require("../runPrettier");
|
||||
|
||||
describe("plugin search should not crash when prettier isn't inside a directory", () => {
|
||||
runPrettier(
|
||||
"plugins/virtualDirectory",
|
||||
["--stdin-filepath", "example.js", "--plugin-search-dir=."],
|
||||
{ input: "" }
|
||||
).test({
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
status: 0,
|
||||
write: []
|
||||
});
|
||||
});
|
|
@ -55,6 +55,15 @@ function runPrettier(dir, args, options) {
|
|||
write.push({ filename, content });
|
||||
});
|
||||
|
||||
const origStatSync = fs.statSync;
|
||||
|
||||
jest.spyOn(fs, "statSync").mockImplementation(filename => {
|
||||
if (path.basename(filename) === `virtualDirectory`) {
|
||||
return origStatSync(path.join(__dirname, __filename));
|
||||
}
|
||||
return origStatSync(filename);
|
||||
});
|
||||
|
||||
const originalCwd = process.cwd();
|
||||
const originalArgv = process.argv;
|
||||
const originalExitCode = process.exitCode;
|
||||
|
|
Loading…
Reference in New Issue