Skip to content

Commit a447abf

Browse files
authored
Make cache directory path Windows friendly (#71)
* Make cache directory path Windows friendly The existing logic for building the cache directory path assumes *nix style paths when attempting to make a unique cache dir for each test fixture and step. Paths with a Windows drive prefix on the front ("C:\...") muck this up. Pathlib's '/' operator silently covers this up (under Python 3.9 on Windows, at least). This change preserves the uniqueness logic whilst being Window friendly by taking a hash of the path to the test fixture's configuration directory `tfdir` and using that in the cache directory path construction. * Fix linting errors * Fix linting errors
1 parent 9651a05 commit a447abf

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

tftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ def _dirhash(self, directory, hash, ignore_hidden=False,
386386
"""Returns hash of directory's file contents"""
387387
assert Path(directory).is_dir()
388388
try:
389-
dir_iter = sorted(Path(directory).iterdir(), key=lambda p: str(p).lower())
389+
dir_iter = sorted(Path(directory).iterdir(),
390+
key=lambda p: str(p).lower())
390391
except FileNotFoundError:
391392
return hash
392393
for path in dir_iter:
@@ -433,7 +434,7 @@ def generate_cache_hash(self, method_kwargs):
433434
params["tfdir"] = self._dirhash(self.tfdir, sha1(), ignore_hidden=True,
434435
exclude_directories=[".terraform"],
435436
excluded_extensions=['.backup', '.tfstate'
436-
]).hexdigest()
437+
]).hexdigest()
437438

438439
return sha1(
439440
json.dumps(params, sort_keys=True,
@@ -458,7 +459,7 @@ def cache(self, **kwargs):
458459
return func(self, **kwargs)
459460

460461
cache_dir = self.cache_dir / \
461-
Path(self.tfdir.strip("/")) / Path(func.__name__)
462+
Path(sha1(self.tfdir.encode("cp037")).hexdigest()) / Path(func.__name__)
462463
cache_dir.mkdir(parents=True, exist_ok=True)
463464

464465
hash_filename = self.generate_cache_hash(kwargs)

0 commit comments

Comments
 (0)