diff --git a/b2sdk/_internal/testing/fixtures/account_info.py b/b2sdk/_internal/testing/fixtures/account_info.py new file mode 100644 index 00000000..901d28a5 --- /dev/null +++ b/b2sdk/_internal/testing/fixtures/account_info.py @@ -0,0 +1,34 @@ +###################################################################### +# +# File: b2sdk/_internal/testing/fixtures/account_info.py +# +# Copyright 2025 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### + +import pytest + +from b2sdk._internal.account_info.sqlite_account_info import ( + B2_ACCOUNT_INFO_ENV_VAR, + XDG_CONFIG_HOME_ENV_VAR, +) + + +@pytest.fixture(scope='session') +def change_account_info_dir(monkeysession, tmp_path_factory): + """ + For the entire test session: + 1) temporarily remove B2_APPLICATION_KEY and B2_APPLICATION_KEY_ID from the environment + 2) create a temporary directory for storing account info database + 3) set B2_ACCOUNT_INFO_ENV_VAR to point to the temporary account info file + """ + + monkeysession.delenv('B2_APPLICATION_KEY_ID', raising=False) + monkeysession.delenv('B2_APPLICATION_KEY', raising=False) + + temp_dir = tmp_path_factory.mktemp('b2_config') + + monkeysession.setenv(B2_ACCOUNT_INFO_ENV_VAR, str(temp_dir / ('.b2_account_info'))) + monkeysession.setenv(XDG_CONFIG_HOME_ENV_VAR, str(temp_dir)) diff --git a/b2sdk/_internal/testing/fixtures/common.py b/b2sdk/_internal/testing/fixtures/common.py new file mode 100644 index 00000000..041f482a --- /dev/null +++ b/b2sdk/_internal/testing/fixtures/common.py @@ -0,0 +1,16 @@ +###################################################################### +# +# File: b2sdk/_internal/testing/fixtures/common.py +# +# Copyright 2025 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### +import pytest + + +@pytest.fixture(scope='session') +def monkeysession(): + with pytest.MonkeyPatch.context() as mp: + yield mp diff --git a/b2sdk/v3/testing/__init__.py b/b2sdk/v3/testing/__init__.py index 2cdbd76e..bd2d012b 100644 --- a/b2sdk/v3/testing/__init__.py +++ b/b2sdk/v3/testing/__init__.py @@ -45,3 +45,5 @@ bucket, b2_subfolder, ) +from b2sdk._internal.testing.fixtures.common import monkeysession +from b2sdk._internal.testing.fixtures.account_info import change_account_info_dir diff --git a/changelog.d/+account_info_change_dir.infrastructure.md b/changelog.d/+account_info_change_dir.infrastructure.md new file mode 100644 index 00000000..5be2b9ef --- /dev/null +++ b/changelog.d/+account_info_change_dir.infrastructure.md @@ -0,0 +1 @@ +Set temporary directory as account info config dir in tests. diff --git a/test/integration/conftest.py b/test/integration/conftest.py new file mode 100644 index 00000000..d700e2fd --- /dev/null +++ b/test/integration/conftest.py @@ -0,0 +1,15 @@ +###################################################################### +# +# File: test/integration/conftest.py +# +# Copyright 2025 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### +import pytest + + +@pytest.fixture(scope='session', autouse=True) +def auto_change_account_info_dir(change_account_info_dir): + pass diff --git a/test/unit/account_info/test_account_info.py b/test/unit/account_info/test_account_info.py index 65069d8f..cfa43e8f 100644 --- a/test/unit/account_info/test_account_info.py +++ b/test/unit/account_info/test_account_info.py @@ -485,10 +485,6 @@ def test_account_info_env_var_overrides_xdg_config_home(self): actual_path = os.path.abspath(account_info.filename) assert expected_path == actual_path - def test_resolve_xdg_os_default(self): - is_xdg_os = bool(SqliteAccountInfo._get_xdg_config_path()) - assert is_xdg_os == (sys.platform not in ('win32', 'darwin')) - def test_resolve_xdg_os_default_no_env_var(self, monkeypatch): # ensure that XDG_CONFIG_HOME_ENV_VAR doesn't to resolve XDG-like OS monkeypatch.delenv(XDG_CONFIG_HOME_ENV_VAR, raising=False) diff --git a/test/unit/conftest.py b/test/unit/conftest.py index cbf31e35..8d208fb0 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -149,6 +149,11 @@ def test_function(self): pytest.skip('test requires apiver to be in range: [%d, %d]' % (from_ver, to_ver)) +@pytest.fixture(scope='session', autouse=True) +def auto_change_account_info_dir(change_account_info_dir): + pass + + @pytest.fixture(scope='session') def apiver(request): """Get apiver as a v-prefixed string, e.g. "v2"."""