Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file config extra_vars options loading #1372

Merged
merged 4 commits into from Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/ansiblelint/cli.py
Expand Up @@ -300,25 +300,29 @@ def merge_config(file_config, cli_config: Namespace) -> Namespace:
return cli_config

for entry in bools:
x = getattr(cli_config, entry) or file_config.get(entry, False)
x = getattr(cli_config, entry) or file_config.pop(entry, False)
setattr(cli_config, entry, x)

for entry, default in scalar_map.items():
x = getattr(cli_config, entry, None) or file_config.get(entry, default)
x = getattr(cli_config, entry, None) or file_config.pop(entry, default)
setattr(cli_config, entry, x)

# if either commandline parameter or config file option is set merge
# with the other, if neither is set use the default
for entry, default in lists_map.items():
if getattr(cli_config, entry, None) or entry in file_config.keys():
value = getattr(cli_config, entry, [])
value.extend(file_config.get(entry, []))
value.extend(file_config.pop(entry, []))
else:
value = default
setattr(cli_config, entry, value)

if 'verbosity' in file_config:
cli_config.verbosity = cli_config.verbosity + file_config['verbosity']
cli_config.verbosity = cli_config.verbosity + file_config.pop('verbosity')

# merge options that can be set only via a file config
for entry, value in file_config.items():
setattr(cli_config, entry, value)

return cli_config

Expand Down
9 changes: 9 additions & 0 deletions test/TestCommandLineInvocationSameAsConfig.py
Expand Up @@ -134,3 +134,12 @@ def test_config_failure(base_arguments, config_file):
"""Ensures specific config files produce error code 2."""
with pytest.raises(SystemExit, match="^2$"):
cli.get_config(base_arguments + ["-c", config_file])


def test_extra_vars_loaded(base_arguments):
"""Ensure ``extra_vars`` option is loaded from file config."""
config = cli.get_config(
base_arguments + ["-c", "test/fixtures/config-with-extra-vars.yml"]
)

assert config.extra_vars == {'foo': 'bar', 'knights_favorite_word': 'NI'}
4 changes: 4 additions & 0 deletions test/fixtures/config-with-extra-vars.yml
@@ -0,0 +1,4 @@
---
extra_vars:
foo: bar
knights_favorite_word: NI