Skip to content

Commit

Permalink
feat(tasks): Allow to use inverted negative patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed May 1, 2018
1 parent 22a0850 commit d05df0f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
22 changes: 19 additions & 3 deletions src/managers/tasks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,18 @@ describe('Managers → Task', () => {
});

describe('.getPositivePatterns', () => {
it('should return only positive patterns', () => {
it('should return positive patterns from patterns', () => {
const expected = ['*'];

const actual = manager.getPositivePatterns(['*', '!*.md']);
const actual = manager.getPositivePatterns(['*', '!*.md'], ['*.txt']);

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

it('should return inverted negative patterns from ignore option', () => {
const expected = ['**/*'];

const actual = manager.getPositivePatterns(['*', '!*.md'], ['!**/*', '*.txt']);

assert.deepEqual(actual, expected);
});
Expand All @@ -139,7 +147,15 @@ describe('Managers → Task', () => {
});

it('should return negative patterns as positive with patterns from ignore option', () => {
const expected = ['*.md', '*.txt', '*.json'];
const expected = ['*.md', '*.txt'];

const actual = manager.getNegativePatternsAsPositive(['*', '!*.md'], ['*.txt']);

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

it('should return negative patterns as positive without inverted negative patterns from ignore option', () => {
const expected = ['*.md', '*.txt'];

const actual = manager.getNegativePatternsAsPositive(['*', '!*.md'], ['*.txt', '!*.json']);

Expand Down
16 changes: 11 additions & 5 deletions src/managers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function generate(patterns: Pattern[], options: IOptions): ITask[] {
const unixPatterns = patterns.map(patternUtils.unixifyPattern);
const unixIgnore = options.ignore.map(patternUtils.unixifyPattern);

const positivePatterns = getPositivePatterns(unixPatterns);
const positivePatterns = getPositivePatterns(unixPatterns, unixIgnore);
const negativePatterns = getNegativePatternsAsPositive(unixPatterns, unixIgnore);

const staticPatterns = positivePatterns.filter(patternUtils.isStaticPattern);
Expand Down Expand Up @@ -49,17 +49,23 @@ export function convertPatternsToTasks(positive: Pattern[], negative: Pattern[],
}

/**
* Return only positive patterns.
* Return only positive or only inverted negative patterns.
*/
export function getPositivePatterns(patterns: Pattern[]): Pattern[] {
export function getPositivePatterns(patterns: Pattern[], ignore: Pattern[]): Pattern[] {
const inversedIgnore = patternUtils.getNegativePatterns(ignore);
if (inversedIgnore.length !== 0) {
return inversedIgnore.map(patternUtils.convertToPositivePattern);
}

return patternUtils.getPositivePatterns(patterns);
}

/**
* Return only negative patterns.
* Return only negative patterns without inverted negative patterns.
*/
export function getNegativePatternsAsPositive(patterns: Pattern[], ignore: Pattern[]): Pattern[] {
const negative = patternUtils.getNegativePatterns(patterns).concat(ignore);
const nonInversedIgnore = patternUtils.getPositivePatterns(ignore);
const negative = patternUtils.getNegativePatterns(patterns).concat(nonInversedIgnore);
const positive = negative.map(patternUtils.convertToPositivePattern);

return positive;
Expand Down
27 changes: 25 additions & 2 deletions src/tests/smoke/ignore.smoke.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import * as smoke from './smoke';

smoke.suite('Smoke → Ignore', [
{
pattern: 'fixtures/**/*',
globOptions: { ignore: ['*.md'] },
fgOptions: { ignore: ['*.md'] }
},
{
pattern: 'fixtures/**/*',
globOptions: { ignore: ['**/*.md'] },
fgOptions: { ignore: ['**/*.md'] }
}
]);

smoke.suite('Smoke → Ignore (inverted)', [
{
pattern: 'fixtures/**/*',
globOptions: { ignore: ['!fixtures/*.md'] },
fgOptions: { ignore: ['!fixtures/*.md'] }
},
{
pattern: 'fixtures/**/*',
globOptions: { ignore: ['**/*.md'] },
fgOptions: { ignore: ['!**/*.md'] }
globOptions: { ignore: ['!fixtures/**/*.md'] },
fgOptions: { ignore: ['!fixtures/**/*.md'] }
},
{
pattern: 'fixtures/**/*',
globOptions: { ignore: ['!fixtures/**/nested/**/*'] },
fgOptions: { ignore: ['!fixtures/**/nested/**/*'] }
},
{
pattern: 'fixtures/**/*',
globOptions: { ignore: ['!fixtures/**/nested/**/*', '**/*.md'] },
fgOptions: { ignore: ['!fixtures/**/nested/**/*', '**/*.md'] }
}
]);

0 comments on commit d05df0f

Please sign in to comment.