Skip to content

Commit 91da48e

Browse files
committed
fix(config): ensure .current file exists before usage
before this change, only the _get_current_path() helper ensured .current existed with this change, both _get_current_path() and _update_current_path() ensure .current exists
1 parent a962b31 commit 91da48e

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

littlepay/config.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@
2121
CONFIG_TYPES = list(DEFAULT_ACTIVE.keys())
2222

2323

24+
def _ensure_current_exists() -> bool:
25+
"""
26+
Creates the CONFIG_FILE_CURRENT file if it doesn't already exist.
27+
28+
Returns (bool):
29+
A flag indicating if the file existed or not.
30+
"""
31+
exists = CONFIG_FILE_CURRENT.exists()
32+
if not exists:
33+
CONFIG_FILE_CURRENT.parent.mkdir(parents=True, exist_ok=True)
34+
CONFIG_FILE_CURRENT.touch()
35+
return exists
36+
37+
2438
def _get_current_path() -> Path:
2539
"""
2640
Returns (Path):
2741
The path to the config file currently in-use, or the default.
2842
"""
29-
CONFIG_FILE_CURRENT.parent.mkdir(parents=True, exist_ok=True)
30-
if not CONFIG_FILE_CURRENT.exists():
31-
CONFIG_FILE_CURRENT.touch()
43+
if not _ensure_current_exists():
3244
_update_current_path(DEFAULT_CONFIG_FILE)
3345
current = CONFIG_FILE_CURRENT.read_text().strip()
3446
return Path(current)
@@ -38,6 +50,7 @@ def _update_current_path(new_path: str | Path):
3850
"""Saves new_path as the path to the current config file."""
3951
if isinstance(new_path, Path):
4052
new_path = str(new_path.expanduser().absolute())
53+
_ensure_current_exists()
4154
CONFIG_FILE_CURRENT.write_text(new_path)
4255

4356

tests/test_config.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from littlepay.config import (
77
DEFAULT_CONFIG,
88
DEFAULT_CREDENTIALS,
9+
_ensure_current_exists,
910
_get_current_path,
1011
_read_config,
1112
_write_config,
@@ -15,45 +16,69 @@
1516
from tests.conftest import CUSTOM_CONFIG_FILE
1617

1718

18-
def test_get_current_path_default(custom_config_file: Path):
19+
@pytest.fixture
20+
def spy_ensure_current_exists(mocker):
21+
return mocker.spy(littlepay.config, "_ensure_current_exists")
22+
23+
24+
def test_ensure_current_exists():
25+
# reminder: the module-level variable littlepay.config.CONFIG_FILE_CURRENT
26+
# is overwritten by the autouse fixture custom_current_file
27+
28+
# the .current file should not exist to begin with
29+
assert not littlepay.config.CONFIG_FILE_CURRENT.exists()
30+
assert _ensure_current_exists() is False
31+
32+
# now having ensured, the current file should exist
33+
assert littlepay.config.CONFIG_FILE_CURRENT.exists()
34+
# subsequent calls to ensure should indicate that it already exists
35+
assert _ensure_current_exists()
36+
37+
38+
def test_get_current_path_default(custom_config_file: Path, spy_ensure_current_exists):
1939
result = _get_current_path()
2040

2141
assert isinstance(result, Path)
2242
assert result.absolute() == custom_config_file.absolute()
43+
assert spy_ensure_current_exists.call_count > 0
2344

2445

25-
def test_get_current_path_custom(custom_current_file: Path):
46+
def test_get_current_path_custom(custom_current_file: Path, spy_ensure_current_exists):
2647
expected = "."
2748
custom_current_file.write_text(expected)
2849

2950
result = _get_current_path()
3051

3152
assert result == Path(expected)
53+
assert spy_ensure_current_exists.call_count > 0
3254

3355

34-
def test_get_current_path_newline(custom_current_file: Path):
56+
def test_get_current_path_newline(custom_current_file: Path, spy_ensure_current_exists):
3557
expected = "."
3658
custom_current_file.write_text(".\n")
3759

3860
result = _get_current_path()
3961

4062
assert result == Path(expected)
63+
assert spy_ensure_current_exists.call_count > 0
4164

4265

43-
def test_update_current_path_str(custom_current_file: Path):
66+
def test_update_current_path_str(custom_current_file: Path, spy_ensure_current_exists):
4467
assert not custom_current_file.exists()
4568

4669
_update_current_path("/the/path")
4770

4871
assert custom_current_file.read_text() == "/the/path"
72+
assert spy_ensure_current_exists.call_count > 0
4973

5074

51-
def test_update_current_path_Path(custom_current_file: Path):
75+
def test_update_current_path_Path(custom_current_file: Path, spy_ensure_current_exists):
5276
assert not custom_current_file.exists()
5377

5478
_update_current_path(Path("/the/path"))
5579

5680
assert custom_current_file.read_text() == "/the/path"
81+
assert spy_ensure_current_exists.call_count > 0
5782

5883

5984
def test_read_config(custom_config_file: Path):

0 commit comments

Comments
 (0)