MCPcopy
hub / github.com/pytest-dev/pytest / test_samefile_false_negatives

Function test_samefile_false_negatives

testing/test_pathlib.py:552–570  ·  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

550
551@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only")
552def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
553 """
554 import_file() should not raise ImportPathMismatchError if the paths are exactly
555 equal on Windows. It seems directories mounted as UNC paths make os.path.samefile
556 return False, even when they are clearly equal.
557 """
558 module_path = tmp_path.joinpath("my_module.py")
559 module_path.write_text("def foo(): return 42", encoding="utf-8")
560 monkeypatch.syspath_prepend(tmp_path)
561
562 with monkeypatch.context() as mp:
563 # Forcibly make os.path.samefile() return False here to ensure we are comparing
564 # the paths too. Using a context to narrow the patch as much as possible given
565 # this is an important system function.
566 mp.setattr(os.path, "samefile", lambda x, y: False)
567 module = import_path(
568 module_path, root=tmp_path, consider_namespace_packages=False
569 )
570 assert getattr(module, "foo")() == 42
571
572
573def test_scandir_with_non_existent_directory() -> None:

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected