| From dd216b91fa2bf2c25fe92baec921d0668ac5a01b Mon Sep 17 00:00:00 2001 |
| From: Skyler Grey <minion@clicks.codes> |
| Date: Sun, 14 Jul 2024 13:49:58 +0000 |
| Subject: [PATCH] fix: Respect ordering of provided ledgers |
| |
| Previously the _add_env_filenames function converted the filenames to a |
| set. As [sets are unordered](https://docs.python.org/3/tutorial/datastructures.html#sets), |
| this prevented specifying order. Some bits of the code, notably the |
| default "/" route, relied on order to determine which the default ledger |
| was |
| |
| The use of set() was first introduced in f33a63e35a1751001ac10fb992aba4e434ea7321. |
| It's not entirely clear from the commit, but presumably it was intended |
| to filter out duplicate items as this is a common pattern in Python. The |
| type is hinted to be an Iterable[] and lists are used in other places, |
| such as tests, so nothing requires using a set |
| |
| To make test changes as minimal as possible, this commit makes the files |
| included from environment variables be ordered *first* |
| --- |
| src/fava/cli.py | 7 ++++--- |
| tests/test_cli.py | 6 +++--- |
| 2 files changed, 7 insertions(+), 6 deletions(-) |
| |
| diff --git a/src/fava/cli.py b/src/fava/cli.py |
| index 6d9b45594..2ae854b2f 100644 |
| --- a/src/fava/cli.py |
| +++ b/src/fava/cli.py |
| @@ -37,18 +37,19 @@ def __init__(self) -> None: # pragma: no cover |
| super().__init__("No file specified") |
| |
| |
| -def _add_env_filenames(filenames: tuple[str, ...]) -> set[str]: |
| +def _add_env_filenames(filenames: tuple[str, ...]) -> tuple[str, ...]: |
| """Read additional filenames from BEANCOUNT_FILE.""" |
| env_filename = os.environ.get("BEANCOUNT_FILE") |
| if not env_filename: |
| - return set(filenames) |
| + return filenames |
| |
| env_names = env_filename.split(os.pathsep) |
| for name in env_names: |
| if not Path(name).is_absolute(): |
| raise NonAbsolutePathError(name) |
| |
| - return set(filenames + tuple(env_names)) |
| + all_names = tuple(env_names) + filenames |
| + return tuple(name for [index, name] in enumerate(all_names) if index == all_names.index(name)) |
| |
| |
| @click.command(context_settings={"auto_envvar_prefix": "FAVA"}) |
| diff --git a/tests/test_cli.py b/tests/test_cli.py |
| index dc7466b75..9d94c046f 100644 |
| --- a/tests/test_cli.py |
| +++ b/tests/test_cli.py |
| @@ -29,17 +29,17 @@ def test__add_env_filenames( |
| other = str(absolute_path / "other") |
| |
| monkeypatch.delenv("BEANCOUNT_FILE", raising=False) |
| - assert _add_env_filenames((asdf,)) == {asdf} |
| + assert _add_env_filenames((asdf,)) == (asdf,) |
| |
| monkeypatch.setenv("BEANCOUNT_FILE", "path") |
| with pytest.raises(NonAbsolutePathError): |
| _add_env_filenames((asdf,)) |
| |
| monkeypatch.setenv("BEANCOUNT_FILE", absolute) |
| - assert _add_env_filenames((asdf,)) == {absolute, asdf} |
| + assert _add_env_filenames((asdf,)) == (absolute, asdf) |
| |
| monkeypatch.setenv("BEANCOUNT_FILE", os.pathsep.join([absolute, other])) |
| - assert _add_env_filenames((asdf,)) == {absolute, other, asdf} |
| + assert _add_env_filenames((asdf,)) == (absolute, other, asdf) |
| |
| |
| @pytest.fixture |