Skip to content

Commit 9ee30c7

Browse files
authored
Fix unreadable unwriteable fixtures for Windows (#103)
1 parent 03ddaf1 commit 9ee30c7

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

tests/conftest.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
3-
import stat
3+
from pathlib import Path
4+
from unittest.mock import mock_open, patch
45

56
import h5py
67
import numpy as np
@@ -38,14 +39,15 @@ def unreadable_file(tmp_path):
3839
"""Return a dictionary containing the file path and
3940
expected permission for an unreadable .h5 file."""
4041
file_path = tmp_path / "unreadable.h5"
41-
with open(file_path, "w") as f:
42-
f.write("unreadable data")
43-
os.chmod(f.name, not stat.S_IRUSR)
44-
yield {
45-
"file_path": file_path,
46-
"expected_permission": "r",
47-
}
48-
os.chmod(f.name, stat.S_IRUSR)
42+
file_mock = mock_open()
43+
file_mock.return_value.read.side_effect = PermissionError
44+
with patch("builtins.open", side_effect=file_mock), patch.object(
45+
Path, "exists", return_value=True
46+
):
47+
yield {
48+
"file_path": file_path,
49+
"expected_permission": "r",
50+
}
4951

5052

5153
@pytest.fixture
@@ -54,13 +56,21 @@ def unwriteable_file(tmp_path):
5456
expected permission for an unwriteable .h5 file."""
5557
unwriteable_dir = tmp_path / "no_write"
5658
unwriteable_dir.mkdir()
57-
os.chmod(unwriteable_dir, not stat.S_IWUSR)
58-
file_path = unwriteable_dir / "unwriteable.h5"
59-
yield {
60-
"file_path": file_path,
61-
"expected_permission": "w",
62-
}
63-
os.chmod(unwriteable_dir, stat.S_IWUSR)
59+
original_access = os.access
60+
61+
def mock_access(path, mode):
62+
if path == unwriteable_dir and mode == os.W_OK:
63+
return False
64+
# Ensure that the original access function is called
65+
# for all other cases
66+
return original_access(path, mode)
67+
68+
with patch("os.access", side_effect=mock_access):
69+
file_path = unwriteable_dir / "unwriteable.h5"
70+
yield {
71+
"file_path": file_path,
72+
"expected_permission": "w",
73+
}
6474

6575

6676
@pytest.fixture

0 commit comments

Comments
 (0)