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

Core: Fix issue with recursive glob with prior special chars #17252

Merged
merged 1 commit into from Jan 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/core-common/src/utils/__tests__/paths.test.ts
Expand Up @@ -12,6 +12,16 @@ describe('paths - normalizeStoryPath()', () => {
expect(normalizeStoryPath(filename)).toEqual(filename);
});

it('returns a path equal to "." unchanged', () => {
const filename = '.';
expect(normalizeStoryPath(filename)).toEqual(filename);
});

it('returns a path equal to ".." unchanged', () => {
const filename = '..';
expect(normalizeStoryPath(filename)).toEqual(filename);
});

it('adds "./" to a normalized relative path', () => {
const filename = path.join('src', 'Comp.story.js');
expect(normalizeStoryPath(filename)).toEqual(`.${path.sep}${filename}`);
Expand Down
15 changes: 15 additions & 0 deletions lib/core-common/src/utils/__tests__/to-require-context.test.ts
Expand Up @@ -238,6 +238,21 @@ const testCases = [
'../src/stories/components/Icon/Icon.mdx',
],
},
// Patterns before the **, see:
// - https://github.com/storybookjs/storybook/issues/17038
// - https://github.com/storybookjs/storybook/issues/16964
// - https://github.com/storybookjs/storybook/issues/16924
{
glob: '../{dir1,dir2}/**/*.stories.js',
recursive: true,
validPaths: [
'../dir1/Icon.stories.js',
'../dir1/nested/Icon.stories.js',
'../dir2/Icon.stories.js',
'../dir2/nested/Icon.stories.js',
],
invalidPaths: ['../dir3/Icon.stories.js', '../dir3/nested/Icon.stories.js'],
},
];

describe('toRequireContext', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/core-common/src/utils/paths.ts
Expand Up @@ -29,9 +29,9 @@ export const nodePathsToArray = (nodePath: string) =>
.filter(Boolean)
.map((p) => path.resolve('./', p));

const relativePattern = /^\.{1,2}[/\\]/;
const relativePattern = /^\.{1,2}([/\\]|$)/;
/**
* Ensures that a path starts with `./` or `../`
* Ensures that a path starts with `./` or `../`, or is entirely `.` or `..`
*/
export function normalizeStoryPath(filename: string) {
if (relativePattern.test(filename)) return filename;
Expand Down
3 changes: 2 additions & 1 deletion lib/core-common/src/utils/to-require-context.ts
Expand Up @@ -7,9 +7,10 @@ export const toRequireContext = (specifier: NormalizedStoriesSpecifier) => {
// The importPathMatcher is a `./`-prefixed matcher that includes the directory
// For `require.context()` we want the same thing, relative to directory
const match = globToRegexp(`./${files}`);

return {
path: directory,
recursive: !!files.match(/^\*{1,2}\//),
recursive: files.includes('**') || files.split('/').length > 1,
match,
};
};
Expand Down