Skip to content

Commit

Permalink
fix: folder pattern (#36)
Browse files Browse the repository at this point in the history
Paths computed from node's path modules often do not end in with a slash,
this replacement fails if there is no slash... which it was not meant to.
(we already know the path points to a directory...
  • Loading branch information
jsg2021 authored Nov 7, 2020
1 parent 19b3699 commit e79741e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function parseFoldersToGlobs(patterns, extensions) {
/* istanbul ignore else */
if (stats.isDirectory()) {
return pattern.replace(
/[/\\]+?$/u,
/[/\\]*?$/u,
`/**/*.${prefix + extensionsGlob + postfix}`
);
}
Expand Down
50 changes: 50 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { parseFoldersToGlobs } from '../src/utils';

jest.mock('fs', () => {
return {
statSync(pattern) {
return {
isDirectory() {
return pattern.indexOf('/path/') === 0;
},
};
},
};
});

test('parseFoldersToGlobs should return globs for folders', () => {
const withoutSlash = '/path/to/code';
const withSlash = `${withoutSlash}/`;

expect(parseFoldersToGlobs(withoutSlash, 'js')).toMatchInlineSnapshot(`
Array [
"/path/to/code/**/*.js",
]
`);
expect(parseFoldersToGlobs(withSlash, 'js')).toMatchInlineSnapshot(`
Array [
"/path/to/code/**/*.js",
]
`);

expect(
parseFoldersToGlobs(
[withoutSlash, withSlash, '/some/file.js'],
['js', 'cjs', 'mjs']
)
).toMatchInlineSnapshot(`
Array [
"/path/to/code/**/*.{js,cjs,mjs}",
"/path/to/code/**/*.{js,cjs,mjs}",
"/some/file.js",
]
`);
});

test('parseFoldersToGlobs should return unmodified globs for globs (ignoring extensions)', () => {
expect(parseFoldersToGlobs('**.notjs', 'js')).toMatchInlineSnapshot(`
Array [
"**.notjs",
]
`);
});

0 comments on commit e79741e

Please sign in to comment.