Skip to content

Commit

Permalink
Allow passing custom environment variables to terraform (#16)
Browse files Browse the repository at this point in the history
* Add env option to to TerraformTest init method

* Remove extra line
  • Loading branch information
juliocc authored Nov 7, 2020
1 parent e43d946 commit d0258f9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,17 @@ class TerraformTest(object):
basedir: optional base directory to use for relative paths, defaults to the
directory above the one this module lives in.
terraform: path to the Terraform command.
env: a dict with custom environment variables to pass to terraform.
"""

def __init__(self, tfdir, basedir=None, terraform='terraform'):
def __init__(self, tfdir, basedir=None, terraform='terraform', env=None):
"""Set Terraform folder to operate on, and optional base directory."""
self._basedir = basedir or os.getcwd()
self.terraform = terraform
self.tfdir = self._abspath(tfdir)
self.env = os.environ.copy()
if env is not None:
self.env.update(env)

@classmethod
def _cleanup(cls, tfdir, filenames, deep=True):
Expand Down Expand Up @@ -379,7 +383,7 @@ def execute_command(self, cmd, *cmd_args):
cmdline += cmd_args
try:
p = subprocess.Popen(cmdline, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=self.tfdir, env=os.environ.copy())
stderr=subprocess.PIPE, cwd=self.tfdir, env=self.env)
except FileNotFoundError as e:
raise TerraformTestError('Terraform executable not found: %s' % e)
out, err = p.communicate()
Expand Down

0 comments on commit d0258f9

Please sign in to comment.