From fb0c369605a07283a7bd0ba3b86932d5e58cdb8b Mon Sep 17 00:00:00 2001 From: Michelle Santarsiero Date: Fri, 8 Sep 2017 07:09:31 -0400 Subject: [PATCH] Handle invalid regexes when filtering code search results --- app/components/addon-source-usages.js | 7 +++- app/components/code-search.js | 7 +++- tests/acceptance/code-search-test.js | 48 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/app/components/addon-source-usages.js b/app/components/addon-source-usages.js index cfa4aacd..721ce85d 100644 --- a/app/components/addon-source-usages.js +++ b/app/components/addon-source-usages.js @@ -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); }); diff --git a/app/components/code-search.js b/app/components/code-search.js index e0c8efeb..c8d8723d 100644 --- a/app/components/code-search.js +++ b/app/components/code-search.js @@ -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); diff --git a/tests/acceptance/code-search-test.js b/tests/acceptance/code-search-test.js index 22361c4f..f8cb97fc 100644 --- a/tests/acceptance/code-search-test.js +++ b/tests/acceptance/code-search-test.js @@ -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 {