Skip to content

Commit 751a0e3

Browse files
committed
fix(page_package): apply the same search order to package scripts
- the script lookup had the same all-at-once shape as the package lookup - a script present in script_dir and an additional dir no longer raises - cover file matches, not just directory matches
1 parent 6e58d9e commit 751a0e3

2 files changed

Lines changed: 56 additions & 14 deletions

File tree

‎src/osw/controller/page_package.py‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -689,21 +689,32 @@ def recursive(
689689
if params.read_listed_pages_from_script:
690690
search_paths = [params.script_dir]
691691
search_paths.extend(params.additional_script_dirs)
692-
try:
693-
script_path = find_package_dir(
694-
f"{package_to_process}.py", search_paths
695-
)
696-
package_script = read_package_script_file(script_path)
697-
new_listed_pages = get_listed_pages_from_package_script(
698-
package_script
699-
)
700-
required_packages = get_required_packages_from_package_script(
701-
package_script
692+
# Prefer the script in script_dir over the additional dirs,
693+
# instead of failing on a script present in several.
694+
script_path = find_first_package_dir(
695+
f"{package_to_process}.py", search_paths
696+
)
697+
new_listed_pages = []
698+
required_packages = []
699+
if script_path is None:
700+
warn(
701+
f"Package script for {package_to_process} not found in any "
702+
f"of the search paths: {search_paths}"
702703
)
703-
except Exception as e:
704-
warn(f"Error reading package script for {package_to_process}: {e}")
705-
new_listed_pages = []
706-
required_packages = []
704+
else:
705+
try:
706+
package_script = read_package_script_file(script_path)
707+
new_listed_pages = get_listed_pages_from_package_script(
708+
package_script
709+
)
710+
required_packages = get_required_packages_from_package_script(
711+
package_script
712+
)
713+
except Exception as e:
714+
warn(
715+
f"Error reading package script for "
716+
f"{package_to_process}: {e}"
717+
)
707718
else:
708719
search_paths = [params.creation_config.working_dir.parent]
709720
search_paths.extend(params.additional_package_dirs)

‎tests/test_page_package_search.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,34 @@ def test_find_package_dir_still_rejects_ambiguity(two_dirs):
6161

6262
with pytest.raises(ValueError):
6363
find_package_dir("MyPackage", [work_dir, extra_dir])
64+
65+
66+
@pytest.fixture
67+
def two_script_dirs(tmp_path):
68+
"""A script dir and an additional dir, both holding 'MyPackage.py'."""
69+
script_dir = tmp_path / "scripts"
70+
extra_dir = tmp_path / "extra_scripts"
71+
script_dir.mkdir()
72+
extra_dir.mkdir()
73+
(script_dir / "MyPackage.py").write_text("# working copy")
74+
(extra_dir / "MyPackage.py").write_text("# dependency copy")
75+
return script_dir, extra_dir
76+
77+
78+
def test_prefers_the_first_search_path_for_scripts(two_script_dirs):
79+
"""The script lookup takes the same route, so files must resolve too."""
80+
script_dir, extra_dir = two_script_dirs
81+
82+
found = find_first_package_dir("MyPackage.py", [script_dir, extra_dir])
83+
84+
assert found == script_dir / "MyPackage.py"
85+
86+
87+
def test_falls_through_to_a_later_script_dir(two_script_dirs, tmp_path):
88+
_, extra_dir = two_script_dirs
89+
empty_dir = tmp_path / "empty_scripts"
90+
empty_dir.mkdir()
91+
92+
found = find_first_package_dir("MyPackage.py", [empty_dir, extra_dir])
93+
94+
assert found == extra_dir / "MyPackage.py"

0 commit comments

Comments
 (0)