|
25 | 25 |
|
26 | 26 | import re |
27 | 27 | import os |
| 28 | +import sys |
28 | 29 | import shutil |
29 | 30 | import subprocess |
| 31 | +import time |
30 | 32 | from pathlib import Path |
31 | 33 |
|
32 | 34 | import numpy as np |
|
60 | 62 | from MDAnalysis.coordinates.base import Timestep |
61 | 63 | from MDAnalysis.coordinates import XDR |
62 | 64 | from MDAnalysisTests.util import get_userid |
| 65 | +from filelock import FileLock |
63 | 66 |
|
64 | 67 |
|
65 | 68 | @pytest.mark.parametrize( |
@@ -977,45 +980,39 @@ def test_unsupported_format(self, traj): |
977 | 980 | reader = self._reader(traj) |
978 | 981 | reader[idx_frame] |
979 | 982 |
|
980 | | - @pytest.mark.skipif(get_userid() == 0, reason="cannot readonly as root") |
981 | | - def test_persistent_offsets_readonly(self, tmpdir): |
| 983 | + def test_persistent_offsets_readonly(self, tmpdir, trajectory): |
982 | 984 | shutil.copy(self.filename, str(tmpdir)) |
983 | 985 |
|
984 | | - if os.name == "nt": |
985 | | - # Windows platform has a unique way to deny write access |
986 | | - subprocess.call( |
987 | | - "icacls {fname} /deny Users:W".format(fname=tmpdir), shell=True |
| 986 | + filename = str(tmpdir.join(os.path.basename(self.filename))) |
| 987 | + print('filename', filename) |
| 988 | + ref_offset = trajectory._xdr.offsets |
| 989 | + # Mock filelock acquire to raise an error |
| 990 | + with patch.object(FileLock, "acquire", side_effect=PermissionError): # Simulate failure |
| 991 | + with pytest.warns(UserWarning, match="Cannot write lock"): |
| 992 | + reader = self._reader(filename) |
| 993 | + saved_offsets = reader._xdr.offsets |
| 994 | + |
| 995 | + # Check if offsets are handled properly and match reference offsets |
| 996 | + assert_almost_equal( |
| 997 | + saved_offsets, # Compare with reference offsets |
| 998 | + ref_offset, |
| 999 | + err_msg="error loading frame offsets", |
988 | 1000 | ) |
989 | | - else: |
990 | | - os.chmod(str(tmpdir), 0o555) |
991 | 1001 |
|
992 | | - filename = str(tmpdir.join(os.path.basename(self.filename))) |
993 | | - # try to write a offsets file |
994 | | - with pytest.warns( |
995 | | - UserWarning, match="Couldn't save offsets" |
996 | | - ) and pytest.warns(UserWarning, match="Cannot write"): |
997 | | - self._reader(filename) |
998 | 1002 | assert_equal(os.path.exists(XDR.offsets_filename(filename)), False) |
999 | 1003 | # check the lock file is not created as well. |
1000 | 1004 | assert_equal( |
1001 | 1005 | os.path.exists(XDR.offsets_filename(filename, ending=".lock")), |
1002 | 1006 | False, |
1003 | 1007 | ) |
1004 | 1008 |
|
1005 | | - # pre-teardown permission fix - leaving permission blocked dir |
1006 | | - # is problematic on py3.9 + Windows it seems. See issue |
1007 | | - # [4123](https://github.com/MDAnalysis/mdanalysis/issues/4123) |
1008 | | - # for more details. |
1009 | | - if os.name == "nt": |
1010 | | - subprocess.call(f"icacls {tmpdir} /grant Users:W", shell=True) |
1011 | | - else: |
1012 | | - os.chmod(str(tmpdir), 0o777) |
1013 | | - |
1014 | | - shutil.rmtree(tmpdir) |
1015 | | - |
1016 | | - def test_offset_lock_created(self): |
| 1009 | + @pytest.mark.skipif( |
| 1010 | + sys.platform.startswith("win"), |
| 1011 | + reason="The lock file only exists when it's locked in windows" |
| 1012 | + ) |
| 1013 | + def test_offset_lock_created(self, traj): |
1017 | 1014 | assert os.path.exists( |
1018 | | - XDR.offsets_filename(self.filename, ending="lock") |
| 1015 | + XDR.offsets_filename(traj, ending="lock") |
1019 | 1016 | ) |
1020 | 1017 |
|
1021 | 1018 |
|
|
0 commit comments