Skip to content

Commit

Permalink
Disallow multiple path filter modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanis committed Jun 16, 2020
1 parent c017d1f commit 0ec9c2c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
57 changes: 57 additions & 0 deletions __tests__/services/state.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { getMatchingFilePaths } from '../../src/services';
import { Comment, Change, ChangeType } from '../../src/models';

test('* matches everything', () => {
const comment: Comment = {
pathFilter: ['*'],
markdown: 'Woohoo!',
blocking: false
};

const changes: Change[] = [
{
file: 'app/models/foo.rb',
changeType: ChangeType.edit
}
];

const result = getMatchingFilePaths(comment, changes);

expect(result).toEqual(changes.map(c => c.file));
});

test('match recursively', () => {
const comment: Comment = {
pathFilter: ['app/**'],
Expand All @@ -20,6 +39,25 @@ test('match recursively', () => {
expect(result).toEqual(changes.map(c => c.file));
});

test('ignore case', () => {
const comment: Comment = {
pathFilter: ['app/**'],
markdown: 'Woohoo!',
blocking: false
};

const changes: Change[] = [
{
file: 'APP/MODELS/FOO.rb',
changeType: ChangeType.edit
}
];

const result = getMatchingFilePaths(comment, changes);

expect(result).toEqual(changes.map(c => c.file));
});

test('remove exclusion patterns', () => {
const comment: Comment = {
pathFilter: ['app/**', '!app/models/*.h'],
Expand Down Expand Up @@ -107,3 +145,22 @@ test('match edited files only', () => {

expect(result).toEqual(['app/models/old.rb']);
});

test('disallow multiple modifiers', () => {
const comment: Comment = {
pathFilter: ['!~app/**'],
markdown: 'Not edited files',
blocking: false
};

const changes: Change[] = [
{
file: 'app/models/old.rb',
changeType: ChangeType.edit
}
];

expect(() => getMatchingFilePaths(comment, changes)).toThrow(
'Multiple path modifiers are not supported'
);
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/services/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { getExistingComments } from '.';
import { IOptions, Minimatch } from 'minimatch';
import { Constants } from '../constants';

const pathModifiers = ['!', '+', '-', '~'];

export async function getTargetState(
octokit: github.GitHub,
allComments: Comment[],
Expand Down Expand Up @@ -91,6 +93,10 @@ export function getMatchingFilePaths(
} else {
inclusions.push(pathFilter);
}

if (pathModifiers.includes(pathFilter[1])) {
throw new Error('Multiple path modifiers are not supported');
}
}

for (const change of changes) {
Expand Down

0 comments on commit 0ec9c2c

Please sign in to comment.