Skip to content

Commit

Permalink
Merge pull request freqtrade#2320 from freqtrade/config_no_allowed
Browse files Browse the repository at this point in the history
args - Add config no allowed list to skip loading config.json
  • Loading branch information
hroff-1902 authored Sep 30, 2019
2 parents 9a2bd83 + 739901b commit b6ee3d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
11 changes: 8 additions & 3 deletions freqtrade/configuration/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
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:
Expand Down Expand Up @@ -75,12 +77,15 @@ def _parse_args(self) -> argparse.Namespace:
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
Expand Down
40 changes: 40 additions & 0 deletions tests/test_arguments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pragma pylint: disable=missing-docstring, C0103
import argparse
from pathlib import Path
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b6ee3d9

Please sign in to comment.