blob: 2005e700c50e53de4931578be7e7bb14a18b2855 [file] [log] [blame]
Skyler Greyf1b97d02024-07-14 15:02:32 +00001From dd216b91fa2bf2c25fe92baec921d0668ac5a01b Mon Sep 17 00:00:00 2001
2From: Skyler Grey <minion@clicks.codes>
3Date: Sun, 14 Jul 2024 13:49:58 +0000
4Subject: [PATCH] fix: Respect ordering of provided ledgers
5
6Previously the _add_env_filenames function converted the filenames to a
7set. As [sets are unordered](https://docs.python.org/3/tutorial/datastructures.html#sets),
8this prevented specifying order. Some bits of the code, notably the
9default "/" route, relied on order to determine which the default ledger
10was
11
12The use of set() was first introduced in f33a63e35a1751001ac10fb992aba4e434ea7321.
13It's not entirely clear from the commit, but presumably it was intended
14to filter out duplicate items as this is a common pattern in Python. The
15type is hinted to be an Iterable[] and lists are used in other places,
16such as tests, so nothing requires using a set
17
18To make test changes as minimal as possible, this commit makes the files
19included from environment variables be ordered *first*
20---
21 src/fava/cli.py | 7 ++++---
22 tests/test_cli.py | 6 +++---
23 2 files changed, 7 insertions(+), 6 deletions(-)
24
25diff --git a/src/fava/cli.py b/src/fava/cli.py
26index 6d9b45594..2ae854b2f 100644
27--- a/src/fava/cli.py
28+++ b/src/fava/cli.py
29@@ -37,18 +37,19 @@ def __init__(self) -> None: # pragma: no cover
30 super().__init__("No file specified")
31
32
33-def _add_env_filenames(filenames: tuple[str, ...]) -> set[str]:
34+def _add_env_filenames(filenames: tuple[str, ...]) -> tuple[str, ...]:
35 """Read additional filenames from BEANCOUNT_FILE."""
36 env_filename = os.environ.get("BEANCOUNT_FILE")
37 if not env_filename:
38- return set(filenames)
39+ return filenames
40
41 env_names = env_filename.split(os.pathsep)
42 for name in env_names:
43 if not Path(name).is_absolute():
44 raise NonAbsolutePathError(name)
45
46- return set(filenames + tuple(env_names))
47+ all_names = tuple(env_names) + filenames
48+ return tuple(name for [index, name] in enumerate(all_names) if index == all_names.index(name))
49
50
51 @click.command(context_settings={"auto_envvar_prefix": "FAVA"})
52diff --git a/tests/test_cli.py b/tests/test_cli.py
53index dc7466b75..9d94c046f 100644
54--- a/tests/test_cli.py
55+++ b/tests/test_cli.py
56@@ -29,17 +29,17 @@ def test__add_env_filenames(
57 other = str(absolute_path / "other")
58
59 monkeypatch.delenv("BEANCOUNT_FILE", raising=False)
60- assert _add_env_filenames((asdf,)) == {asdf}
61+ assert _add_env_filenames((asdf,)) == (asdf,)
62
63 monkeypatch.setenv("BEANCOUNT_FILE", "path")
64 with pytest.raises(NonAbsolutePathError):
65 _add_env_filenames((asdf,))
66
67 monkeypatch.setenv("BEANCOUNT_FILE", absolute)
68- assert _add_env_filenames((asdf,)) == {absolute, asdf}
69+ assert _add_env_filenames((asdf,)) == (absolute, asdf)
70
71 monkeypatch.setenv("BEANCOUNT_FILE", os.pathsep.join([absolute, other]))
72- assert _add_env_filenames((asdf,)) == {absolute, other, asdf}
73+ assert _add_env_filenames((asdf,)) == (absolute, other, asdf)
74
75
76 @pytest.fixture