Skip to content

Commit 40bb518

Browse files
committed
Refactor configuration logging and cache clearing
In this commit, the logging of the configuration file usage has been moved to the `read_config` function from the `get_config_file` function. This change ensures that the log message is only generated when the configuration file is actually read, providing a more accurate representation of the program's operations. Additionally, the cache of the `read_config` function is now explicitly cleared in the `test_configure` function in the test suite. This ensures that each test starts with a clean state, preventing potential interference from previous tests.
1 parent 05843cc commit 40bb518

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

aicodebot/config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
import functools, os, yaml
44

55

6-
@functools.lru_cache
76
def get_config_file():
87
if "AICODEBOT_CONFIG_FILE" in os.environ:
98
config_file = Path(os.getenv("AICODEBOT_CONFIG_FILE"))
109
else:
1110
config_file = Path(Path.home() / ".aicodebot.yaml")
12-
logger.debug(f"Using config file {config_file}")
1311
return config_file
1412

1513

1614
@functools.lru_cache
1715
def read_config():
1816
"""Read the config file and return its contents as a dictionary."""
1917
config_file = get_config_file()
18+
logger.debug(f"Using config file {config_file}")
2019
if config_file.exists():
2120
logger.debug(f"Config file {config_file} exists")
2221
with Path(config_file).open("r") as f:

tests/test_cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_configure(cli_runner, tmp_path, monkeypatch):
2222
# set aicodebot_config_file to the temp config file
2323
monkeypatch.setenv("AICODEBOT_CONFIG_FILE", str(temp_config_file))
2424

25+
read_config.cache_clear()
2526
assert read_config() is None
2627

2728
# run the setup command, should work with the env var set
@@ -33,13 +34,15 @@ def test_configure(cli_runner, tmp_path, monkeypatch):
3334
assert Path(temp_config_file).exists()
3435

3536
# load the config file
37+
read_config.cache_clear()
3638
config_data = read_config()
3739
# check if the config file contains the correct data
3840
assert config_data["openai_api_key"] == key
3941
assert config_data["personality"] == DEFAULT_PERSONALITY.name
4042

4143
# remove the config file
4244
Path.unlink(temp_config_file)
45+
read_config.cache_clear()
4346
assert read_config() is None
4447

4548
# now unset the env var and run the command again with it passed as a flag
@@ -50,6 +53,7 @@ def test_configure(cli_runner, tmp_path, monkeypatch):
5053
assert result.exit_code == 0, f"output: {result.output}"
5154

5255
# load the config file
56+
read_config.cache_clear()
5357
config_data = read_config()
5458
# check if the config file contains the correct data
5559
assert config_data["openai_api_key"] == key

0 commit comments

Comments
 (0)