Skip to content

Commit bc641e0

Browse files
committed
address review
1 parent f06d6d5 commit bc641e0

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

gearbox/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ def load_commands_for_package(self, package_name, search_paths=None):
272272

273273
if not found_distribution:
274274
log.error(
275-
"Failed to load project commands with error "
276-
"``%s``, have you installed your project?" % package_name
275+
"Failed to load project commands for package '%s'. "
276+
"Have you installed your project?",
277+
package_name,
277278
)
278279

279280
@staticmethod

gearbox/utils/plugins.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
def find_local_distribution(start_dir, entry_point_group=None):
66
dir = os.path.abspath(start_dir)
77
while True:
8-
for dist in importlib.metadata.distributions(path=[dir]):
8+
try:
9+
distributions = importlib.metadata.distributions(path=[dir])
10+
except (OSError, PermissionError):
11+
distributions = ()
12+
13+
for dist in distributions:
914
if entry_point_group is None or any(
1015
ep.group == entry_point_group for ep in dist.entry_points
1116
):

tests/test_commands.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from gearbox.commands.serve import ServeCommand
1212
from gearbox.commands.setup_app import SetupAppCommand
1313
from gearbox.main import GearBox, main
14+
from gearbox.utils.plugins import find_local_distribution
1415

1516

1617
def _write_dist_info(
@@ -477,6 +478,54 @@ def test_run_continues_when_project_distribution_is_missing():
477478
run_subcommand.assert_called_once_with(["help"])
478479

479480

481+
def test_find_local_distribution_continues_after_unreadable_directory(tmp_path):
482+
project_root = tmp_path / "project"
483+
nested_dir = project_root / "app" / "pkg"
484+
nested_dir.mkdir(parents=True)
485+
486+
plugin_ep = MagicMock(group="gearbox.plugins")
487+
dist = MagicMock()
488+
dist.entry_points = [plugin_ep]
489+
490+
def fake_distributions(path):
491+
scan_dir = path[0]
492+
if scan_dir == str(nested_dir):
493+
raise PermissionError("denied")
494+
if scan_dir == str(project_root):
495+
return [dist]
496+
return []
497+
498+
with patch(
499+
"gearbox.utils.plugins.importlib.metadata.distributions",
500+
side_effect=fake_distributions,
501+
):
502+
found_dist, found_path = find_local_distribution(
503+
str(nested_dir), "gearbox.plugins"
504+
)
505+
506+
assert found_dist is dist
507+
assert found_path == str(project_root)
508+
509+
510+
def test_load_commands_for_package_logs_context_when_distribution_missing(caplog):
511+
app = GearBox()
512+
513+
with patch(
514+
"importlib.metadata.packages_distributions",
515+
return_value={"tg": ["TurboGears2.devtools"]},
516+
), patch(
517+
"importlib.metadata.distribution",
518+
side_effect=importlib.metadata.PackageNotFoundError("missing"),
519+
):
520+
with caplog.at_level("ERROR", logger="gearbox"):
521+
app.load_commands_for_package("tg.devtools")
522+
523+
assert (
524+
"Failed to load project commands for package 'tg.devtools'" in caplog.text
525+
)
526+
assert "Have you installed your project?" in caplog.text
527+
528+
480529
def test_version_option_uses_gearbox_version_fallback(capsys, monkeypatch):
481530
monkeypatch.setattr(GearBox, "VERSION", "unknown-test")
482531
app = GearBox()

0 commit comments

Comments
 (0)