From 4f20dfab754a09db66741b84a413e667b9882c78 Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 8 Aug 2017 14:58:33 -0700 Subject: [PATCH] handle space or special characters in windows & linux --- python_terraform/__init__.py | 9 ++++----- test/test_terraform.py | 12 +++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 35c6e3b..19ca603 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -192,8 +192,7 @@ class Terraform(object): cmds += ['-{k}={v}'.format(k=k, v=v)] cmds += args - cmd = ' '.join(cmds) - return cmd + return cmds def cmd(self, cmd, *args, **kwargs): """ @@ -224,8 +223,8 @@ class Terraform(object): stderr = sys.stderr stdout = sys.stdout - cmd_string = self.generate_cmd_string(cmd, *args, **kwargs) - log.debug('command: {c}'.format(c=cmd_string)) + cmds = self.generate_cmd_string(cmd, *args, **kwargs) + log.debug('command: {c}'.format(c=' '.join(cmds))) working_folder = self.working_dir if self.working_dir else None @@ -233,7 +232,7 @@ class Terraform(object): if self.is_env_vars_included: environ_vars = os.environ.copy() - p = subprocess.Popen(cmd_string, stdout=stdout, stderr=stderr, shell=True, + p = subprocess.Popen(cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars) out, err = p.communicate() ret_code = p.returncode diff --git a/test/test_terraform.py b/test/test_terraform.py index b9581d4..84ae447 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -14,6 +14,7 @@ logging.basicConfig(level=logging.DEBUG) root_logger = logging.getLogger() current_path = os.path.dirname(os.path.realpath(__file__)) +FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!" STRING_CASES = [ [ lambda x: x.generate_cmd_string('apply', 'the_folder', @@ -45,6 +46,14 @@ CMD_CASES = [ 1, 'command: terraform import -no-color aws_instance.foo i-abcd1234', '' + ], + # test with space and special character in file path + [ + lambda x: x.cmd('plan', 'var_to_output', out=FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS), + '', + 0, + '', + 'var_to_output' ] ] ] @@ -95,6 +104,7 @@ class TestTerraform(object): purge('.', '*.tfstate') purge('.', '*.terraform') + purge('.', FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS) @pytest.mark.parametrize([ "method", "expected" @@ -189,7 +199,7 @@ class TestTerraform(object): tf.init('var_to_output') tf.apply('var_to_output') result = tf.output('test_output', **param) - regex = re.compile('terraform output (-module=test2 -json|-json -module=test2) test_output') + regex = re.compile("terraform output (-module=test2 -json|-json -module=test2) test_output") log_str = string_logger() if param: assert re.search(regex, log_str), log_str