Skip to content

Commit

Permalink
Fix file config extra_vars options loading (#1372)
Browse files Browse the repository at this point in the history
* add test for extra_vars loading issue

* fix file config only options merging

Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
skarzi and ssbarnea committed Feb 18, 2021
1 parent 1dd1df6 commit 384b863
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
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

0 comments on commit 384b863

Please sign in to comment.