Skip to content
Open
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
34 changes: 34 additions & 0 deletions b2sdk/_internal/testing/fixtures/account_info.py
Original file line number Diff line number Diff line change
@@ -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))
16 changes: 16 additions & 0 deletions b2sdk/_internal/testing/fixtures/common.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions b2sdk/v3/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions changelog.d/+account_info_change_dir.infrastructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set temporary directory as account info config dir in tests.
15 changes: 15 additions & 0 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions test/unit/account_info/test_account_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"."""
Expand Down