Skip to content

Commit

Permalink
InvalidLinkBear: Multiple links
Browse files Browse the repository at this point in the history
The InvalidLinkBear should analyze all the links
in a line instead of just analyzing the first one.

Fixes coala#1296
  • Loading branch information
satwikkansal committed Feb 1, 2017
1 parent 77fba92 commit 2ef96a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions bears/general/InvalidLinkBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ def find_links_in_file(file, network_timeout, link_ignore_regex,
(?<!\.)(?<!,) # Exclude trailing `.` or `,` from URL
""", re.VERBOSE)
for line_number, line in enumerate(file):
match = regex.search(line)
if match:
link = match.group()
for match in re.findall(regex, line):
link = match[0]
if not (link_ignore_regex.search(link) or
fnmatch(link, link_ignore_list)):
if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')):
Expand Down
23 changes: 23 additions & 0 deletions tests/general/InvalidLinkBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ def assertResult(self, valid_file=None, invalid_file=None, settings={}):
self.assertNotEqual(out, [])
self.assertNotEqual(out, None)

def assertResultCount(self, test_file, expected_num):
with requests_mock.Mocker() as m:
InvalidLinkBear.check_prerequisites = lambda *args: True
uut = InvalidLinkBear(self.section, Queue())
m.add_matcher(custom_matcher)
for line, num in zip(test_file, expected_num):
outputs = list(uut.run('testline', [line]))
self.assertEqual(num, len(outputs))

def test_run(self):
# Valid Links
valid_file = """
Expand Down Expand Up @@ -155,6 +164,20 @@ def test_redirect_threshold(self):
invalid_file=short_url_redirect,
settings={'follow_redirects': 'yeah'})

def test_multiple_results_per_line(self):

test_file = """
http://httpbin.org/status/410
http://httpbin.org/status/200
http://httpbin.org/status/404 http://httpbin.org/status/410
http://httpbin.org/status/200 http://httpbin.org/status/404
http://httpbin.org/status/200 http://httpbin.org/status/201
""".splitlines()

expected_num_results = [0, 1, 0, 2, 1, 0]

self.assertResultCount(test_file, expected_num_results)

def test_pip_vcs_url(self):
with_at = """
git+http://httpbin.org/status/200@master
Expand Down

0 comments on commit 2ef96a9

Please sign in to comment.