MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_samefile_false_negatives

Function test_samefile_false_negatives

testing/test_pathlib.py:433–449  ·  view source on GitHub ↗

import_file() should not raise ImportPathMismatchError if the paths are exactly equal on Windows. It seems directories mounted as UNC paths make os.path.samefile return False, even when they are clearly equal.

(tmp_path: Path, monkeypatch: MonkeyPatch)

Source from the content-addressed store, hash-verified

431
432@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only")
433def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
434 """
435 import_file() should not raise ImportPathMismatchError if the paths are exactly
436 equal on Windows. It seems directories mounted as UNC paths make os.path.samefile
437 return False, even when they are clearly equal.
438 """
439 module_path = tmp_path.joinpath("my_module.py")
440 module_path.write_text("def foo(): return 42")
441 monkeypatch.syspath_prepend(tmp_path)
442
443 with monkeypatch.context() as mp:
444 # Forcibly make os.path.samefile() return False here to ensure we are comparing
445 # the paths too. Using a context to narrow the patch as much as possible given
446 # this is an important system function.
447 mp.setattr(os.path, "samefile", lambda x, y: False)
448 module = import_path(module_path, root=tmp_path)
449 assert getattr(module, "foo")() == 42
450
451
452class TestImportLibMode:

Callers

nothing calls this directly

Calls 4

import_pathFunction · 0.90
syspath_prependMethod · 0.80
contextMethod · 0.80
setattrMethod · 0.80

Tested by

no test coverage detected