Skip to content

Commit

Permalink
Make search_path required
Browse files Browse the repository at this point in the history
Summary: this diff adds a new `process_required_raw_elements` function and uses it for the non-optional raw_elements, raising InvalidConfiguration if it doesn't exist.

Reviewed By: pradeep90

Differential Revision: D47127471

fbshipit-source-id: 4f29c0f433d90f3bf35fbc1e38a34f02aea93647
  • Loading branch information
kinto0 authored and facebook-github-bot committed Jul 5, 2023
1 parent 1c962b8 commit 678d292
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def expand_and_get_existent_search_paths(
self,
) -> List[search_path_module.Element]:
site_roots = self.get_site_roots()
existent_paths = search_path_module.process_raw_elements(
existent_paths = search_path_module.process_required_raw_elements(
self.search_path, site_roots
) + search_path_module.process_raw_elements(
self.optional_search_path, site_roots
Expand Down
59 changes: 50 additions & 9 deletions client/configuration/search_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,24 @@ def assert_string_item(input: Dict[str, object], name: str) -> str:
)


def add_if_exists(elements: List[Element], element: Element) -> bool:
if os.path.exists(element.path()):
elements.append(element)
return True
return False


def process_raw_elements(
raw_elements: Iterable[RawElement], site_roots: Sequence[str]
raw_elements: Iterable[RawElement],
site_roots: Sequence[str],
) -> List[Element]:
elements: List[Element] = []

def add_if_exists(element: Element) -> bool:
if os.path.exists(element.path()):
elements.append(element)
return True
return False

for raw_element in raw_elements:
for expanded_raw_element in raw_element.expand_glob():
if isinstance(expanded_raw_element, SitePackageRawElement):
added = any(
add_if_exists(expanded_raw_element.to_element(site_root))
add_if_exists(elements, expanded_raw_element.to_element(site_root))
for site_root in site_roots
)
if not added:
Expand All @@ -242,7 +244,7 @@ def add_if_exists(element: Element) -> bool:
expanded_raw_element, (SimpleRawElement, SubdirectoryRawElement)
):
element = expanded_raw_element.to_element()
added = add_if_exists(element)
added = add_if_exists(elements, element)
if not added:
LOG.warning(f"Path does not exist for search path: {element}")
else:
Expand All @@ -251,3 +253,42 @@ def add_if_exists(element: Element) -> bool:
)

return elements


def process_required_raw_elements(
raw_elements: Iterable[RawElement],
site_roots: Sequence[str],
) -> List[Element]:
elements: List[Element] = []

for raw_element in raw_elements:
expanded_raw_elements = raw_element.expand_glob()
if len(expanded_raw_elements) == 0:
raise exceptions.InvalidConfiguration(
f"Invalid path {raw_element}: does not exist."
)
for expanded_raw_element in expanded_raw_elements:
if isinstance(expanded_raw_element, SitePackageRawElement):
added = any(
add_if_exists(elements, expanded_raw_element.to_element(site_root))
for site_root in site_roots
)
if not added:
raise exceptions.InvalidConfiguration(
f"Invalid path {expanded_raw_element.package_name}: does not exist."
)
elif isinstance(
expanded_raw_element, (SimpleRawElement, SubdirectoryRawElement)
):
element = expanded_raw_element.to_element()
added = add_if_exists(elements, element)
if not added:
raise exceptions.InvalidConfiguration(
f"Path does not exist for search path: {element}"
)
else:
raise RuntimeError(
f"Unhandled raw search path element type: {expanded_raw_element}"
)

return elements
62 changes: 62 additions & 0 deletions client/configuration/tests/search_path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..search_path import (
create_raw_element,
process_raw_elements,
process_required_raw_elements,
SimpleElement,
SimpleRawElement,
SitePackageElement,
Expand Down Expand Up @@ -202,3 +203,64 @@ def test_process_raw_elements_site_root_priority(self) -> None:
),
],
)

def test_process_required_raw_elements_existence(self) -> None:
with tempfile.TemporaryDirectory() as root:
root_path = Path(root).resolve()
ensure_directories_exists(
root_path, ["a", "b/c", "d/e/f", "venv/lib/pythonX/site-packages"]
)

self.assertListEqual(
process_required_raw_elements(
[
SimpleRawElement(str(root_path / "a")),
SubdirectoryRawElement(
root=str(root_path / "b"), subdirectory="c"
),
SitePackageRawElement(package_name="f"),
],
site_roots=[str(root_path / "d/e"), str(root_path / "u/v")],
),
[
SimpleElement(str(root_path / "a")),
SubdirectoryElement(root=str(root_path / "b"), subdirectory="c"),
SitePackageElement(
site_root=str(root_path / "d/e"), package_name="f"
),
],
)

def test_process_required_raw_elements_nonexistence(self) -> None:
with self.assertRaises(InvalidConfiguration):
process_required_raw_elements(
[
SimpleRawElement("/tmp/does-not-exist"),
],
site_roots=[],
)

def test_process_required_raw_elements_glob_nonexistence(self) -> None:
with self.assertRaises(InvalidConfiguration):
process_required_raw_elements(
[
SimpleRawElement("/tmp/does-not-exist/*"),
],
site_roots=[],
)

def test_process_required_raw_elements_subdirectory_nonexistence(self) -> None:
with self.assertRaises(InvalidConfiguration):
process_required_raw_elements(
[
SubdirectoryRawElement(root="/tmp", subdirectory="does-not-exist"),
],
site_roots=[],
)

def test_process_required_raw_elements_site_package_nonexistence(self) -> None:
with self.assertRaises(InvalidConfiguration):
process_required_raw_elements(
[SitePackageRawElement(package_name="f")],
site_roots=[],
)
2 changes: 1 addition & 1 deletion scripts/run_server_unsaved_changes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _initialize_pyre_directory(
{
"source_directories": ["."],
"typeshed": str(typeshed_path.absolute()),
"search_path": ["stubs"],
"optional_search_path": ["stubs"],
}
)
)
Expand Down

0 comments on commit 678d292

Please sign in to comment.