forked from manubot/manubot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
73 lines (55 loc) · 2.39 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pytest
markers = {
"integration": "marks integration tests that cover large portions of the codebase and their interactions",
"appveyor": "marks tests where execution on AppVeyor provides additional coverage beyond GitHub Actions",
"travis": "marks tests where execution on Travis CI provides additional coverage beyond GitHub Actions",
"pandoc_version_sensitive": "marks tests that require a specific version of pandoc to pass",
}
def pytest_configure(config):
# register additional markers
for k, v in markers.items():
config.addinivalue_line("markers", f"{k}: {v}")
def pytest_addoption(parser):
parser.addoption(
"--use-requests-cache",
action="store_true",
help="Use requests_cache for caching web API calls",
)
parser.addoption(
"--requests-cache-hours",
action="store",
default="12",
help="Set time to live for requests cache. To be used with --use-requests-cache.",
)
@pytest.fixture(scope="session", autouse=True)
def use_requests_cache(request):
"""Use requests_cache for caching web API calls.
Enabled with --use-requests-cache flag:
pytest --use-requests-cache
pytest --use-requests-cache --requests-cache-hours=3
pytest --use-requests-cache --requests-cache-hours=8760
Notes:
- By default the cache persists for 12 hours. Default can be overrriden
with --requests-cache-hours option.
- Cache does not work for invocations of manubot commands as subprocess.
Cache file location: ./.cache/requests-cache.sqlite
"""
if request.config.getoption("--use-requests-cache"):
hours = int(request.config.getoption("--requests-cache-hours"))
start_caching(hours)
def cache_path():
from pathlib import Path
folder = Path(__file__).resolve().parent / ".cache"
if not folder.exists():
folder.mkdir()
# requests_cache itself always adds extension .sqlite
return str(folder / "requests-cache")
def start_caching(hours: int):
import logging
from datetime import timedelta
from requests_cache import install_cache
ttl = timedelta(hours=hours)
install_cache(cache_name=cache_path(), expire_after=ttl)
logging.info("Using requests_cache to speed up local tests execution.")
logging.info(f"Cache location is {cache_path() + '.sqlite'}")
logging.info(f"Cache expiration time is set to {ttl}")