Skip to content

Commit 59d04d4

Browse files
hroncokfrenzymadness
authored andcommitted
Make order of potential config file locations deterministic and tested
Also, join paths in the test with os.path.join. Fixes #37
1 parent 907f7d0 commit 59d04d4

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

joft/utils.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def load_and_parse_yaml_file(path: str) -> typing.Dict[str, typing.Any]:
1616

1717

1818
def load_toml_app_config() -> typing.Any:
19-
possible_paths = set()
19+
possible_paths = []
2020

21-
possible_paths.add(platformdirs.user_config_dir())
22-
possible_paths.update(platformdirs.site_config_dir(multipath=True).split(":"))
23-
possible_paths.add("/etc")
24-
possible_paths.add(str(pathlib.Path.cwd()))
21+
possible_paths.append(str(pathlib.Path.cwd()))
22+
possible_paths.append(platformdirs.user_config_dir())
23+
possible_paths.extend(platformdirs.site_config_dir(multipath=True).split(":"))
24+
possible_paths.append("/etc")
2525

2626
for path in possible_paths:
2727
config_file_path = pathlib.Path(path) / "joft.config.toml"

tests/test_utils.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,31 @@ def test_load_invalid_yaml_raise() -> None:
3030
@unittest.mock.patch("joft.utils.pathlib.Path.cwd")
3131
@unittest.mock.patch("joft.utils.platformdirs")
3232
def test_load_toml_app_config(mock_platformdirs, mock_cwd) -> None:
33-
"""Test if we can find the app config file in one of the platform dirs"""
34-
hostname = "test"
35-
pat_token = "pat_token"
33+
"""Test if we can find the app config file in one of the platform dirs
3634
37-
config_file_contents = b"""[jira.server]
38-
hostname = "test"
39-
pat_token = "pat_token"
35+
Assert that user_config_dir is preferred over site_config_dir."""
36+
config_file_contents = """[jira.server]
37+
hostname = "{name}"
38+
pat_token = "__pat_token__"
4039
"""
4140

4241
with tempfile.TemporaryDirectory() as tmpdir:
4342
mock_cwd.return_value = tmpdir
4443
dir_names = ["etc", ".config"]
4544
for name in dir_names:
46-
config_dir = tmpdir + "/" + name
45+
config_dir = os.path.join(tmpdir, name)
4746
os.makedirs(config_dir)
4847

49-
with open(tmpdir + "/" + dir_names[1] + "/" + "joft.config.toml", "wb") as fp:
50-
fp.write(config_file_contents)
48+
with open(os.path.join(config_dir, "joft.config.toml"), "w") as fp:
49+
fp.write(config_file_contents.format(name=name))
5150

52-
mock_platformdirs.user_config_dir.return_value = tmpdir + "/" + ".config"
53-
mock_platformdirs.site_config_dir.return_value = ""
51+
mock_platformdirs.user_config_dir.return_value = os.path.join(tmpdir, ".config")
52+
mock_platformdirs.site_config_dir.return_value = os.path.join(tmpdir, "etc")
5453

5554
config = joft.utils.load_toml_app_config()
5655

57-
assert config["jira"]["server"]["hostname"] == hostname
58-
assert config["jira"]["server"]["pat_token"] == pat_token
56+
assert config["jira"]["server"]["hostname"] == ".config"
57+
assert config["jira"]["server"]["pat_token"] == "__pat_token__"
5958

6059

6160
@unittest.mock.patch("joft.utils.pathlib.Path.cwd")

0 commit comments

Comments
 (0)