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)
| 550 | |
| 551 | @pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only") |
| 552 | def 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 | |
| 573 | def test_scandir_with_non_existent_directory() -> None: |
nothing calls this directly
no test coverage detected