Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename apiver skip function #264

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
Expand All @@ -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')
19 changes: 11 additions & 8 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,26 @@ 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)


@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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/integration/test_b2_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ 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']}"],
expected_pattern=f"^{uploaded_sample_file['fileName']}",
)


@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']}"])
Expand Down
11 changes: 3 additions & 8 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,14 @@ 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')
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.
Expand Down Expand Up @@ -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
Expand Down
58 changes: 29 additions & 29 deletions test/unit/test_apiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
4 changes: 2 additions & 2 deletions test/unit/test_console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down