Skip to content

Commit 384b863

Browse files
skarzissbarnea
andauthored
Fix file config extra_vars options loading (#1372)
* add test for extra_vars loading issue * fix file config only options merging Co-authored-by: Sorin Sbarnea <[email protected]>
1 parent 1dd1df6 commit 384b863

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/ansiblelint/cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,29 @@ def merge_config(file_config, cli_config: Namespace) -> Namespace:
300300
return cli_config
301301

302302
for entry in bools:
303-
x = getattr(cli_config, entry) or file_config.get(entry, False)
303+
x = getattr(cli_config, entry) or file_config.pop(entry, False)
304304
setattr(cli_config, entry, x)
305305

306306
for entry, default in scalar_map.items():
307-
x = getattr(cli_config, entry, None) or file_config.get(entry, default)
307+
x = getattr(cli_config, entry, None) or file_config.pop(entry, default)
308308
setattr(cli_config, entry, x)
309309

310310
# if either commandline parameter or config file option is set merge
311311
# with the other, if neither is set use the default
312312
for entry, default in lists_map.items():
313313
if getattr(cli_config, entry, None) or entry in file_config.keys():
314314
value = getattr(cli_config, entry, [])
315-
value.extend(file_config.get(entry, []))
315+
value.extend(file_config.pop(entry, []))
316316
else:
317317
value = default
318318
setattr(cli_config, entry, value)
319319

320320
if 'verbosity' in file_config:
321-
cli_config.verbosity = cli_config.verbosity + file_config['verbosity']
321+
cli_config.verbosity = cli_config.verbosity + file_config.pop('verbosity')
322+
323+
# merge options that can be set only via a file config
324+
for entry, value in file_config.items():
325+
setattr(cli_config, entry, value)
322326

323327
return cli_config
324328

test/TestCommandLineInvocationSameAsConfig.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,12 @@ def test_config_failure(base_arguments, config_file):
134134
"""Ensures specific config files produce error code 2."""
135135
with pytest.raises(SystemExit, match="^2$"):
136136
cli.get_config(base_arguments + ["-c", config_file])
137+
138+
139+
def test_extra_vars_loaded(base_arguments):
140+
"""Ensure ``extra_vars`` option is loaded from file config."""
141+
config = cli.get_config(
142+
base_arguments + ["-c", "test/fixtures/config-with-extra-vars.yml"]
143+
)
144+
145+
assert config.extra_vars == {'foo': 'bar', 'knights_favorite_word': 'NI'}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
extra_vars:
3+
foo: bar
4+
knights_favorite_word: NI

0 commit comments

Comments
 (0)