Merge pull request #17 from beelit94/python-terraform-16

handle space or special characters in windows & linux
This commit is contained in:
beelit94 2017-08-08 15:01:52 -07:00 committed by GitHub
commit 5213202861
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 += 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

View file

@ -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