Repository: pytest-dev/pytest
File: testing/test_pathlib.py (Python)

Code before:
    module_path.write_text("def foo(): return 42", encoding="utf-8")
    monkeypatch.syspath_prepend(tmp_path)

    with monkeypatch.context() as mp:
        # Forcibly make os.path.samefile() return False here to ensure we are comparing
        # the paths too. Using a context to narrow the patch as much as possible given
        # this is an important system function.
        mp.setattr(os.path, "samefile", lambda x, y: False)
        module = import_path(
            module_path, root=tmp_path, consider_namespace_packages=False
        )
    assert getattr(module, "foo")() == 42


def test_scandir_with_non_existent_directory() -> None:
    # Test with a directory that does not exist
    non_existent_dir = "path_to_non_existent_dir"
    result = scandir(non_existent_dir)
    # Assert that the result is an empty list
    assert result == []


def test_scandir_handles_os_error() -> None:
    # Create a mock entry that will raise an OSError when is_file is called
    mock_entry = unittest.mock.MagicMock()
    mock_entry.is_file.side_effect = OSError("Permission denied")
    # Mock os.scandir to return an iterator with our mock entry
    with unittest.mock.patch("os.scandir") as mock_scandir:
        mock_scandir.return_value.__enter__.return_value = [mock_entry]
        # Call the scandir function with a path
        # We expect an OSError to be raised here
        with pytest.raises(OSError, match="Permission denied"):
            scandir("/fake/path")
        # Verify that the is_file method was called on the mock entry
        mock_entry.is_file.assert_called_once()


class TestImportLibMode:
    def test_importmode_importlib_with_dataclass(
        self, tmp_path: Path, ns_param: bool
    ) -> None:
        """Ensure that importlib mode works with a module containing dataclasses (#7856)."""
        fn = tmp_path.joinpath("_src/tests/test_dataclass.py")
        fn.parent.mkdir(parents=True)
        fn.write_text(
            dedent(
                """
                from dataclasses import dataclass

                @dataclass
                class Data:

Review comment (security):
```suggestion
    mock_entry.is_file.side_effect = OSError("some permission error")
```

Code after:
    module_path.write_text("def foo(): return 42", encoding="utf-8")
    monkeypatch.syspath_prepend(tmp_path)

    with monkeypatch.context() as mp:
        # Forcibly make os.path.samefile() return False here to ensure we are comparing
        # the paths too. Using a context to narrow the patch as much as possible given
        # this is an important system function.
        mp.setattr(os.path, "samefile", lambda x, y: False)
        module = import_path(
            module_path, root=tmp_path, consider_namespace_packages=False
        )
    assert getattr(module, "foo")() == 42


def test_scandir_with_non_existent_directory() -> None:
    # Test with a directory that does not exist
    non_existent_dir = "path_to_non_existent_dir"
    result = scandir(non_existent_dir)
    # Assert that the result is an empty list
    assert result == []


def test_scandir_handles_os_error() -> None:
    # Create a mock entry that will raise an OSError when is_file is called
    mock_entry = unittest.mock.MagicMock()
    mock_entry.is_file.side_effect = OSError("some permission error")
    # Mock os.scandir to return an iterator with our mock entry
    with unittest.mock.patch("os.scandir") as mock_scandir:
        mock_scandir.return_value.__enter__.return_value = [mock_entry]
        # Call the scandir function with a path
        # We expect an OSError to be raised here
        with pytest.raises(OSError, match="some permission error"):
            scandir("/fake/path")
        # Verify that the is_file method was called on the mock entry
        mock_entry.is_file.assert_called_once()


class TestImportLibMode:
    def test_importmode_importlib_with_dataclass(
        self, tmp_path: Path, ns_param: bool
    ) -> None:
        """Ensure that importlib mode works with a module containing dataclasses (#7856)."""
        fn = tmp_path.joinpath("_src/tests/test_dataclass.py")
        fn.parent.mkdir(parents=True)
        fn.write_text(
            dedent(
                """
                from dataclasses import dataclass

                @dataclass
                class Data: