Skip to content

Commit

Permalink
Handle invalid regexes when filtering code search results
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelyghost authored and kategengler committed Sep 15, 2017
1 parent 4b8c4f4 commit fb0c369
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/components/addon-source-usages.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ function filterByFilePath(usages, filterTerm) {
if (isEmpty(filterTerm)) {
return usages;
}
let filterRegex = new RegExp(filterTerm);
let filterRegex;
try {
filterRegex = new RegExp(filterTerm);
} catch(e) {
return [];
}
return usages.filter((usage) => {
return usage.filename.match(filterRegex);
});
Expand Down
7 changes: 6 additions & 1 deletion app/components/code-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ function filterByFilePath(results, filterTerm) {
}

let filteredList = [];
let filterRegex = new RegExp(filterTerm);
let filterRegex;
try {
filterRegex = new RegExp(filterTerm);
} catch(e) {
return [];
}
results.forEach((result) => {
let filteredFiles = result.files.filter((filePath) => {
return filePath.match(filterRegex);
Expand Down
48 changes: 48 additions & 0 deletions tests/acceptance/code-search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,54 @@ test('filtering works with sorting and pagination', function(assert) {
});
});

test('when file filter regex is invalid', function(assert) {
server.create('addon', { name: 'ember-try' });
server.create('addon', { name: 'ember-blanket' });
server.create('addon', { name: 'ember-foo' });

let invalidFilter = '(index';

server.get('/search/addons', () => {
return {
results: [
{
addon: 'ember-try',
count: 1,
files: ['app/controllers/index.js']
},
{
addon: 'ember-blanket',
count: 2,
files: ['app/components/blanket.js', 'app/templates/components/blanket.hbs']
},
{
addon: 'ember-foo',
count: 3,
files: ['app/controllers/index.js', 'app/controllers/index.js', 'app/services/current-foo.js']
}
]
};
});

visit('/code-search');
fillIn('#code-search-input', 'whatever');
click('.test-submit-search');

andThen(function() {
assert.equal(find('.test-addon-name').length, 3, 'shows all addons before filtering');
});

fillIn('.test-file-filter-input', invalidFilter);

andThen(function() {
assert.equal(find('.test-addon-name').length, 0, 'no addons show after filtering');
assert.contains('.test-result-info', '3 addons', 'full result count still shows');
assert.contains('.test-result-info', '6 usages', 'full usage count still shows');
assert.contains('.test-filtered-result-info', '0 addons', 'filtered result count is 0');
assert.contains('.test-filtered-result-info', '0 usages', 'filtered usage count is 0');
});
});

function searchResults(addons) {
return addons.map((addon) => {
return {
Expand Down

0 comments on commit fb0c369

Please sign in to comment.