Skip to content

Commit 195f334

Browse files
committed
Fix: preserve absolute Path object for tfdir
- Ensure `_abspath` returns the original `Path` object when it's absolute, reverting the modernization regression. - Cast `self.tfdir` to `str` before encoding to prevent `AttributeError` when cache is enabled. - Add test cases to verify both behaviors.
1 parent 4d4fca5 commit 195f334

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

test/test_tfdir_path.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import tempfile
2+
from pathlib import Path
3+
import tftest
4+
5+
def test_tfdir_as_path():
6+
with tempfile.TemporaryDirectory() as tmpdir:
7+
tmpdir_path = Path(tmpdir).resolve()
8+
tf = tftest.TerraformTest(tmpdir_path)
9+
# The regression: tf.tfdir / 'something' throws TypeError
10+
# if tf.tfdir is converted to a string.
11+
assert (tf.tfdir / 'something') == tmpdir_path / 'something'
12+
13+
def test_tfdir_as_str():
14+
with tempfile.TemporaryDirectory() as tmpdir:
15+
tf = tftest.TerraformTest(tmpdir)
16+
assert isinstance(tf.tfdir, str)
17+

tftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _abspath(self, path):
349349
"""Make relative path absolute from base dir."""
350350
path_obj = Path(path)
351351
if path_obj.is_absolute():
352-
return str(path_obj)
352+
return path
353353
return str(Path(self._basedir) / path)
354354

355355
def _dirhash(self, directory, hash, ignore_hidden=False,
@@ -429,7 +429,7 @@ def cache(self, **kwargs):
429429
return func(self, **kwargs)
430430

431431
cache_dir = self.cache_dir / \
432-
Path(sha1(self.tfdir.encode("cp037")).hexdigest()) / \
432+
Path(sha1(str(self.tfdir).encode("cp037")).hexdigest()) / \
433433
Path(func.__name__)
434434
cache_dir.mkdir(parents=True, exist_ok=True)
435435

0 commit comments

Comments
 (0)