Skip to content

Commit

Permalink
Merge pull request #32 from mrmlnc/fix_deep_patterns
Browse files Browse the repository at this point in the history
ISSUE-15: Fix work with non-nested patterns
  • Loading branch information
mrmlnc authored Jan 14, 2018
2 parents 0221888 + 91511d7 commit fea0294
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/managers/tasks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Managers → Task', () => {
const expected: TaskGroup = {
a: {
base: 'a',
globstar: false,
patterns: ['a/*.txt', 'a/*.md'],
positive: ['a/*.txt', 'a/*.md'],
negative: []
Expand All @@ -42,6 +43,7 @@ describe('Managers → Task', () => {
const expected: TaskGroup = {
a: {
base: 'a',
globstar: false,
patterns: ['!a/*.txt', '!a/*.md'],
positive: [],
negative: ['a/*.txt', 'a/*.md']
Expand All @@ -59,6 +61,7 @@ describe('Managers → Task', () => {
const positive: TaskGroup = {
a: {
base: 'a',
globstar: true,
patterns: ['a/**/*'],
positive: ['a/**/*'],
negative: []
Expand All @@ -68,6 +71,7 @@ describe('Managers → Task', () => {
const negative: TaskGroup = {
a: {
base: 'a',
globstar: false,
patterns: ['!a/**/*.txt'],
positive: [],
negative: ['a/**/*.txt']
Expand All @@ -77,6 +81,7 @@ describe('Managers → Task', () => {
const expected: TaskGroup = {
a: {
base: 'a',
globstar: true,
patterns: ['a/**/*', '!a/**/*.txt'],
positive: ['a/**/*'],
negative: ['a/**/*.txt']
Expand All @@ -93,6 +98,7 @@ describe('Managers → Task', () => {
it('should returns tasks', () => {
const expected: ITask[] = [{
base: 'a',
globstar: true,
patterns: ['a/**/*', '!a/**/*.txt'],
positive: ['a/**/*'],
negative: ['a/**/*.txt']
Expand All @@ -118,6 +124,7 @@ describe('Managers → Task', () => {
it('should returns one global task', () => {
const expected: manager.ITask[] = [{
base: '.',
globstar: true,
patterns: ['**/*'],
positive: ['**/*'],
negative: []
Expand All @@ -132,6 +139,7 @@ describe('Managers → Task', () => {
it('should returns one global task with negative patterns', () => {
const expected: manager.ITask[] = [{
base: '.',
globstar: true,
patterns: ['**/*', '!**/*.md'],
positive: ['**/*'],
negative: ['**/*.md']
Expand All @@ -146,6 +154,7 @@ describe('Managers → Task', () => {
it('should returns one global task with negative patterns from options', () => {
const expected: manager.ITask[] = [{
base: '.',
globstar: true,
patterns: ['**/*', '!**/*.md'],
positive: ['**/*'],
negative: ['**/*.md']
Expand All @@ -162,6 +171,7 @@ describe('Managers → Task', () => {
it('should returns one task', () => {
const expected: manager.ITask[] = [{
base: 'a',
globstar: true,
patterns: ['a/**/*'],
positive: ['a/**/*'],
negative: []
Expand All @@ -176,6 +186,7 @@ describe('Managers → Task', () => {
it('should returns one task with negative patterns', () => {
const expected: manager.ITask[] = [{
base: 'a',
globstar: true,
patterns: ['a/**/*', '!a/*.md'],
positive: ['a/**/*'],
negative: ['a/*.md']
Expand All @@ -190,6 +201,7 @@ describe('Managers → Task', () => {
it('should returns one task without unused negative patterns', () => {
const expected: manager.ITask[] = [{
base: 'a',
globstar: true,
patterns: ['a/**/*'],
positive: ['a/**/*'],
negative: []
Expand All @@ -204,6 +216,7 @@ describe('Managers → Task', () => {
it('should returns one task with negative patterns from options', () => {
const expected: manager.ITask[] = [{
base: 'a',
globstar: true,
patterns: ['a/**/*', '!a/*.md'],
positive: ['a/**/*'],
negative: ['a/*.md']
Expand All @@ -221,12 +234,14 @@ describe('Managers → Task', () => {
const expected: manager.ITask[] = [
{
base: 'a',
globstar: false,
patterns: ['a/*', '!a/*.md'],
positive: ['a/*'],
negative: ['a/*.md']
},
{
base: 'b',
globstar: false,
patterns: ['b/*'],
positive: ['b/*'],
negative: []
Expand All @@ -243,12 +258,14 @@ describe('Managers → Task', () => {
const expected: manager.ITask[] = [
{
base: 'a',
globstar: false,
patterns: ['a/*', '!a/*.md', '!**/*.txt'],
positive: ['a/*'],
negative: ['a/*.md', '**/*.txt']
},
{
base: 'b',
globstar: false,
patterns: ['b/*', '!**/*.txt'],
positive: ['b/*'],
negative: ['**/*.txt']
Expand All @@ -265,12 +282,14 @@ describe('Managers → Task', () => {
const expected: manager.ITask[] = [
{
base: 'a',
globstar: false,
patterns: ['a/*', '!a/*.md', '!**/*.txt'],
positive: ['a/*'],
negative: ['a/*.md', '**/*.txt']
},
{
base: 'b',
globstar: false,
patterns: ['b/*', '!**/*.txt'],
positive: ['b/*'],
negative: ['**/*.txt']
Expand Down
4 changes: 4 additions & 0 deletions src/managers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IOptions } from './options';

export interface ITask {
base: string;
globstar: boolean;
patterns: Pattern[];
positive: Pattern[];
negative: Pattern[];
Expand Down Expand Up @@ -39,6 +40,7 @@ export function makePositiveTaskGroup(positive: PatternsGroup): TaskGroup {

collection[base] = {
base,
globstar: positivePatterns.some(patternUtils.hasGlobStar),
patterns: positivePatterns,
positive: positivePatterns,
negative: []
Expand All @@ -57,6 +59,7 @@ export function makeNegativeTaskGroup(negative: PatternsGroup): TaskGroup {

collection[base] = {
base,
globstar: false,
patterns: negativePatterns.map(patternUtils.convertToNegativePattern),
positive: [],
negative: negativePatterns
Expand Down Expand Up @@ -124,6 +127,7 @@ export function generate(patterns: Pattern[], options: IOptions): ITask[] {
if ('.' in positiveGroup) {
const task: ITask = {
base: '.',
globstar: positive.some(patternUtils.hasGlobStar),
patterns: ([] as Pattern[]).concat(positive, negative.map(patternUtils.convertToNegativePattern)),
positive,
negative
Expand Down
1 change: 1 addition & 0 deletions src/providers/reader-async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Providers → ReaderAsync', () => {
describe('.read', () => {
const task: ITask = {
base: 'fixtures',
globstar: true,
patterns: ['**/*'],
positive: ['**/*'],
negative: []
Expand Down
1 change: 1 addition & 0 deletions src/providers/reader-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('Providers → ReaderStream', () => {
describe('.read', () => {
const task: ITask = {
base: 'fixtures',
globstar: true,
patterns: ['**/*'],
positive: ['**/*'],
negative: []
Expand Down
1 change: 1 addition & 0 deletions src/providers/reader-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Providers → ReaderSync', () => {
describe('.read', () => {
const task: ITask = {
base: 'fixtures',
globstar: true,
patterns: ['**/*'],
positive: ['**/*'],
negative: []
Expand Down
44 changes: 34 additions & 10 deletions src/providers/reader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('Providers → Reader', () => {

const actual = reader.getReaderOptions({
base: '.',
globstar: true,
patterns: ['**/*'],
positive: ['**/*'],
negative: []
Expand All @@ -102,6 +103,7 @@ describe('Providers → Reader', () => {

const actual = reader.getReaderOptions({
base: 'fixtures',
globstar: true,
patterns: ['**/*'],
positive: ['**/*'],
negative: []
Expand Down Expand Up @@ -392,7 +394,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(!actual);
});
Expand All @@ -406,7 +408,7 @@ describe('Providers → Reader', () => {
isDirectory: () => true
});

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(!actual);
});
Expand All @@ -418,7 +420,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, true /** isSymbolicLink */);

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(actual);
});
Expand All @@ -428,7 +430,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, true /** isSymbolicLink */);

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(!actual);
});
Expand All @@ -440,7 +442,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(true /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(actual);
});
Expand All @@ -450,7 +452,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(true /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(!actual);
});
Expand All @@ -462,7 +464,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, []);
const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(actual);
});
Expand All @@ -472,7 +474,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, ['**/pony/**']);
const actual = reader.deep(entry, ['**/pony/**'], true /** globstar */);

assert.ok(actual);
});
Expand All @@ -482,7 +484,7 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, ['**/directory/**']);
const actual = reader.deep(entry, ['**/directory/**'], true /** globstar */);

assert.ok(actual);
});
Expand All @@ -492,7 +494,29 @@ describe('Providers → Reader', () => {

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, ['**/directory']);
const actual = reader.deep(entry, ['**/directory'], true /** globstar */);

assert.ok(!actual);
});
});

describe('Filter by «globstar» parameter', () => {
it('should return true by globstar parameter', () => {
const reader = getReader();

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, [], true /** globstar */);

assert.ok(actual);
});

it('should return false by globstar parameter', () => {
const reader = getReader();

const entry = getDirectoryEntry(false /** dot */, false /** isSymbolicLink */);

const actual = reader.deep(entry, [], false /** globstar */);

assert.ok(!actual);
});
Expand Down
10 changes: 7 additions & 3 deletions src/providers/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default abstract class Reader {
return {
basePath: task.base === '.' ? '' : task.base,
filter: (entry) => this.filter(entry, task.patterns, task.negative),
deep: (entry) => this.deep(entry, task.negative),
deep: (entry) => this.deep(entry, task.negative, task.globstar),
sep: '/'
};
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export default abstract class Reader {
/**
* Returns true if directory must be read.
*/
public deep(entry: IEntry, negative: Pattern[]): boolean {
public deep(entry: IEntry, negative: Pattern[], globstar: boolean): boolean {
if (!this.options.deep) {
return false;
}
Expand All @@ -115,7 +115,11 @@ export default abstract class Reader {
return false;
}

return !micromatch.any(entry.path, negative, this.micromatchOptions);
if (micromatch.any(entry.path, negative, this.micromatchOptions)) {
return false;
}

return globstar;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/utils/pattern.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,18 @@ describe('Utils → Pattern', () => {
assert.equal(actual, expected);
});
});

describe('.hasGlobStar', () => {
it('should returns true', () => {
const actual = util.hasGlobStar('**/*.js');

assert.ok(actual);
});

it('should returns false', () => {
const actual = util.hasGlobStar('*.js');

assert.ok(!actual);
});
});
});
7 changes: 7 additions & 0 deletions src/utils/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ export function getPositivePatterns(patterns: Pattern[]): Pattern[] {
export function getBaseDirectory(pattern: Pattern): string {
return globParent(pattern);
}

/**
* Return true if provided pattern has globstar.
*/
export function hasGlobStar(pattern: Pattern): boolean {
return pattern.indexOf('**') !== -1;
}

0 comments on commit fea0294

Please sign in to comment.