Skip to content

Commit aed0fad

Browse files
committed
Set temporary directory as account info config dir in tests
1 parent c884f6f commit aed0fad

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/fixtures/account_info.py
4+
#
5+
# Copyright 2025 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from os import path
11+
12+
import pytest
13+
14+
from b2sdk._internal.account_info.sqlite_account_info import (
15+
B2_ACCOUNT_INFO_ENV_VAR,
16+
XDG_CONFIG_HOME_ENV_VAR,
17+
)
18+
19+
20+
@pytest.fixture(scope='session')
21+
def change_account_info_dir(monkeysession, tmp_path_factory):
22+
"""
23+
For the entire test session:
24+
1) temporarily remove B2_APPLICATION_KEY and B2_APPLICATION_KEY_ID from the environment
25+
2) create a temporary directory for storing account info database
26+
3) set B2_ACCOUNT_INFO_ENV_VAR to point to the temporary account info file
27+
"""
28+
29+
monkeysession.delenv('B2_APPLICATION_KEY_ID', raising=False)
30+
monkeysession.delenv('B2_APPLICATION_KEY', raising=False)
31+
32+
temp_dir = tmp_path_factory.mktemp('b2_config')
33+
34+
monkeysession.setenv(B2_ACCOUNT_INFO_ENV_VAR, path.join(temp_dir, '.b2_account_info'))
35+
monkeysession.setenv(XDG_CONFIG_HOME_ENV_VAR, temp_dir)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/fixtures/common.py
4+
#
5+
# Copyright 2025 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
import pytest
11+
12+
13+
@pytest.fixture(scope='session')
14+
def monkeysession():
15+
with pytest.MonkeyPatch.context() as mp:
16+
yield mp

b2sdk/v3/testing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@
4545
bucket,
4646
b2_subfolder,
4747
)
48+
from b2sdk._internal.testing.fixtures.common import monkeysession
49+
from b2sdk._internal.testing.fixtures.account_info import change_account_info_dir
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set temporary directory as account info config dir in tests.

test/integration/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
######################################################################
2+
#
3+
# File: test/integration/conftest.py
4+
#
5+
# Copyright 2025 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
import pytest
11+
12+
13+
@pytest.fixture(scope='session', autouse=True)
14+
def auto_change_account_info_dir(change_account_info_dir):
15+
pass

test/unit/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def test_function(self):
149149
pytest.skip('test requires apiver to be in range: [%d, %d]' % (from_ver, to_ver))
150150

151151

152+
@pytest.fixture(scope='session', autouse=True)
153+
def auto_change_account_info_dir(change_account_info_dir):
154+
pass
155+
156+
152157
@pytest.fixture(scope='session')
153158
def apiver(request):
154159
"""Get apiver as a v-prefixed string, e.g. "v2"."""

0 commit comments

Comments
 (0)