Skip to content
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
nicoddemus committed Apr 2, 2021
commit 03e78ff06ce8d671631468a01ca61debd1b9e9ac
2 changes: 1 addition & 1 deletion testing/code/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def method(self):
)
path = tmp_path.joinpath("a.py")
path.write_text(str(source))
mod: Any = import_path(path)
mod: Any = import_path(path, root=tmp_path)
s2 = Source(mod.A)
assert str(source).strip() == str(s2).strip()

Expand Down
19 changes: 11 additions & 8 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from pathlib import Path
from textwrap import dedent
from types import ModuleType
from typing import Generator
from typing import Any
from typing import Generator

import pytest
from _pytest.monkeypatch import MonkeyPatch
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_renamed_dir_creates_mismatch(
tmp_path.joinpath("a").mkdir()
p = tmp_path.joinpath("a", "test_x123.py")
p.touch()
import_path(p)
import_path(p, root=tmp_path)
tmp_path.joinpath("a").rename(tmp_path.joinpath("b"))
with pytest.raises(ImportPathMismatchError):
import_path(tmp_path.joinpath("b", "test_x123.py"), root=tmp_path)
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_import_after(self, tmp_path: Path) -> None:
tmp_path.joinpath("xxxpackage", "__init__.py").touch()
mod1path = tmp_path.joinpath("xxxpackage", "module1.py")
mod1path.touch()
mod1 = import_path(mod1path, root=path1)
mod1 = import_path(mod1path, root=tmp_path)
assert mod1.__name__ == "xxxpackage.module1"
from xxxpackage import module1

Expand All @@ -225,7 +225,7 @@ def test_check_filepath_consistency(
pseudopath.touch()
mod.__file__ = str(pseudopath)
monkeypatch.setitem(sys.modules, name, mod)
newmod = import_path(p, root=Path(tmpdir))
newmod = import_path(p, root=tmp_path)
assert mod == newmod
monkeypatch.undo()
mod = ModuleType(name)
Expand All @@ -234,7 +234,7 @@ def test_check_filepath_consistency(
mod.__file__ = str(pseudopath)
monkeypatch.setitem(sys.modules, name, mod)
with pytest.raises(ImportPathMismatchError) as excinfo:
import_path(p, root=Path(tmpdir))
import_path(p, root=tmp_path)
modname, modfile, orig = excinfo.value.args
assert modname == name
assert modfile == str(pseudopath)
Expand Down Expand Up @@ -271,7 +271,8 @@ def test_invalid_path(self, tmp_path: Path) -> None:

@pytest.fixture
def simple_module(self, tmp_path: Path) -> Path:
fn = tmp_path / "mymod.py"
fn = tmp_path / "_src/tests/mymod.py"
fn.parent.mkdir(parents=True)
fn.write_text(
dedent(
"""
Expand All @@ -291,7 +292,9 @@ def test_importmode_importlib(self, simple_module: Path, tmp_path: Path) -> None
assert "_src" in sys.modules
assert "_src.tests" in sys.modules

def test_importmode_twice_is_different_module(self, simple_module: Path, tmp_path: Path) -> None:
def test_importmode_twice_is_different_module(
self, simple_module: Path, tmp_path: Path
) -> None:
"""`importlib` mode always returns a new module."""
module1 = import_path(simple_module, mode="importlib", root=tmp_path)
module2 = import_path(simple_module, mode="importlib", root=tmp_path)
Expand All @@ -312,7 +315,7 @@ def test_no_meta_path_found(
importlib.util, "spec_from_file_location", lambda *args: None
)
with pytest.raises(ImportError):
import_path(simple_module, mode="importlib", root=Path(tmpdir))
import_path(simple_module, mode="importlib", root=tmp_path)


def test_resolve_package_path(tmp_path: Path) -> None:
Expand Down