Skip to content

Commit

Permalink
ENH: explicit decorator for without_http_proxy to be used in conjunction
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikoptic committed May 20, 2016
1 parent a0f94b3 commit 78b26f5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions datalad/downloaders/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class NoHTTPPretty(object):
from ...tests.utils import with_tempfile
from ...tests.utils import use_cassette
from ...tests.utils import skip_if
from ...tests.utils import without_http_proxy
from ...support.status import FileStatus

def test_docstring():
Expand Down Expand Up @@ -247,6 +248,7 @@ def enter_new(self):

#@skip_httpretty_on_problematic_pythons
@skip_if(not httpretty, "no httpretty")
@without_http_proxy
@httpretty.activate
@with_tempfile(mkdir=True)
@with_fake_cookies_db
Expand Down Expand Up @@ -325,6 +327,7 @@ def enter_new(self):


@skip_if(not httpretty, "no httpretty")
@without_http_proxy
@httpretty.activate
@with_tempfile(mkdir=True)
@with_fake_cookies_db(cookies={'example.com': dict(some_site_id='idsomething', expires='Tue, 15 Jan 2013 21:47:38 GMT')})
Expand Down
24 changes: 24 additions & 0 deletions datalad/tests/test_tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from .utils import use_cassette
from .utils import skip_if
from .utils import ok_file_has_content
from .utils import without_http_proxy

#
# Test with_tempfile, especially nested invocations
Expand Down Expand Up @@ -407,6 +408,29 @@ def test_serve_path_via_http():
yield _test_serve_path_via_http, test_fpath


def test_without_http_proxy():

@without_http_proxy
def check(a, kw=False):
assert_false('http_proxy' in os.environ)
assert_false('https_proxy' in os.environ)
assert_in(kw, [False, 'custom'])

check(1)

with patch.dict('os.environ', {'http_proxy': 'http://127.0.0.1:9/'}):
check(1)
check(1, "custom")
with assert_raises(AssertionError):
check(1, "wrong")

with patch.dict('os.environ', {'https_proxy': 'http://127.0.0.1:9/'}):
check(1)
with patch.dict('os.environ', {'http_proxy': 'http://127.0.0.1:9/',
'https_proxy': 'http://127.0.0.1:9/'}):
check(1)


def test_assert_re_in():
assert_re_in(".*", "")
assert_re_in(".*", ["any"])
Expand Down
18 changes: 18 additions & 0 deletions datalad/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,24 @@ def newfunc(*args, **kwargs):
return newfunc


@optional_args
def without_http_proxy(tfunc):
"""Decorator to remove http*_proxy env variables for the duration of the test
"""

@wraps(tfunc)
def newfunc(*args, **kwargs):
# Such tests don't require real network so if http_proxy settings were
# provided, we remove them from the env for the duration of this run
env = os.environ.copy()
env.pop('http_proxy', None)
env.pop('https_proxy', None)
with patch.dict('os.environ', env, clear=True):
return tfunc(*args, **kwargs)

return newfunc


@optional_args
def with_tempfile(t, content=None, **tkwargs):
"""Decorator function to provide a temporary file name and remove it at the end
Expand Down

0 comments on commit 78b26f5

Please sign in to comment.