Skip to content

Commit

Permalink
Add method to clear cache data on a Resolver instance
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Nov 14, 2023
1 parent 3be6eb9 commit 8b0d7d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hab/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8b0d7d8

Please sign in to comment.