Skip to content

Commit

Permalink
Merge pull request #399 from mrmlnc/ISSUE-312_base_name_match
Browse files Browse the repository at this point in the history
ISSUE-312: correctly handle patterns without slashes when the `baseNameMatch` option is enabled
  • Loading branch information
mrmlnc authored May 22, 2023
2 parents ba7e86c + 4bf7ed9 commit 116ca60
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 20 deletions.
51 changes: 36 additions & 15 deletions __snapshots__/base-name-match.e2e.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
exports['Options MatchBase {"pattern":"*.md","options":{"cwd":"fixtures","baseNameMatch":true}} (sync) 1'] = [
".directory/file.md",
exports['Options MatchBase {"pattern":"file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (sync) 1'] = [
"file.md",
"first/file.md",
"first/nested/directory/file.md",
"first/nested/file.md",
"second/file.md",
"second/nested/directory/file.md",
"second/nested/file.md",
"third/library/a/book.md",
"third/library/b/book.md"
"second/nested/file.md"
]

exports['Options MatchBase {"pattern":"*.md","options":{"cwd":"fixtures","baseNameMatch":true}} (async) 1'] = [
".directory/file.md",
exports['Options MatchBase {"pattern":"file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (async) 1'] = [
"file.md",
"first/file.md",
"first/nested/directory/file.md",
"first/nested/file.md",
"second/file.md",
"second/nested/directory/file.md",
"second/nested/file.md",
"third/library/a/book.md",
"third/library/b/book.md"
"second/nested/file.md"
]

exports['Options MatchBase {"pattern":"*.md","options":{"cwd":"fixtures","baseNameMatch":true}} (stream) 1'] = [
".directory/file.md",
exports['Options MatchBase {"pattern":"file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (stream) 1'] = [
"file.md",
"first/file.md",
"first/nested/directory/file.md",
"first/nested/file.md",
"second/file.md",
"second/nested/directory/file.md",
"second/nested/file.md",
"third/library/a/book.md",
"third/library/b/book.md"
"second/nested/file.md"
]

exports['Options MatchBase {"pattern":"first/*/file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (sync) 1'] = [
"first/nested/file.md"
]

exports['Options MatchBase {"pattern":"first/*/file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (async) 1'] = [
"first/nested/file.md"
]

exports['Options MatchBase {"pattern":"first/*/file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (stream) 1'] = [
"first/nested/file.md"
]

exports['Options MatchBase {"pattern":"first/**/file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (sync) 1'] = [
"first/file.md",
"first/nested/directory/file.md",
"first/nested/file.md"
]

exports['Options MatchBase {"pattern":"first/**/file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (async) 1'] = [
"first/file.md",
"first/nested/directory/file.md",
"first/nested/file.md"
]

exports['Options MatchBase {"pattern":"first/**/file.md","options":{"cwd":"fixtures","baseNameMatch":true}} (stream) 1'] = [
"first/file.md",
"first/nested/directory/file.md",
"first/nested/file.md"
]
24 changes: 24 additions & 0 deletions src/managers/tasks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ describe('Managers → Task', () => {

assert.deepStrictEqual(actual, expected);
});

it('should do not process patterns when the `baseNameMatch` option is enabled and the pattern has a slash', () => {
const settings = new Settings({ baseNameMatch: true });

const expected = [
tests.task.builder().base('root').positive('root/*/file.txt').build()
];

const actual = manager.generate(['root/*/file.txt'], settings);

assert.deepStrictEqual(actual, expected);
});

it('should add glob star to patterns when the `baseNameMatch` option is enabled and the pattern does not have a slash', () => {
const settings = new Settings({ baseNameMatch: true });

const expected = [
tests.task.builder().base('.').positive('**/file.txt').build()
];

const actual = manager.generate(['file.txt'], settings);

assert.deepStrictEqual(actual, expected);
});
});

describe('.convertPatternsToTasks', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/managers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ function processPatterns(input: Pattern[], settings: Settings): Pattern[] {
patterns = utils.pattern.expandPatternsWithBraceExpansion(patterns);
}

/**
* If the `baseNameMatch` option is enabled, we must add globstar to patterns, so that they can be used
* at any nesting level.
*
* We do this here, because otherwise we have to complicate the filtering logic. For example, we need to change
* the pattern in the filter before creating a regular expression. There is no need to change the patterns
* in the application. Only on the input.
*/
if (settings.baseNameMatch) {
patterns = patterns.map((pattern) => pattern.includes('/') ? pattern : `**/${pattern}`);
}

/**
* This method also removes duplicate slashes that may have been in the pattern or formed as a result of expansion.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/providers/filters/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ describe('Providers → Filters → Entry', () => {
describe('options.baseNameMatch', () => {
it('should reject an entry', () => {
reject(FILE_ENTRY, {
positive: ['*'],
positive: ['file.txt'],
options: { baseNameMatch: false }
});
});

it('should accept an entry', () => {
accept(FILE_ENTRY, {
positive: ['*'],
// The task manager adds globstar for patterns without slash.
positive: ['**/file.txt'],
options: { baseNameMatch: true }
});
});
Expand Down
3 changes: 1 addition & 2 deletions src/providers/filters/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ export default class EntryFilter {
return false;
}

const filepath = this._settings.baseNameMatch ? entry.name : entry.path;
const isDirectory = entry.dirent.isDirectory();

const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(entry.path, negativeRe, isDirectory);
const isMatched = this._isMatchToPatterns(entry.path, positiveRe, isDirectory) && !this._isMatchToPatterns(entry.path, negativeRe, isDirectory);

if (this._settings.unique && isMatched) {
this._createIndexRecord(entry);
Expand Down
16 changes: 15 additions & 1 deletion src/tests/e2e/options/base-name-match.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ import * as runner from '../runner';
runner.suite('Options MatchBase', {
tests: [
{
pattern: '*.md',
pattern: 'file.md',
options: {
cwd: 'fixtures',
baseNameMatch: true
}
},
{
pattern: 'first/*/file.md',
options: {
cwd: 'fixtures',
baseNameMatch: true
}
},
{
pattern: 'first/**/file.md',
options: {
cwd: 'fixtures',
baseNameMatch: true
Expand Down

0 comments on commit 116ca60

Please sign in to comment.