Skip to content

Commit

Permalink
Skip modules without packages (#90)
Browse files Browse the repository at this point in the history
* Skip modules without a package

* Increment version
  • Loading branch information
JamesHutchison authored May 19, 2023
1 parent aa1587b commit 90df395
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
13 changes: 13 additions & 0 deletions megamock/import_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def add_reference(
calling_module: ModuleType,
original_name: str,
named_as: str,
) -> None:
# do not bother with bad modules. Example: conftest from pytest
if not calling_module.__package__:
# this can happen if using megamock + pytest hot reloader
return
References._add_reference(module, calling_module, original_name, named_as)

@staticmethod
def _add_reference(
module: ModuleType,
calling_module: ModuleType,
original_name: str,
named_as: str,
) -> None:
module_path = module.__name__
References.references[calling_module.__name__][named_as] = ModAndName(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "megamock"
version = "0.1.0-beta.3"
version = "0.1.0-beta.4"
description = "Mega mocking capabilities - stop using dot-notated paths!"
authors = ["James Hutchison <[email protected]>"]
readme = "README.md"
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_import_references.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from megamock.import_references import References
from megamock.megamocks import MegaMock
from megamock.megapatches import MegaPatch
from megamock.megas import Mega


class TestReferences:
class TestAddReference:
def test_when_package_is_missing_do_not_add(self) -> None:
patch = MegaPatch.it(References._add_reference)

calling_module = MegaMock()
calling_module.__package__ = ""
References.add_reference(MegaMock(), calling_module, "orig", "named_as")

assert Mega(patch.mock).not_called()

0 comments on commit 90df395

Please sign in to comment.