lemon24 / reader

A Python feed reader library.
https://reader.readthedocs.io
BSD 3-Clause "New" or "Revised" License
456 stars 38 forks source link

test_cli.py fails when local config is present #355

Closed maksverver closed 1 month ago

maksverver commented 1 month ago

To reproduce (on Linux):

% git clone git@github.com:lemon24/reader.git
% cd reader
% mkdir -p ~/.config/reader
% cp -n examples/config.yaml ~/.config/reader/
% PYTHONPATH=src/ pytest --runslow tests/test_cli.py

Partial output:

platform linux -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/maks/tmp/reader
configfile: pyproject.toml
plugins: typeguard-4.3.0, requests-mock-1.11.0, hypothesis-6.112.1, anyio-4.4.0, subtests-0.12.1, benchmark-4.0.0
collected 8 items

...

FAILED tests/test_cli.py::test_cli - AssertionError: assert '/home/maks/t...00000000000\n' == ''
FAILED tests/test_cli.py::test_cli_plugin_update_exception - AssertionError: assert '2 ok, 0 error, 0 not modified; entries: 4 new, 0 modified' in '0:00:00.003477\t0/2\t/home/maks/tmp/reader/tests/data/full.atom\tnot modified, 2 total\n0:00:...
FAILED tests/test_cli.py::test_cli_serve_calls_create_app - AssertionError: assert {'plugins': {...0/db.sqlite'}} == {'reader': {'...0/db.sqlite'}}

The problem is that the CLI will read the local config from ~/.config/reader/config.yaml, if that file exists, which can result in additional plugins to be loaded, and then the output doesn't match expectations.

I can work around it locally like this:

% XDG_CONFIG_HOME=/nonexistent PYTHONPATH=src/ pytest --runslow tests/test_cli.py 

I can also patch the test to do that, like this:

diff --git a/tests/test_cli.py b/tests/test_cli.py
index c563a38..1bfb7a8 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -3,6 +3,8 @@ import os
 import pathlib
 from datetime import timedelta

+os.environ['XDG_CONFIG_HOME'] = '/nonexistent'
+
 import click
 import pytest
 import yaml

However, this seems a bit ugly and only fixes the issue on Linux.

lemon24 commented 1 month ago

Hi, thank you for reporting this, https://github.com/lemon24/reader/commit/bdf9c182acfeb37ee36d9339d89abded47581bf6 should fix it.

maksverver commented 1 month ago

Unfortunately that doesn't seem to fix the problem entirely, since get_app_dir() is called when reader._cli is imported, so the override happens too late to take effect:

https://github.com/lemon24/reader/blob/ae5bc327909521655d0645b7e1d2332638b84c40/src/reader/_cli.py#L188

I have an idea how to fix it properly. I'll send you a pull request.

lemon24 commented 1 month ago

Thank you for the PR!