From 8b0d7d80f33fc2064bc8c1196ffeeecdb9fc7021 Mon Sep 17 00:00:00 2001 From: Mike Hendricks Date: Mon, 13 Nov 2023 18:49:44 -0800 Subject: [PATCH] Add method to clear cache data on a Resolver instance --- hab/resolver.py | 6 ++++++ tests/test_resolver.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) 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