|
13 | 13 | import unittest.mock as mock
|
14 | 14 | import os
|
15 | 15 | import platform
|
| 16 | +import shutil |
16 | 17 | import tempfile
|
17 | 18 |
|
18 | 19 | import pytest
|
19 | 20 |
|
20 |
| -from apiver_deps import AbstractAccountInfo, InMemoryAccountInfo, UploadUrlPool, SqliteAccountInfo |
| 21 | +from apiver_deps import AbstractAccountInfo, InMemoryAccountInfo, UploadUrlPool, SqliteAccountInfo, TempDir, B2_ACCOUNT_INFO_ENV_VAR |
21 | 22 | from apiver_deps_exception import CorruptAccountInfo, MissingAccountData
|
22 | 23 |
|
23 | 24 | from .fixtures import *
|
@@ -288,12 +289,14 @@ def setUp(self, request):
|
288 | 289 | os.unlink(self.db_path)
|
289 | 290 | except OSError:
|
290 | 291 | pass
|
291 |
| - print('using %s' % self.db_path) |
| 292 | + self.home = tempfile.mkdtemp() |
| 293 | + |
292 | 294 | yield
|
293 | 295 | try:
|
294 | 296 | os.unlink(self.db_path)
|
295 | 297 | except OSError:
|
296 | 298 | pass
|
| 299 | + shutil.rmtree(self.home) |
297 | 300 |
|
298 | 301 | def test_corrupted(self):
|
299 | 302 | """
|
@@ -331,8 +334,67 @@ def test_convert_from_json(self):
|
331 | 334 | def _make_info(self):
|
332 | 335 | return self._make_sqlite_account_info()
|
333 | 336 |
|
334 |
| - def _make_sqlite_account_info(self, last_upgrade_to_run=None): |
| 337 | + def _make_sqlite_account_info(self, env=None, last_upgrade_to_run=None): |
335 | 338 | """
|
336 | 339 | Returns a new SqliteAccountInfo that has just read the data from the file.
|
| 340 | +
|
| 341 | + :param dict env: Override Environment variables. |
337 | 342 | """
|
338 |
| - return SqliteAccountInfo(file_name=self.db_path, last_upgrade_to_run=None) |
| 343 | + # Override HOME to ensure hermetic tests |
| 344 | + with mock.patch('os.environ', env or {'HOME': self.home}): |
| 345 | + return SqliteAccountInfo( |
| 346 | + file_name=self.db_path if not env else None, |
| 347 | + last_upgrade_to_run=last_upgrade_to_run, |
| 348 | + ) |
| 349 | + |
| 350 | + def test_uses_default(self): |
| 351 | + account_info = self._make_sqlite_account_info( |
| 352 | + env={ |
| 353 | + 'HOME': self.home, |
| 354 | + } |
| 355 | + ) |
| 356 | + actual_path = os.path.abspath(account_info.filename) |
| 357 | + assert os.path.join(self.home, '.b2_account_info') == actual_path |
| 358 | + |
| 359 | + def test_uses_xdg_config_home(self, apiver): |
| 360 | + with TempDir() as d: |
| 361 | + account_info = self._make_sqlite_account_info( |
| 362 | + env={ |
| 363 | + 'HOME': self.home, |
| 364 | + 'XDG_CONFIG_HOME': d, |
| 365 | + } |
| 366 | + ) |
| 367 | + if apiver in ['v0', 'v1']: |
| 368 | + expected_path = os.path.abspath(os.path.join(self.home, '.b2_account_info')) |
| 369 | + else: |
| 370 | + assert os.path.exists(os.path.join(d, 'b2')) |
| 371 | + expected_path = os.path.abspath(os.path.join(d, 'b2', 'account_info')) |
| 372 | + actual_path = os.path.abspath(account_info.filename) |
| 373 | + assert expected_path == actual_path |
| 374 | + |
| 375 | + def test_uses_existing_file_and_ignores_xdg(self): |
| 376 | + with TempDir() as d: |
| 377 | + default_db_file_location = os.path.join(self.home, '.b2_account_info') |
| 378 | + open(default_db_file_location, 'a').close() |
| 379 | + account_info = self._make_sqlite_account_info( |
| 380 | + env={ |
| 381 | + 'HOME': self.home, |
| 382 | + 'XDG_CONFIG_HOME': d, |
| 383 | + } |
| 384 | + ) |
| 385 | + assert not os.path.exists(os.path.join(d, 'b2')) |
| 386 | + actual_path = os.path.abspath(account_info.filename) |
| 387 | + assert default_db_file_location == actual_path |
| 388 | + |
| 389 | + def test_account_info_env_var_overrides_xdg_config_home(self): |
| 390 | + with TempDir() as d: |
| 391 | + account_info = self._make_sqlite_account_info( |
| 392 | + env={ |
| 393 | + 'HOME': self.home, |
| 394 | + 'XDG_CONFIG_HOME': d, |
| 395 | + B2_ACCOUNT_INFO_ENV_VAR: os.path.join(d, 'b2_account_info'), |
| 396 | + } |
| 397 | + ) |
| 398 | + expected_path = os.path.abspath(os.path.join(d, 'b2_account_info')) |
| 399 | + actual_path = os.path.abspath(account_info.filename) |
| 400 | + assert expected_path == actual_path |
0 commit comments