Skip to content

Commit

Permalink
Merge pull request #1900 from sopel-irc/env-SOPEL_CONFIG_DIR
Browse files Browse the repository at this point in the history
cli: support `SOPEL_CONFIG_DIR` envvar to override `--config-dir` default
  • Loading branch information
dgw authored Jul 3, 2020
2 parents 64b8cdf + e3e792b commit 73e7230
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 7 additions & 2 deletions sopel/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,14 @@ def add_common_arguments(parser):
"""))
parser.add_argument(
'--config-dir',
default=config.DEFAULT_HOMEDIR,
default=os.environ.get('SOPEL_CONFIG_DIR') or config.DEFAULT_HOMEDIR,
dest='configdir',
help='Look for configuration files in this directory.')
help=inspect.cleandoc("""
Look for configuration files in this directory.
By default, Sopel will search in ``~/.sopel``.
When the ``SOPEL_CONFIG_DIR`` environment variable is set and not
empty, it is used as the default value.
"""))


def load_settings(options):
Expand Down
24 changes: 19 additions & 5 deletions test/cli/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ def config_dir(tmpdir):
return test_dir


@pytest.fixture
def env_dir(tmpdir):
"""Pytest fixture used to generate an extra (external) config directory"""
test_dir = tmpdir.mkdir("fromenv")
test_dir.join('fromenv.cfg').write('')

return test_dir


def test_green():
assert green('hello') == '\x1b[32mhello\x1b[0m'
assert green('hello', reset=False) == '\x1b[32mhello'
Expand Down Expand Up @@ -232,8 +241,9 @@ def test_load_settings(config_dir):
assert settings.basename == 'config'


def test_load_settings_arg_priority_over_env(monkeypatch, config_dir):
def test_load_settings_arg_priority_over_env(monkeypatch, config_dir, env_dir):
monkeypatch.setenv('SOPEL_CONFIG', 'fromenv')
monkeypatch.setenv('SOPEL_CONFIG_DIR', env_dir.strpath)

config_dir.join('fromenv.cfg').write(TMP_CONFIG)
config_dir.join('fromarg.cfg').write(TMP_CONFIG)
Expand All @@ -248,6 +258,7 @@ def test_load_settings_arg_priority_over_env(monkeypatch, config_dir):
settings = load_settings(options)
assert isinstance(settings, config.Config)
assert settings.basename == 'fromarg'
assert os.path.dirname(settings.filename) == config_dir.strpath


def test_load_settings_default(config_dir):
Expand All @@ -261,18 +272,21 @@ def test_load_settings_default(config_dir):
assert isinstance(settings, config.Config)


def test_load_settings_default_env_var(monkeypatch, config_dir):
monkeypatch.setenv('SOPEL_CONFIG', 'config')
def test_load_settings_default_env_var(monkeypatch, config_dir, env_dir):
monkeypatch.setenv('SOPEL_CONFIG', 'fromenv')
monkeypatch.setenv('SOPEL_CONFIG_DIR', env_dir.strpath)

config_dir.join('config.cfg').write(TMP_CONFIG)
env_dir.join('fromenv.cfg').write(TMP_CONFIG)
parser = argparse.ArgumentParser()
add_common_arguments(parser)

options = parser.parse_args(['--config-dir', config_dir.strpath])
options = parser.parse_args([])

settings = load_settings(options)
assert isinstance(settings, config.Config)
assert settings.basename == 'config'
assert settings.basename == 'fromenv'
assert os.path.dirname(settings.filename) == env_dir.strpath


def test_load_settings_default_not_found(config_dir):
Expand Down

0 comments on commit 73e7230

Please sign in to comment.