Skip to content

Commit

Permalink
Make cache directory path Windows friendly (#71)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andrewesweet authored Apr 24, 2023
1 parent 9651a05 commit a447abf
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ def _dirhash(self, directory, hash, ignore_hidden=False,
"""Returns hash of directory's file contents"""
assert Path(directory).is_dir()
try:
dir_iter = sorted(Path(directory).iterdir(), key=lambda p: str(p).lower())
dir_iter = sorted(Path(directory).iterdir(),
key=lambda p: str(p).lower())
except FileNotFoundError:
return hash
for path in dir_iter:
Expand Down Expand Up @@ -433,7 +434,7 @@ def generate_cache_hash(self, method_kwargs):
params["tfdir"] = self._dirhash(self.tfdir, sha1(), ignore_hidden=True,
exclude_directories=[".terraform"],
excluded_extensions=['.backup', '.tfstate'
]).hexdigest()
]).hexdigest()

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

cache_dir = self.cache_dir / \
Path(self.tfdir.strip("/")) / Path(func.__name__)
Path(sha1(self.tfdir.encode("cp037")).hexdigest()) / Path(func.__name__)
cache_dir.mkdir(parents=True, exist_ok=True)

hash_filename = self.generate_cache_hash(kwargs)
Expand Down

0 comments on commit a447abf

Please sign in to comment.