|
11 | 11 | from gearbox.commands.serve import ServeCommand |
12 | 12 | from gearbox.commands.setup_app import SetupAppCommand |
13 | 13 | from gearbox.main import GearBox, main |
| 14 | +from gearbox.utils.plugins import find_local_distribution |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def _write_dist_info( |
@@ -477,6 +478,54 @@ def test_run_continues_when_project_distribution_is_missing(): |
477 | 478 | run_subcommand.assert_called_once_with(["help"]) |
478 | 479 |
|
479 | 480 |
|
| 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 | + |
480 | 529 | def test_version_option_uses_gearbox_version_fallback(capsys, monkeypatch): |
481 | 530 | monkeypatch.setattr(GearBox, "VERSION", "unknown-test") |
482 | 531 | app = GearBox() |
|
0 commit comments