diff --git a/hab/resolver.py b/hab/resolver.py index ae67842..9250394 100644 --- a/hab/resolver.py +++ b/hab/resolver.py @@ -76,6 +76,12 @@ def __init__( # simply exit instead of running the requested command. self.dump_scripts = False + def clear_caches(self): + """Clears cached resolved data so it is re-generated on next use.""" + logger.debug("Resolver cache cleared.") + self._configs = None + self._distros = None + def closest_config(self, path, default=False): """Returns the most specific leaf or the tree root matching path. Ignoring any path names that don't exist in self.configs. diff --git a/tests/test_resolver.py b/tests/test_resolver.py index 194d84c..3622de4 100644 --- a/tests/test_resolver.py +++ b/tests/test_resolver.py @@ -693,3 +693,22 @@ def test_star_import(): # name from __all__ or make sure to import the missing object. # `AttributeError: module 'hab' has no attribute 'DistroVersion'` getattr(mdl, k) + + +def test_clear_caches(resolver): + """Test that Resolver.clear_cache works as expected.""" + # Resolver cache is empty + assert resolver._configs is None + assert resolver._distros is None + + # Populate resolver cache data + resolver.resolve("not_set") + assert isinstance(resolver._configs, dict) + assert isinstance(resolver._distros, dict) + assert len(resolver._configs) > 1 + assert len(resolver._distros) > 1 + + # Calling clear_caches resets the resolver cache + resolver.clear_caches() + assert resolver._configs is None + assert resolver._distros is None