Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Landy committed Nov 19, 2024
1 parent 8de6694 commit 78a7b82
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
16 changes: 5 additions & 11 deletions tests/test_module_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
import pytest

import traceback_with_variables as twv
from traceback_with_variables.module_alias import (
create_alias,
rm_alias,
module_name_to_path,
Path,
NoModuleError,
)
from traceback_with_variables.module_alias import create_alias, rm_alias, module_name_to_path, Path


ROOT_PATH = Path(twv.__file__).parent.parent
Expand All @@ -21,7 +15,7 @@
def test_module_name_to_path():
assert module_name_to_path(OS) == Path(os.__file__)
assert module_name_to_path(TWV) == ROOT_PATH / TWV
with pytest.raises(NoModuleError):
with pytest.raises(ModuleNotFoundError):
module_name_to_path('nonexistant_module')


Expand All @@ -36,14 +30,14 @@ def test_create_and_rm_alias():
create_alias('bad name', TWV)
assert str(e.value) == 'the alias must have only ascii lowecases, digits and underscores'

with pytest.raises(NoModuleError):
with pytest.raises(ModuleNotFoundError):
create_alias(ALIAS, 'nonexistant_module')

with pytest.raises(ValueError) as e:
create_alias(OS, TWV)
assert str(e.value) == 'a module with the alias name already exists'

with pytest.raises(NoModuleError):
with pytest.raises(ModuleNotFoundError):
rm_alias(ALIAS)

with pytest.raises(ValueError) as e:
Expand All @@ -67,7 +61,7 @@ def test_create_and_rm_alias():

# problems after we rm the alias

with pytest.raises(NoModuleError):
with pytest.raises(ModuleNotFoundError):
rm_alias(ALIAS)

# rare case of garbage in the lib dir
Expand Down
8 changes: 2 additions & 6 deletions traceback_with_variables/module_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@
VALID_CHARS = set(string.ascii_lowercase + string.digits + '_')


class NoModuleError(ValueError):
pass


def module_name_to_path(name: str) -> Path:
spec = importlib.util.find_spec(name)
if spec is None:
raise NoModuleError(name)
raise ModuleNotFoundError(name)
path = Path(spec.origin)
return path.parent if path.name == '__init__.py' else path


def module_exists(name: str) -> bool:
try:
module_name_to_path(name)
except NoModuleError:
except ModuleNotFoundError:
return False
return True

Expand Down

0 comments on commit 78a7b82

Please sign in to comment.