1
1
import logging
2
2
import os
3
- import stat
3
+ from pathlib import Path
4
+ from unittest .mock import mock_open , patch
4
5
5
6
import h5py
6
7
import numpy as np
@@ -38,14 +39,15 @@ def unreadable_file(tmp_path):
38
39
"""Return a dictionary containing the file path and
39
40
expected permission for an unreadable .h5 file."""
40
41
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
+ }
49
51
50
52
51
53
@pytest .fixture
@@ -54,13 +56,21 @@ def unwriteable_file(tmp_path):
54
56
expected permission for an unwriteable .h5 file."""
55
57
unwriteable_dir = tmp_path / "no_write"
56
58
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
+ }
64
74
65
75
66
76
@pytest .fixture
0 commit comments