Skip to content

Commit

Permalink
FullRepoManager cache property (Instagram#330)
Browse files Browse the repository at this point in the history
Add public cache property to FullRepoManager
  • Loading branch information
josieesh authored Jul 6, 2020
1 parent 6a5f71c commit 8523852
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libcst/metadata/full_repo_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def __init__(
self._providers = providers
self._paths: List[str] = list(paths)

@property
def cache(self) -> Dict["ProviderT", Mapping[str, object]]:
"""
The full repository cache data for all metadata providers passed in the ``providers`` parameter when
constructing :class:`~libcst.metadata.FullRepoManager`. Each provider is mapped to a mapping of path to cache.
"""
# Make sure that the cache is available to us. If resolve_cache() was called manually then this is a noop.
self.resolve_cache()
return self._cache

def resolve_cache(self) -> None:
"""
Resolve cache for all providers that require it. Normally this is called by
Expand Down
12 changes: 12 additions & 0 deletions libcst/metadata/tests/test_full_repo_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ def test_get_metadata_wrapper_with_invalid_path(self, gen_cache: Mock) -> None:
"The path needs to be in paths parameter when constructing FullRepoManager for efficient batch processing.",
):
manager.get_metadata_wrapper_for_path(path)

@patch.object(TypeInferenceProvider, "gen_cache")
def test_get_full_repo_cache(self, gen_cache: Mock) -> None:
path_prefix = "tests/pyre/simple_class"
path = f"{path_prefix}.py"
mock_cache = {
path: json.loads((Path(REPO_ROOT_DIR) / f"{path_prefix}.json").read_text())
}
gen_cache.return_value = mock_cache
manager = FullRepoManager(REPO_ROOT_DIR, path, [TypeInferenceProvider])
cache = manager.cache
self.assertEqual(cache, {TypeInferenceProvider: mock_cache})

0 comments on commit 8523852

Please sign in to comment.