|
38 | 38 | from _pytest.pathlib import resolve_package_path |
39 | 39 | from _pytest.pathlib import resolve_pkg_root_and_module_name |
40 | 40 | from _pytest.pathlib import safe_exists |
| 41 | +from _pytest.pathlib import scandir |
41 | 42 | from _pytest.pathlib import spec_matches_module_path |
42 | 43 | from _pytest.pathlib import symlink_or_skip |
43 | 44 | from _pytest.pathlib import visit |
@@ -569,6 +570,29 @@ def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> N |
569 | 570 | assert getattr(module, "foo")() == 42 |
570 | 571 |
|
571 | 572 |
|
| 573 | +def test_scandir_with_non_existent_directory() -> None: |
| 574 | + # Test with a directory that does not exist |
| 575 | + non_existent_dir = "path_to_non_existent_dir" |
| 576 | + result = scandir(non_existent_dir) |
| 577 | + # Assert that the result is an empty list |
| 578 | + assert result == [] |
| 579 | + |
| 580 | + |
| 581 | +def test_scandir_handles_os_error() -> None: |
| 582 | + # Create a mock entry that will raise an OSError when is_file is called |
| 583 | + mock_entry = unittest.mock.MagicMock() |
| 584 | + mock_entry.is_file.side_effect = OSError("some permission error") |
| 585 | + # Mock os.scandir to return an iterator with our mock entry |
| 586 | + with unittest.mock.patch("os.scandir") as mock_scandir: |
| 587 | + mock_scandir.return_value.__enter__.return_value = [mock_entry] |
| 588 | + # Call the scandir function with a path |
| 589 | + # We expect an OSError to be raised here |
| 590 | + with pytest.raises(OSError, match="some permission error"): |
| 591 | + scandir("/fake/path") |
| 592 | + # Verify that the is_file method was called on the mock entry |
| 593 | + mock_entry.is_file.assert_called_once() |
| 594 | + |
| 595 | + |
572 | 596 | class TestImportLibMode: |
573 | 597 | def test_importmode_importlib_with_dataclass( |
574 | 598 | self, tmp_path: Path, ns_param: bool |
|
0 commit comments