Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Catch autoload directory errors #185

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function findPlugins(dir, pattern, registered) {
}

return plugins;
}, []));
}, []), () => []);
}

// automatically register/unregister plugins by altering the CLI's package.json within node_modules
Expand Down
15 changes: 7 additions & 8 deletions packages/cli/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mockRequire from 'mock-require';
export function pluginMocker() {
let pkgPath = path.resolve(__dirname, '../package.json');

let mock = ({ plugins, packages }) => {
let mock = ({ plugins, packages = {} }) => {
for (let [name, dep] of Object.entries(plugins)) {
if (dep) mock.pkg.dependencies[name] = true;
mock.pkg.oclif.plugins.push(name);
Expand All @@ -15,29 +15,28 @@ export function pluginMocker() {
if (name.startsWith('@percy')) dir = path.resolve(dir, '@percy');
let dirname = name.replace('@percy', '');

mock.dir[dir].push(dirname);
(mock.dir[dir] ||= []).push(dirname);
mockRequire(`${dir}/${dirname}/package.json`, plugin ? (
{ name, oclif: { bin: 'percy' } }
) : { name });
}
};

mock.dir = {};
mock.pkg = {
oclif: { plugins: [] },
dependencies: {}
};

mock.dir = {
[path.resolve(__dirname, '../node_modules/@percy')]: [],
[path.resolve(__dirname, '../node_modules')]: []
};

mockRequire(pkgPath, mock.pkg);
mockRequire('fs', {
...require('fs'),

promises: {
readdir: async dir => mock.dir[dir],
readdir: async dir => {
if (mock.dir[dir]) return mock.dir[dir];
throw new Error('ENOENT');
},
writeFile: async (path, contents) => {
if (path === pkgPath) {
mock.pkg = JSON.parse(contents);
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ describe('Plugin auto-registration', () => {
'@percy/cli-other'
]);
});

it('ignores missing node_modules directory', async () => {
// not mocking packages will not create mock directories
mock({
plugins: {
'@percy/cli-exec': true
}
});

await run();

expect(mock.pkg.oclif.plugins).toEqual([
'@percy/cli-exec'
]);
});
});