Debian Codegraph @

test/conftest.py

raw
1 ######################################################################
2 #
3 # File: test/conftest.py
4 #
5 # Copyright 2023 Backblaze Inc. All Rights Reserved.
6 #
7 # License https://www.backblaze.com/using_b2_code.html
8 #
9 ######################################################################
10
11 import sys
12
13 import pytest
14
15 from b2._internal._utils.python_compat import removeprefix
16
17
18 @pytest.hookimpl
19 def pytest_configure(config):
20 config.addinivalue_line(
21 "markers",
22 "apiver(from_ver, to_ver): run tests only on certain apiver versions",
23 )
24
25
26 @pytest.fixture(scope='session')
27 def apiver(request):
28 """Get apiver as a v-prefixed string, e.g. "v2"."""
29 return removeprefix(request.config.getoption('--cli', '').lstrip('_'), "b2") or None
30
31
32 @pytest.fixture(scope='session')
33 def apiver_int(apiver) -> int:
34 return int(apiver[1:]) if apiver else -1
35
36
37 @pytest.fixture(autouse=True)
38 def run_on_apiver_handler(request, apiver_int):
39 """
40 Auto-fixture that allows skipping tests based on the CLI apiver versions.
41
42 Usage:
43 @pytest.mark.apiver(1, 3)
44 def test_foo():
45 # Test is run only for versions 1 and 3
46 ...
47
48 @pytest.mark.apiver(from_ver=2, to_ver=5)
49 def test_bar():
50 # Test is run only for versions 2, 3, 4 and 5
51 ...
52
53 Note that it requires the `cli_int_version` fixture to be defined.
54 Both unit tests and integration tests handle it a little bit different, thus
55 two different fixtures are provided.
56 """
57 node = request.node.get_closest_marker('apiver')
58 if not node:
59 return
60
61 if not node.args and not node.kwargs:
62 return
63
64 assert apiver_int >= 0, 'apiver_int fixture is not defined'
65
66 if node.args:
67 if apiver_int in node.args:
68 # Run the test.
69 return
70
71 if node.kwargs:
72 from_ver = node.kwargs.get('from_ver', 0)
73 to_ver = node.kwargs.get('to_ver', sys.maxsize)
74
75 if from_ver <= apiver_int <= to_ver:
76 # Run the test.
77 return
78
79 pytest.skip('Not supported on this apiver version')