From 03b5be91f7e26ee961a0206694eff5b940601e1e Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 30 Sep 2019 07:28:49 +0200 Subject: [PATCH 1/2] some commands should not have config at all --- freqtrade/configuration/arguments.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/freqtrade/configuration/arguments.py b/freqtrade/configuration/arguments.py index 6e2ecea2e..89c86bdca 100644 --- a/freqtrade/configuration/arguments.py +++ b/freqtrade/configuration/arguments.py @@ -41,7 +41,9 @@ ARGS_PLOT_DATAFRAME = ["pairs", "indicators1", "indicators2", "plot_limit", "db_ ARGS_PLOT_PROFIT = ["pairs", "timerange", "export", "exportfilename", "db_url", "trade_source", "ticker_interval"] -NO_CONF_REQURIED = ["create-userdir", "download-data", "plot-dataframe", "plot-profit"] +NO_CONF_REQURIED = ["download-data", "plot-dataframe", "plot-profit"] + +NO_CONF_ALLOWED = ["create-userdir", "list-exchanges"] class Arguments: @@ -75,12 +77,15 @@ class Arguments: parsed_arg = self.parser.parse_args(self.args) # When no config is provided, but a config exists, use that configuration! + subparser = parsed_arg.subparser if 'subparser' in parsed_arg else None # Workaround issue in argparse with action='append' and default value # (see https://bugs.python.org/issue16399) # Allow no-config for certain commands (like downloading / plotting) - if (parsed_arg.config is None and ((Path.cwd() / constants.DEFAULT_CONFIG).is_file() or - not ('subparser' in parsed_arg and parsed_arg.subparser in NO_CONF_REQURIED))): + if (parsed_arg.config is None + and subparser not in NO_CONF_ALLOWED + and ((Path.cwd() / constants.DEFAULT_CONFIG).is_file() + or (subparser not in NO_CONF_REQURIED))): parsed_arg.config = [constants.DEFAULT_CONFIG] return parsed_arg From 739901b606a3793a01a4dcc4c6801b3b7ac0fbb2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 30 Sep 2019 09:48:00 +0200 Subject: [PATCH 2/2] Add test for this behaviour --- tests/test_arguments.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_arguments.py b/tests/test_arguments.py index cf0104c01..e874e6769 100644 --- a/tests/test_arguments.py +++ b/tests/test_arguments.py @@ -1,5 +1,7 @@ # pragma pylint: disable=missing-docstring, C0103 import argparse +from pathlib import Path +from unittest.mock import MagicMock import pytest @@ -177,6 +179,44 @@ def test_plot_profit_options() -> None: assert pargs["db_url"] == "sqlite:///whatever.sqlite" +def test_config_notallowed(mocker) -> None: + mocker.patch.object(Path, "is_file", MagicMock(return_value=False)) + args = [ + 'create-userdir', + ] + pargs = Arguments(args).get_parsed_arg() + + assert pargs["config"] is None + + # When file exists: + mocker.patch.object(Path, "is_file", MagicMock(return_value=True)) + args = [ + 'create-userdir', + ] + pargs = Arguments(args).get_parsed_arg() + # config is not added even if it exists, since create-userdir is in the notallowed list + assert pargs["config"] is None + + +def test_config_notrequired(mocker) -> None: + mocker.patch.object(Path, "is_file", MagicMock(return_value=False)) + args = [ + 'download-data', + ] + pargs = Arguments(args).get_parsed_arg() + + assert pargs["config"] is None + + # When file exists: + mocker.patch.object(Path, "is_file", MagicMock(return_value=True)) + args = [ + 'download-data', + ] + pargs = Arguments(args).get_parsed_arg() + # config is added if it exists + assert pargs["config"] == ['config.json'] + + def test_check_int_positive() -> None: assert check_int_positive("3") == 3 assert check_int_positive("1") == 1