Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eltoder committed Mar 25, 2024
1 parent 8626cc2 commit 908f6d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/slotscheck/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from .discovery import (
AbsPath,
FailedImport,
FileNotInSysPathError,
ModuleLocated,
ModuleName,
ModuleTree,
Expand Down Expand Up @@ -162,9 +163,9 @@ def root(

try:
classes, modules = _collect(files, module, conf)
except ModuleNotFoundError as e:
except (ModuleNotFoundError, FileNotInSysPathError) as e:
print(
f"ERROR: Module '{e.name}' not found.\n\n"
f"ERROR: {e}.\n\n"
"See slotscheck.rtfd.io/en/latest/discovery.html\n"
"for help resolving common import problems."
)
Expand Down
10 changes: 8 additions & 2 deletions src/slotscheck/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def module_tree(
except BaseException as e:
return FailedImport(module, e)
if spec is None:
raise ModuleNotFoundError(f"No module named '{module}'", name=module)
raise ModuleNotFoundError(f"No module named {module!r}", name=module)
*namespaces, name = module.split(".")
location = Path(spec.origin) if spec.has_location and spec.origin else None
tree: ModuleTree
Expand Down Expand Up @@ -292,6 +292,12 @@ class ModuleLocated(NamedTuple):
expected_location: Optional[AbsPath]


class FileNotInSysPathError(Exception):
def __init__(self, file: Path) -> None:
super().__init__(f"File {str(file)!r} is not in PYTHONPATH")
self.file = file


def _is_module(p: AbsPath) -> bool:
return (p.is_file() and p.suffixes == [".py"]) or _is_package(p)

Expand All @@ -308,7 +314,7 @@ def _module_parents(
if pp in sys_path:
return
yield pp
raise ModuleNotFoundError(f"No module named '{p.stem}'", name=p.stem)
raise FileNotInSysPathError(p)


def _find_modules(
Expand Down
14 changes: 8 additions & 6 deletions tests/src/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
from importlib.util import find_spec
from pathlib import Path

Expand Down Expand Up @@ -33,22 +34,23 @@ def test_module_doesnt_exist(runner: CliRunner):
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert result.output == (
"ERROR: Module 'foo' not found.\n\n"
"ERROR: No module named 'foo'.\n\n"
"See slotscheck.rtfd.io/en/latest/discovery.html\n"
"for help resolving common import problems.\n"
)


def test_python_file_not_in_sys_path(runner: CliRunner, tmpdir):
file = tmpdir / "foo.py"
def test_python_file_not_in_sys_path(runner: CliRunner, tmp_path: Path):
file = tmp_path / "foo.py"
file.write_text('print("Hello, world!")', encoding="utf-8")
result = runner.invoke(cli, [str(file)])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert result.output == (
"ERROR: Module 'foo' not found.\n\n"
assert re.fullmatch(
"ERROR: File '.*/foo.py' is not in PYTHONPATH.\n\n"
"See slotscheck.rtfd.io/en/latest/discovery.html\n"
"for help resolving common import problems.\n"
"for help resolving common import problems.\n",
result.output,
)


Expand Down
6 changes: 4 additions & 2 deletions tests/src/test_discovery.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from pathlib import Path
from typing import FrozenSet, List, TypeVar
from unittest import mock

import pytest

from slotscheck.discovery import (
FailedImport,
FileNotInSysPathError,
Module,
ModuleLocated,
ModuleTree,
Expand Down Expand Up @@ -418,10 +420,10 @@ def test_given_init_py(self):
ModuleLocated("files.subdir.some_module.sub", location)
]

def test_given_file_not_in_sys_path(self, tmp_path):
def test_given_file_not_in_sys_path(self, tmp_path: Path):
location = tmp_path / "foo.py"
location.touch()
with pytest.raises(ModuleNotFoundError, match="foo"):
with pytest.raises(FileNotInSysPathError, match=r"foo\.py"):
list(find_modules(location))


Expand Down

0 comments on commit 908f6d4

Please sign in to comment.