|
| 1 | +import functools |
1 | 2 | import os |
2 | 3 | import platform |
3 | 4 | import sys |
|
17 | 18 | ) |
18 | 19 |
|
19 | 20 |
|
| 21 | +def has_internet_connection(): |
| 22 | + import requests |
| 23 | + |
| 24 | + try: |
| 25 | + requests.get("http://example.com") |
| 26 | + except requests.exceptions.ConnectionError: |
| 27 | + return False |
| 28 | + else: |
| 29 | + return True |
| 30 | + |
| 31 | + |
| 32 | +def xfail_on_github_rate_limit(func): |
| 33 | + """ |
| 34 | + Method decorator to mark test as xfail when GitHub rate limit is exceeded. |
| 35 | + """ |
| 36 | + |
| 37 | + @functools.wraps(func) |
| 38 | + def wrapped_method(self, *args, **kwargs): |
| 39 | + import requests |
| 40 | + |
| 41 | + try: |
| 42 | + return func(self, *args, **kwargs) |
| 43 | + except AssertionError as e: |
| 44 | + if "nodename nor servname provided, or not known" in str(e): |
| 45 | + pytest.xfail(reason="No internet connection") |
| 46 | + raise |
| 47 | + except requests.exceptions.ConnectionError: |
| 48 | + pytest.xfail(reason="No internet connection") |
| 49 | + except Exception as e: |
| 50 | + if "rate limit exceeded" in str(e): |
| 51 | + pytest.xfail("GitHub API rate limit exceeded") |
| 52 | + else: |
| 53 | + raise |
| 54 | + |
| 55 | + return wrapped_method |
| 56 | + |
| 57 | + |
| 58 | +def wrap_github_rate_limit_check(cls): |
| 59 | + """ |
| 60 | + Class decorator to wrap all test methods with the |
| 61 | + xfail_on_github_rate_limit decorator. |
| 62 | + """ |
| 63 | + for attr_name in dir(cls): |
| 64 | + if attr_name.startswith("test_"): |
| 65 | + orig_method = getattr(cls, attr_name) |
| 66 | + setattr(cls, attr_name, xfail_on_github_rate_limit(orig_method)) |
| 67 | + return cls |
| 68 | + |
| 69 | + |
| 70 | +@wrap_github_rate_limit_check |
20 | 71 | class TestUPathGitHubPath(BaseTests): |
21 | 72 | """ |
22 | 73 | Unit-tests for the GitHubPath implementation of UPath. |
|
0 commit comments