Skip to content

Commit

Permalink
InvalidLinkBear: Deprecate timeout setting
Browse files Browse the repository at this point in the history
Fixes #1244
  • Loading branch information
meetmangukiya committed Jan 21, 2017
1 parent 7f3c97e commit e20e679
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
20 changes: 15 additions & 5 deletions bears/general/InvalidLinkBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ def find_links_in_file(file, network_timeout, link_ignore_regex,
link = InvalidLinkBear.parse_pip_vcs_url(link)
host = urlparse(link).netloc
code = InvalidLinkBear.get_status_code(
link, network_timeout.get(
host, InvalidLinkBear.DEFAULT_TIMEOUT))
link,
network_timeout.get(host)
if host in network_timeout
else network_timeout.get('*')
if '*' in network_timeout
else InvalidLinkBear.DEFAULT_TIMEOUT)
yield line_number + 1, link, code

@deprecate_settings(link_ignore_regex='ignore_regex')
@deprecate_settings(link_ignore_regex='ignore_regex',
network_timeout=('timeout', lambda t: {'*': t}))
def run(self, filename, file,
network_timeout: typed_dict(str, int, DEFAULT_TIMEOUT)=dict(),
link_ignore_regex: str='([.\/]example\.com|\{|\$)',
Expand All @@ -117,12 +122,17 @@ def run(self, filename, file,
:param network_timeout: A dict mapping URLs and timeout to be
used for that URL. All the URLs that have
the same host as that of URLs provided
will be passed that timeout.
will be passed that timeout. It can also
contain a wildcard timeout entry with key
'*'. The timeout of all the websites not
in the dict will be the value of the key
'*'.
:param link_ignore_regex: A regex for urls to ignore.
:param link_ignore_list: Comma separated url globs to ignore
:param follow_redirects: Set to true to autocorrect redirects.
"""
network_timeout = {urlparse(url).netloc: timeout
network_timeout = {urlparse(url).netloc
if not url == '*' else '*': timeout
for url, timeout in network_timeout.items()}

for line_number, link, code in InvalidLinkBear.find_links_in_file(
Expand Down
26 changes: 25 additions & 1 deletion tests/general/InvalidLinkBearTest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import logging
from queue import Queue
import requests
import requests_mock
Expand Down Expand Up @@ -234,7 +235,8 @@ def test_links_to_ignore(self):
def test_variable_timeouts(self):
nt = {
'https://google.com/timeout/test/2/3/4/5/something': 10,
'https://facebook.com/timeout': 2
'https://facebook.com/timeout': 2,
'*': 25
}

file_contents = """
Expand All @@ -255,10 +257,32 @@ def response(status_code, *args, **kwargs):
self.assertEqual([x.message
for x in list(uut.run('file', file_contents,
network_timeout=nt))], [])

with self.assertLogs(logging.getLogger()) as log:
self.assertEqual([x.message
for x in list(uut.run('file', file_contents,
timeout=20))], [])
self.assertEqual(log.output,
['WARNING:root:The setting `timeout` is '
'deprecated. Please use `network_timeout` '
'instead.'])

self.assertEqual([x.message
for x in list(uut.run('file',
['https://gitmate.io']))],
[])
mock.assert_has_calls([
unittest.mock.call('https://facebook.com/', timeout=2,
allow_redirects=False),
unittest.mock.call('https://google.com/',
timeout=10, allow_redirects=False),
unittest.mock.call('https://coala.io/som/thingg/page/123',
timeout=25, allow_redirects=False),
unittest.mock.call('https://facebook.com/', timeout=20,
allow_redirects=False),
unittest.mock.call('https://google.com/',
timeout=20, allow_redirects=False),
unittest.mock.call('https://coala.io/som/thingg/page/123',
timeout=20, allow_redirects=False),
unittest.mock.call('https://gitmate.io',
timeout=15, allow_redirects=False)])

0 comments on commit e20e679

Please sign in to comment.