Update tests/test_io_utils.py
Run Pytest with Allure and Coverage Reports / tests (push) Successful in 41s
Run Pytest with Allure and Coverage Reports / tests (push) Successful in 41s
This commit is contained in:
+10
-18
@@ -22,56 +22,48 @@ def test_write_file(tmp_path):
|
||||
|
||||
def test_cause_io_error():
|
||||
# Raises manual IOError to simulate IO failure
|
||||
with pytest.raises(IOError):
|
||||
cause_io_error()
|
||||
cause_io_error()
|
||||
|
||||
def test_file_not_found():
|
||||
# Reading non-existing file raises FileNotFoundError
|
||||
with pytest.raises(FileNotFoundError):
|
||||
read_file("nonexistent.file")
|
||||
read_file("nonexistent.file")
|
||||
|
||||
def test_permission_error(monkeypatch):
|
||||
# Patch open to raise PermissionError simulating access denial
|
||||
def raise_perm_error(*args, **kwargs):
|
||||
raise PermissionError("Permission denied")
|
||||
monkeypatch.setattr("builtins.open", raise_perm_error)
|
||||
with pytest.raises(PermissionError):
|
||||
read_file("anyfile.txt")
|
||||
read_file("anyfile.txt")
|
||||
|
||||
def test_mock_open_error(monkeypatch):
|
||||
# Mock open() to raise IOError simulating read error
|
||||
mocked_open = mock.mock_open()
|
||||
mocked_open.side_effect = IOError("Mocked IOError")
|
||||
monkeypatch.setattr("builtins.open", mocked_open)
|
||||
with pytest.raises(IOError):
|
||||
with open("file.txt", "r") as f:
|
||||
f.read()
|
||||
with open("file.txt", "r") as f:
|
||||
f.read()
|
||||
|
||||
def test_file_handle_closed_error():
|
||||
# Accessing closed file raises ValueError
|
||||
f = io.StringIO("content")
|
||||
f.close()
|
||||
with pytest.raises(ValueError):
|
||||
f.read()
|
||||
f.read()
|
||||
|
||||
def test_os_error(monkeypatch):
|
||||
# Patch os.remove to raise OSError simulating filesystem error
|
||||
def raise_os_error(path):
|
||||
raise OSError("Simulated OSError")
|
||||
monkeypatch.setattr("os.remove", raise_os_error)
|
||||
with pytest.raises(OSError):
|
||||
os.remove("file.txt")
|
||||
os.remove("file.txt")
|
||||
|
||||
def test_write_file_readonly(tmp_path):
|
||||
# Writing to read-only file raises PermissionError
|
||||
file = tmp_path / "readonly.txt"
|
||||
file.write_text("data")
|
||||
os.chmod(file, 0o444)
|
||||
with pytest.raises(PermissionError):
|
||||
with open(file, "w") as f:
|
||||
f.write("new content")
|
||||
with open(file, "w") as f:
|
||||
f.write("new content")
|
||||
|
||||
def test_file_not_found_error():
|
||||
# Raises FileNotFoundError when opening a non-existent file
|
||||
with pytest.raises(FileNotFoundError):
|
||||
open("no_such_file.txt", "r")
|
||||
open("no_such_file.txt", "r")
|
||||
Reference in New Issue
Block a user