Tweak the plugin directory search (#5819)

* Update load-plugins.js

* Update load-plugins.js

* Adds a test

* Fixes lint
master
Maël Nison 2019-02-05 12:47:55 +00:00 committed by Jed Fox
parent ca43aad88a
commit 9793154833
4 changed files with 37 additions and 6 deletions

View File

@ -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, {

View File

@ -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: []
});
});

View File

View File

@ -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;