Skip to content

Commit

Permalink
Add SqliteAccountInfo.get_user_account_info_path to public API
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Mar 28, 2024
1 parent ea8626b commit a3d5f22
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions b2sdk/account_info/sqlite_account_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, file_name=None, last_upgrade_to_run=None, profile: str | None
"""
self.thread_local = threading.local()

self.filename = self._get_user_account_info_path(file_name=file_name, profile=profile)
self.filename = self.get_user_account_info_path(file_name=file_name, profile=profile)
logger.debug('%s file path to use: %s', self.__class__.__name__, self.filename)

self._validate_database(last_upgrade_to_run)
Expand Down Expand Up @@ -105,7 +105,7 @@ def _get_xdg_config_path(cls) -> str | None:
return None

@classmethod
def _get_user_account_info_path(cls, file_name: str | None = None, profile: str | None = None):
def get_user_account_info_path(cls, file_name: str | None = None, profile: str | None = None):
if profile and not B2_ACCOUNT_INFO_PROFILE_NAME_REGEXP.match(profile):
raise ValueError(f'Invalid profile name: {profile}')

Expand Down
1 change: 1 addition & 0 deletions changelog.d/+public_get_user_account_info_path.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add SqliteAccountInfo.get_user_account_info_path to public API.
20 changes: 10 additions & 10 deletions test/unit/account_info/test_sqlite_account_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ def setup(self, monkeypatch, tmpdir):

def test_invalid_profile_name(self):
with pytest.raises(ValueError):
SqliteAccountInfo._get_user_account_info_path(profile='&@(*$')
SqliteAccountInfo.get_user_account_info_path(profile='&@(*$')

def test_profile_and_file_name_conflict(self):
with pytest.raises(ValueError):
SqliteAccountInfo._get_user_account_info_path(file_name='foo', profile='bar')
SqliteAccountInfo.get_user_account_info_path(file_name='foo', profile='bar')

def test_profile_and_env_var_conflict(self, monkeypatch):
monkeypatch.setenv(B2_ACCOUNT_INFO_ENV_VAR, 'foo')
with pytest.raises(ValueError):
SqliteAccountInfo._get_user_account_info_path(profile='bar')
SqliteAccountInfo.get_user_account_info_path(profile='bar')

def test_profile_and_xdg_config_env_var(self, monkeypatch):
monkeypatch.setenv(XDG_CONFIG_HOME_ENV_VAR, os.path.join('~', 'custom'))
account_info_path = SqliteAccountInfo._get_user_account_info_path(profile='secondary')
account_info_path = SqliteAccountInfo.get_user_account_info_path(profile='secondary')
assert account_info_path == os.path.expanduser(
os.path.join('~', 'custom', 'b2', 'db-secondary.sqlite')
)
Expand All @@ -102,18 +102,18 @@ def test_profile(self, monkeypatch):
else:
expected_path = ('~', '.b2db-foo.sqlite')

account_info_path = SqliteAccountInfo._get_user_account_info_path(profile='foo')
account_info_path = SqliteAccountInfo.get_user_account_info_path(profile='foo')
assert account_info_path == os.path.expanduser(os.path.join(*expected_path))

def test_file_name(self):
account_info_path = SqliteAccountInfo._get_user_account_info_path(
account_info_path = SqliteAccountInfo.get_user_account_info_path(
file_name=os.path.join('~', 'foo')
)
assert account_info_path == os.path.expanduser(os.path.join('~', 'foo'))

def test_env_var(self, monkeypatch):
monkeypatch.setenv(B2_ACCOUNT_INFO_ENV_VAR, os.path.join('~', 'foo'))
account_info_path = SqliteAccountInfo._get_user_account_info_path()
account_info_path = SqliteAccountInfo.get_user_account_info_path()
assert account_info_path == os.path.expanduser(os.path.join('~', 'foo'))

def test_default_file_if_exists(self, monkeypatch):
Expand All @@ -124,12 +124,12 @@ def test_default_file_if_exists(self, monkeypatch):
os.makedirs(parent_dir, exist_ok=True)
with open(account_file_path, 'w') as account_file:
account_file.write('')
account_info_path = SqliteAccountInfo._get_user_account_info_path()
account_info_path = SqliteAccountInfo.get_user_account_info_path()
assert account_info_path == os.path.expanduser(B2_ACCOUNT_INFO_DEFAULT_FILE)

def test_xdg_config_env_var(self, monkeypatch):
monkeypatch.setenv(XDG_CONFIG_HOME_ENV_VAR, os.path.join('~', 'custom'))
account_info_path = SqliteAccountInfo._get_user_account_info_path()
account_info_path = SqliteAccountInfo.get_user_account_info_path()
assert account_info_path == os.path.expanduser(
os.path.join('~', 'custom', 'b2', 'account_info')
)
Expand All @@ -141,5 +141,5 @@ def test_default_file(self):
else:
expected_path = B2_ACCOUNT_INFO_DEFAULT_FILE

account_info_path = SqliteAccountInfo._get_user_account_info_path()
account_info_path = SqliteAccountInfo.get_user_account_info_path()
assert account_info_path == os.path.expanduser(expected_path)

0 comments on commit a3d5f22

Please sign in to comment.