From e6d29c0dbe02c62517f6318eb7a834ee7d4bd836 Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Wed, 20 Mar 2024 14:24:16 +0100 Subject: [PATCH] rename apiver skip function --- test/conftest.py | 40 ++++++++-------- test/integration/conftest.py | 19 ++++---- test/integration/test_b2_command_line.py | 4 +- test/unit/conftest.py | 11 ++--- test/unit/test_apiver.py | 58 ++++++++++++------------ test/unit/test_console_tool.py | 4 +- 6 files changed, 69 insertions(+), 67 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 5cfb332a5..b2c10da7f 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -12,36 +12,40 @@ import pytest +from b2._internal._utils.python_compat import removeprefix + @pytest.hookimpl def pytest_configure(config): config.addinivalue_line( "markers", - "cli_version(from_version, to_version): run tests only on certain versions", + "apiver(from_ver, to_ver): run tests only on certain apiver versions", ) @pytest.fixture(scope='session') -def cli_int_version() -> int: - """ - This should never be called, only provides a placeholder for tests - not belonging to neither units nor integrations. - """ - return -1 +def apiver(request): + """Get apiver as a v-prefixed string, e.g. "v2".""" + return removeprefix(request.config.getoption('--cli', '').lstrip('_'), "b2") or None + + +@pytest.fixture(scope='session') +def apiver_int(apiver) -> int: + return int(apiver[1:]) if apiver else -1 @pytest.fixture(autouse=True) -def run_on_cli_version_handler(request, cli_int_version): +def run_on_apiver_handler(request, apiver_int): """ - Auto-fixture that allows skipping tests based on the CLI version. + Auto-fixture that allows skipping tests based on the CLI apiver versions. Usage: - @pytest.mark.cli_version(1, 3) + @pytest.mark.apiver(1, 3) def test_foo(): # Test is run only for versions 1 and 3 ... - @pytest.mark.cli_version(from_version=2, to_version=5) + @pytest.mark.apiver(from_ver=2, to_ver=5) def test_bar(): # Test is run only for versions 2, 3, 4 and 5 ... @@ -50,26 +54,26 @@ def test_bar(): Both unit tests and integration tests handle it a little bit different, thus two different fixtures are provided. """ - node = request.node.get_closest_marker('cli_version') + node = request.node.get_closest_marker('apiver') if not node: return if not node.args and not node.kwargs: return - assert cli_int_version >= 0, 'cli_int_version fixture is not defined' + assert apiver_int >= 0, 'apiver_int fixture is not defined' if node.args: - if cli_int_version in node.args: + if apiver_int in node.args: # Run the test. return if node.kwargs: - from_version = node.kwargs.get('from_version', 0) - to_version = node.kwargs.get('to_version', sys.maxsize) + from_ver = node.kwargs.get('from_ver', 0) + to_ver = node.kwargs.get('to_ver', sys.maxsize) - if from_version <= cli_int_version <= to_version: + if from_ver <= apiver_int <= to_ver: # Run the test. return - pytest.skip('Not supported on this CLI version') + pytest.skip('Not supported on this apiver version') diff --git a/test/integration/conftest.py b/test/integration/conftest.py index 5bada7f85..a734862b9 100755 --- a/test/integration/conftest.py +++ b/test/integration/conftest.py @@ -100,6 +100,11 @@ def get_raw_cli_int_version(config) -> int | None: return None +@pytest.fixture(scope='session') +def apiver(request): + return f"v{get_cli_int_version(request.config)}" + + def get_cli_int_version(config) -> int: return get_raw_cli_int_version(config) or get_int_version(LATEST_STABLE_VERSION) @@ -107,16 +112,14 @@ def get_cli_int_version(config) -> int: @pytest.hookimpl def pytest_report_header(config): cli_version = get_cli_int_version(config) - return f'b2cli version: {cli_version}' - - -@pytest.fixture(scope='session') -def cli_int_version(request) -> int: - return get_cli_int_version(request.config) + return f'b2 apiver: {cli_version}' @pytest.fixture(scope='session') def cli_version(request) -> str: + """ + Get CLI version name, i.e. b2v3, _b2v4, etc. + """ # The default stable version could be provided directly as e.g.: b2v3, but also indirectly as b2. # In case there is no direct version, we return the default binary name instead. raw_cli_version = get_raw_cli_int_version(request.config) @@ -381,8 +384,8 @@ def pytest_collection_modifyitems(items): @pytest.fixture(scope='module') -def b2_uri_args(cli_int_version): - if cli_int_version >= 4: +def b2_uri_args(apiver_int): + if apiver_int >= 4: return b2_uri_args_v4 else: return b2_uri_args_v3 diff --git a/test/integration/test_b2_command_line.py b/test/integration/test_b2_command_line.py index 2e21e01e6..5f3289ea2 100755 --- a/test/integration/test_b2_command_line.py +++ b/test/integration/test_b2_command_line.py @@ -208,7 +208,7 @@ def test_basic(b2_tool, bucket_name, sample_file, tmp_path, b2_uri_args): ) # \r? is for Windows, as $ doesn't match \r\n -@pytest.mark.cli_version(from_version=4) +@pytest.mark.apiver(from_ver=4) def test_ls_b2id(b2_tool, uploaded_sample_file): b2_tool.should_succeed( ['ls', f"b2id://{uploaded_sample_file['fileId']}"], @@ -216,7 +216,7 @@ def test_ls_b2id(b2_tool, uploaded_sample_file): ) -@pytest.mark.cli_version(from_version=4) +@pytest.mark.apiver(from_ver=4) def test_rm_b2id(b2_tool, bucket_name, uploaded_sample_file): # remove the file by id b2_tool.should_succeed(['rm', f"b2id://{uploaded_sample_file['fileId']}"]) diff --git a/test/unit/conftest.py b/test/unit/conftest.py index 6722ad0d5..53cb5004e 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -35,7 +35,7 @@ def pytest_addoption(parser): @pytest.hookimpl def pytest_report_header(config): int_version = get_int_version(config.getoption('--cli')) - return f'b2cli version: {int_version}' + return f"b2 apiver: {int_version}" @pytest.fixture(scope='session') @@ -43,11 +43,6 @@ def cli_version(request) -> str: return request.config.getoption('--cli') -@pytest.fixture(scope='session') -def cli_int_version(cli_version) -> int: - return get_int_version(cli_version) - - @pytest.fixture(scope='session') def console_tool_class(cli_version): # Ensures import of the correct library to handle all the tests. @@ -165,8 +160,8 @@ def uploaded_file(b2_cli, bucket_info, local_file): @pytest.fixture(scope='class') -def b2_uri_args(cli_int_version, request): - if cli_int_version >= 4: +def b2_uri_args(apiver_int, request): + if apiver_int >= 4: fn = b2_uri_args_v4 else: fn = b2_uri_args_v3 diff --git a/test/unit/test_apiver.py b/test/unit/test_apiver.py index bf1176772..abd2ca687 100644 --- a/test/unit/test_apiver.py +++ b/test/unit/test_apiver.py @@ -13,55 +13,55 @@ @pytest.fixture -def inject_cli_int_version(request, cli_int_version): - request.cls.cli_int_version = cli_int_version +def inject_apiver_int(request, apiver_int): + request.cls.apiver_int = apiver_int -@pytest.mark.usefixtures('inject_cli_int_version') +@pytest.mark.usefixtures('inject_apiver_int') class UnitTestClass(unittest.TestCase): - cli_int_version: int + apiver_int: int - @pytest.mark.cli_version(to_version=3) + @pytest.mark.apiver(to_ver=3) def test_passes_below_and_on_v3(self): - assert self.cli_int_version <= 3 + assert self.apiver_int <= 3 - @pytest.mark.cli_version(from_version=4) + @pytest.mark.apiver(from_ver=4) def test_passes_above_and_on_v4(self): - assert self.cli_int_version >= 4 + assert self.apiver_int >= 4 - @pytest.mark.cli_version(3) + @pytest.mark.apiver(3) def test_passes_only_on_v3(self): - assert self.cli_int_version == 3 + assert self.apiver_int == 3 - @pytest.mark.cli_version(4) + @pytest.mark.apiver(4) def test_passes_only_on_v4(self): - assert self.cli_int_version == 4 + assert self.apiver_int == 4 - @pytest.mark.cli_version(3, 4) + @pytest.mark.apiver(3, 4) def test_passes_on_both_v3_and_v4(self): - assert self.cli_int_version in {3, 4} + assert self.apiver_int in {3, 4} -@pytest.mark.cli_version(to_version=3) -def test_passes_below_and_on_v3(cli_int_version): - assert cli_int_version <= 3 +@pytest.mark.apiver(to_ver=3) +def test_passes_below_and_on_v3(apiver_int): + assert apiver_int <= 3 -@pytest.mark.cli_version(from_version=4) -def test_passes_above_and_on_v4(cli_int_version): - assert cli_int_version >= 4 +@pytest.mark.apiver(from_ver=4) +def test_passes_above_and_on_v4(apiver_int): + assert apiver_int >= 4 -@pytest.mark.cli_version(3) -def test_passes_only_on_v3(cli_int_version): - assert cli_int_version == 3 +@pytest.mark.apiver(3) +def test_passes_only_on_v3(apiver_int): + assert apiver_int == 3 -@pytest.mark.cli_version(4) -def test_passes_only_on_v4(cli_int_version): - assert cli_int_version == 4 +@pytest.mark.apiver(4) +def test_passes_only_on_v4(apiver_int): + assert apiver_int == 4 -@pytest.mark.cli_version(3, 4) -def test_passes_on_both_v3_and_v4(cli_int_version): - assert cli_int_version in {3, 4} +@pytest.mark.apiver(3, 4) +def test_passes_on_both_v3_and_v4(apiver_int): + assert apiver_int in {3, 4} diff --git a/test/unit/test_console_tool.py b/test/unit/test_console_tool.py index b6921c908..9c8b755ff 100644 --- a/test/unit/test_console_tool.py +++ b/test/unit/test_console_tool.py @@ -2423,7 +2423,7 @@ def test_passing_api_parameters(self): ) assert parallel_strategy.max_streams == params['--max-download-streams-per-file'] - @pytest.mark.cli_version(from_version=4) + @pytest.mark.apiver(from_ver=4) def test_ls_b2id(self): self._authorize_account() self._create_my_bucket() @@ -2918,7 +2918,7 @@ def test_rm_skipping_over_errors(self): ''' self._run_command(['ls', '--recursive', *self.b2_uri_args('my-bucket')], expected_stdout) - @pytest.mark.cli_version(from_version=4) + @pytest.mark.apiver(from_ver=4) def test_rm_b2id(self): # Create a file file_version = self.bucket.upload(UploadSourceBytes(b''), 'new-file.txt')