Skip to content

Commit

Permalink
* Copy files instead of creating symlinks on Windows
Browse files Browse the repository at this point in the history
* Manage temporary files in a Windows-friendly way
  • Loading branch information
andrewesweet committed Jun 2, 2021
1 parent 3f997fb commit f8b882b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 9 additions & 6 deletions test/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

def test_setup_files():
with tempfile.TemporaryDirectory() as tmpdir:
with tempfile.NamedTemporaryFile() as tmpfile:
tf = tftest.TerraformTest(tmpdir)
tf.setup(extra_files=[tmpfile.name])
assert os.path.exists(os.path.join(
try:
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
tf = tftest.TerraformTest(tmpdir)
tf.setup(extra_files=[tmpfile.name])
assert os.path.exists(os.path.join(
tmpdir, os.path.basename(tmpfile.name)))
tf = None
assert not os.path.exists(os.path.join(
tf = None
assert not os.path.exists(os.path.join(
tmpdir, os.path.basename(tmpfile.name)))
finally:
os.unlink(tmpfile.name)
11 changes: 7 additions & 4 deletions tftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ def _cleanup(cls, tfdir, filenames, deep=True):
_LOGGER.debug('cleaning up %s %s', tfdir, filenames)
for filename in filenames:
path = os.path.join(tfdir, filename)
if os.path.islink(path):
os.unlink(path)
os.unlink(path)
if not deep:
return
path = os.path.join(tfdir, '.terraform')
Expand Down Expand Up @@ -296,12 +295,15 @@ def setup(self, extra_files=None, plugin_dir=None, init_vars=None,
if os.path.isfile(link_src):
link_dst = os.path.join(self.tfdir, filename)
try:
os.symlink(link_src, link_dst)
if os.name == 'nt':
shutil.copy(link_src, link_dst)
else:
os.symlink(link_src, link_dst)
filenames.append(filename)
except FileExistsError as e: # pylint:disable=undefined-variable
_LOGGER.warning(e)
else:
_LOGGER.debug('linked %s', link_src)
filenames.append(filename)
else:
_LOGGER.warning('no such file {}'.format(link_src))
self._finalizer = weakref.finalize(
Expand All @@ -324,6 +326,7 @@ def plan(self, input=False, color=False, refresh=True, tf_vars=None, targets=Non
if not output:
return self.execute_command('plan', *cmd_args).out
with tempfile.NamedTemporaryFile() as fp:
fp.close()
cmd_args.append('-out={}'.format(fp.name))
self.execute_command('plan', *cmd_args)
result = self.execute_command('show', '-no-color', '-json', fp.name)
Expand Down

0 comments on commit f8b882b

Please sign in to comment.