Skip to content

Commit b88b80d

Browse files
QuLogicap--
andauthored
Fix GitHub tests without a network connection (#509)
* Fix GitHub tests without a network connection The wrapper is supposed to catch connection errors and turn them into `xfail`, but `test_rmdir_not_empty` _also_ catches connection errors because they are `OSError` (which is what it expect, thus the test still fails everything. Instead, change the `xfail_on_github_rate_limit` function to check for networking first, and mark everything `xfail` directly. Also, remove the `_xfail_on_rate_limit_errors` fixture, which is redundant to the above and doesn't work fully because it only applies to the class's direct test methods and not its inherited test methods. * tests: improve stability of github connection / ratelimit error checking --------- Co-authored-by: Andreas Poehlmann <[email protected]>
1 parent a8b133d commit b88b80d

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

upath/tests/implementations/test_github.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,42 @@
1818
)
1919

2020

21-
def xfail_on_github_rate_limit(func):
22-
"""
23-
Method decorator to mark test as xfail when GitHub rate limit is exceeded.
24-
"""
21+
def xfail_on_github_connection_error(func):
22+
"""Method decorator to xfail tests on GitHub rate limit or connection errors."""
2523

2624
@functools.wraps(func)
27-
def wrapped_method(self, *args, **kwargs):
28-
import requests
29-
25+
def wrapper(self, *args, **kwargs):
3026
try:
3127
return func(self, *args, **kwargs)
32-
except AssertionError as e:
33-
if "nodename nor servname provided, or not known" in str(e):
34-
pytest.xfail(reason="No internet connection")
35-
raise
36-
except requests.exceptions.ConnectionError:
37-
pytest.xfail(reason="No internet connection")
3828
except Exception as e:
39-
if "rate limit exceeded" in str(e):
29+
str_e = str(e)
30+
if "rate limit exceeded" in str_e or "too many requests for url" in str_e:
4031
pytest.xfail("GitHub API rate limit exceeded")
32+
elif (
33+
"nodename nor servname provided, or not known" in str_e
34+
or "Network is unreachable" in str_e
35+
):
36+
pytest.xfail("No internet connection")
4137
else:
4238
raise
4339

44-
return wrapped_method
40+
return wrapper
4541

4642

47-
def wrap_github_rate_limit_check(cls):
48-
"""
49-
Class decorator to wrap all test methods with the
50-
xfail_on_github_rate_limit decorator.
51-
"""
52-
for attr_name in dir(cls):
53-
if attr_name.startswith("test_"):
54-
orig_method = getattr(cls, attr_name)
55-
setattr(cls, attr_name, xfail_on_github_rate_limit(orig_method))
56-
return cls
43+
def wrap_all_tests(decorator):
44+
"""Class decorator factory to wrap all test methods with a given decorator."""
45+
46+
def class_decorator(cls):
47+
for attr_name in dir(cls):
48+
if attr_name.startswith("test_"):
49+
orig_method = getattr(cls, attr_name)
50+
setattr(cls, attr_name, decorator(orig_method))
51+
return cls
52+
53+
return class_decorator
5754

5855

59-
@wrap_github_rate_limit_check
56+
@wrap_all_tests(xfail_on_github_connection_error)
6057
class TestUPathGitHubPath(BaseTests):
6158
"""
6259
Unit-tests for the GitHubPath implementation of UPath.
@@ -70,16 +67,6 @@ def path(self):
7067
path = "github://ap--:universal_pathlib@test_data/data"
7168
self.path = UPath(path)
7269

73-
@pytest.fixture(autouse=True)
74-
def _xfail_on_rate_limit_errors(self):
75-
try:
76-
yield
77-
except Exception as e:
78-
if "rate limit exceeded" in str(e):
79-
pytest.xfail("GitHub API rate limit exceeded")
80-
else:
81-
raise
82-
8370
def test_is_GitHubPath(self):
8471
"""
8572
Test that the path is a GitHubPath instance.

0 commit comments

Comments
 (0)