Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,14 @@ def test_use_cache_with_new_tf_content(tf, dummy_tf_filepath):
f.write(str(uuid.uuid4()))

assert mock_execute_command.call_count == expected_call_count


@pytest.mark.parametrize("tf", [True], indirect=True)
def test_output_positional_arg(tf):
"""
Ensures output() can be called with positional arguments when caching is enabled
"""
with patch.object(tf, 'execute_command') as mock_execute:
mock_execute.return_value.out = "{}"
tf.output("my_output", use_cache=True)
assert mock_execute.call_count == 1
21 changes: 13 additions & 8 deletions tftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

__version__ = '1.8.6'
__version__ = '1.8.8'

_LOGGER = logging.getLogger('tftest')

Expand Down Expand Up @@ -411,8 +411,9 @@ def generate_cache_hash(self, method_kwargs):
default=str).encode("cp037")).hexdigest() + ".pickle"

def _cache(func):
sig = inspect.signature(func)

def cache(self, **kwargs):
def cache(self, *args, **kwargs):
"""
Runs the tftest instance method or retreives the cache value if it exists

Expand All @@ -424,16 +425,20 @@ def cache(self, **kwargs):
_LOGGER.info("Cache decorated method: %s", func.__name__)

if not self.enable_cache:
return func(self, **kwargs)
elif not kwargs.get("use_cache", False):
return func(self, **kwargs)
return func(self, *args, **kwargs)

method_args = sig.bind(self, *args, **kwargs).arguments
method_args.pop('self', None)

if not method_args.get("use_cache", False):
return func(self, *args, **kwargs)

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

hash_filename = self.generate_cache_hash(kwargs)
hash_filename = self.generate_cache_hash(method_args)
cache_key = cache_dir / hash_filename
_LOGGER.debug("Cache key: %s", cache_key)

Expand All @@ -446,12 +451,12 @@ def cache(self, **kwargs):
return pickle.load(f)

_LOGGER.info("Running command")
out = func(self, **kwargs)
out = func(self, *args, **kwargs)

if out:
# the hash value will now include any changes
# to the tfdir directory
hash_filename = self.generate_cache_hash(kwargs)
hash_filename = self.generate_cache_hash(method_args)
cache_key = cache_dir / hash_filename
_LOGGER.debug("Cache key: %s", cache_key)

Expand Down
Loading