handle space or special characters in windows & linux

This commit is contained in:
beelit94 2017-08-08 14:58:33 -07:00
parent 58c1146387
commit 4f20dfab75
2 changed files with 15 additions and 6 deletions

View file

@ -192,8 +192,7 @@ class Terraform(object):
cmds += ['-{k}={v}'.format(k=k, v=v)] cmds += ['-{k}={v}'.format(k=k, v=v)]
cmds += args cmds += args
cmd = ' '.join(cmds) return cmds
return cmd
def cmd(self, cmd, *args, **kwargs): def cmd(self, cmd, *args, **kwargs):
""" """
@ -224,8 +223,8 @@ class Terraform(object):
stderr = sys.stderr stderr = sys.stderr
stdout = sys.stdout stdout = sys.stdout
cmd_string = self.generate_cmd_string(cmd, *args, **kwargs) cmds = self.generate_cmd_string(cmd, *args, **kwargs)
log.debug('command: {c}'.format(c=cmd_string)) log.debug('command: {c}'.format(c=' '.join(cmds)))
working_folder = self.working_dir if self.working_dir else None working_folder = self.working_dir if self.working_dir else None
@ -233,7 +232,7 @@ class Terraform(object):
if self.is_env_vars_included: if self.is_env_vars_included:
environ_vars = os.environ.copy() 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) cwd=working_folder, env=environ_vars)
out, err = p.communicate() out, err = p.communicate()
ret_code = p.returncode ret_code = p.returncode

View file

@ -14,6 +14,7 @@ logging.basicConfig(level=logging.DEBUG)
root_logger = logging.getLogger() root_logger = logging.getLogger()
current_path = os.path.dirname(os.path.realpath(__file__)) current_path = os.path.dirname(os.path.realpath(__file__))
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!"
STRING_CASES = [ STRING_CASES = [
[ [
lambda x: x.generate_cmd_string('apply', 'the_folder', lambda x: x.generate_cmd_string('apply', 'the_folder',
@ -45,6 +46,14 @@ CMD_CASES = [
1, 1,
'command: terraform import -no-color aws_instance.foo i-abcd1234', '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('.', '*.tfstate')
purge('.', '*.terraform') purge('.', '*.terraform')
purge('.', FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS)
@pytest.mark.parametrize([ @pytest.mark.parametrize([
"method", "expected" "method", "expected"
@ -189,7 +199,7 @@ class TestTerraform(object):
tf.init('var_to_output') tf.init('var_to_output')
tf.apply('var_to_output') tf.apply('var_to_output')
result = tf.output('test_output', **param) 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() log_str = string_logger()
if param: if param:
assert re.search(regex, log_str), log_str assert re.search(regex, log_str), log_str