From 707a604b3ba661fcdd05b8ac00b113b5a3290d1e Mon Sep 17 00:00:00 2001 From: MrImpossibru Date: Thu, 26 Jan 2023 11:26:37 +0100 Subject: [PATCH] [feature] added support to pass a complex dict (#69) * [feature] added support to pass a complex dict to the tf_vars parameter * [feature] added some tests * [cleanup] * fix linting Co-authored-by: Nikolai Shmatenkov Co-authored-by: Ludo --- test/test_args.py | 9 +++++++++ tftest.py | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/test/test_args.py b/test/test_args.py index b25b1b1..d070c00 100644 --- a/test/test_args.py +++ b/test/test_args.py @@ -47,6 +47,15 @@ ({'tf_var_file': 'foo.tfvar'}, ['-var-file=foo.tfvar']), ({'tf_var_file': ['foo.tfvar', 'bar.tfvar']}, [ '-var-file=foo.tfvar', '-var-file=bar.tfvar']), + ({'tf_vars': {'text': 'text'}}, ['-var', 'text=text']), + ({'tf_vars': {'number': 0}}, ['-var', 'number=0']), + ({'tf_vars': {'bool': False}}, ['-var', 'bool=False']), + ({'tf_vars': {'dict': {'text': 'text'}}}, + ['-var', 'dict={"text": "text"}']), + ({'tf_vars': {'list': ['item1', 'item2']}}, + ['-var', 'list=["item1", "item2"]']), + ({'tf_vars': {'dict': {'list': ['item1', 'item2']}}}, + ['-var', 'dict={"list": ["item1", "item2"]}']), ) diff --git a/tftest.py b/tftest.py index 83b1af3..339b1b6 100644 --- a/tftest.py +++ b/tftest.py @@ -146,7 +146,10 @@ def parse_args(init_vars=None, tf_vars=None, targets=None, **kw): if tf_vars: cmd_args += list( itertools.chain.from_iterable( - ("-var", "{}={}".format(k, v)) for k, v in tf_vars.items())) + ("-var", + "{}={}".format(k, json.dumps(v) if isinstance(v, (dict, list)) else v)) + for k, v in tf_vars.items() + )) if targets: cmd_args += [("-target={}".format(t)) for t in targets] if kw.get('tf_var_file'):