From 61f59f3c7a86d5588806bd073af04ed1d3a81c9e Mon Sep 17 00:00:00 2001 From: Maks Verver Date: Thu, 10 Oct 2024 10:36:59 +0200 Subject: [PATCH 1/2] Postpone evaluation of get_default_config_path(). #355 Previously, get_default_config_path() was called upon import, which makes it hard to override the return value in tests. With this change, the config path is determined when cli() is called and not sooner. --- src/reader/_cli.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/reader/_cli.py b/src/reader/_cli.py index 082c1d16..282e4431 100644 --- a/src/reader/_cli.py +++ b/src/reader/_cli.py @@ -122,13 +122,12 @@ def wrapper(*args, **kwargs): def config_option(*args, **kwargs): def callback(ctx, param, value): - # TODO: the default file is allowed to not exist, a user specified file must exist + config_path = value if value is not None else get_default_config_path() try: - with open(value) as file: + with open(config_path) as file: config = make_reader_config(yaml.safe_load(file)) - except FileNotFoundError as e: - if value != param.default: - raise click.BadParameter(str(e), ctx=ctx, param=param) from None + except FileNotFoundError: + assert value is None config = make_reader_config({}) ctx.default_map = config['cli'].get('defaults', {}) @@ -136,17 +135,14 @@ def callback(ctx, param, value): ctx.obj = config return config - def inner(fn): - return click.option( - *args, - type=click.Path(dir_okay=False), - callback=callback, - is_eager=True, - expose_value=False, - **kwargs, - )(fn) - - return inner + return click.option( + *args, + type=click.Path(exists=True, dir_okay=False), + callback=callback, + is_eager=True, + expose_value=False, + **kwargs, + ) def pass_reader(fn): @@ -166,7 +162,6 @@ def wrapper(*args, **kwargs): '--db', type=click.Path(dir_okay=False), envvar=reader._DB_ENVVAR, - show_default=True, help=f"Path to the reader database. [default: {get_default_db_path()}]", ) @click.option( @@ -184,9 +179,7 @@ def wrapper(*args, **kwargs): @config_option( '--config', envvar=reader._CONFIG_ENVVAR, - help="Path to the reader config.", - default=get_default_config_path(), - show_default=True, + help=f"Path to the reader config. [default: {get_default_config_path()}]", ) @click.option( '--feed-root', From b6f93ee1c4d27af87fd2c48aee1347a6d796faf7 Mon Sep 17 00:00:00 2001 From: Maks Verver Date: Thu, 10 Oct 2024 11:29:28 +0200 Subject: [PATCH 2/2] Ignore local config when running test_bench.py too. #355 See also commit bdf9c18. --- tests/test_bench.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_bench.py b/tests/test_bench.py index 4633edc3..443aa8c6 100644 --- a/tests/test_bench.py +++ b/tests/test_bench.py @@ -5,6 +5,7 @@ from click.testing import CliRunner from reader import make_reader +from test_cli import patch_app_dir from test_reader_filter import setup_reader_for_tags