|
25 | 25 |
|
26 | 26 | import re |
27 | 27 | import os |
28 | | -import sys |
29 | 28 | import shutil |
30 | | -import subprocess |
31 | | -import time |
| 29 | +import sys |
| 30 | +from filelock import FileLock |
32 | 31 | from pathlib import Path |
33 | 32 |
|
34 | 33 | import numpy as np |
|
59 | 58 | ) |
60 | 59 |
|
61 | 60 | import MDAnalysis as mda |
62 | | -from MDAnalysis.coordinates.base import Timestep |
63 | 61 | from MDAnalysis.coordinates import XDR |
64 | 62 | from MDAnalysisTests.util import get_userid |
65 | | -from filelock import FileLock |
66 | 63 |
|
67 | 64 |
|
68 | 65 | @pytest.mark.parametrize( |
@@ -320,7 +317,7 @@ def test_with_statement(self): |
320 | 317 | with XTCReader(XTC) as trj: |
321 | 318 | N = trj.n_frames |
322 | 319 | frames = [ts.frame for ts in trj] |
323 | | - except: |
| 320 | + except Exception: |
324 | 321 | raise AssertionError("with_statement not working for XTCReader") |
325 | 322 | assert_equal( |
326 | 323 | N, |
@@ -1047,16 +1044,25 @@ def test_persistent_offsets_readonly(self, tmpdir, trajectory): |
1047 | 1044 | assert_equal(os.path.exists(XDR.offsets_filename(filename)), False) |
1048 | 1045 | # check the lock file is not created as well. |
1049 | 1046 | assert_equal( |
1050 | | - os.path.exists(XDR.offsets_filename(filename, ending=".lock")), |
| 1047 | + os.path.exists(XDR.offsets_filename(filename, ending="lock")), |
1051 | 1048 | False, |
1052 | 1049 | ) |
1053 | 1050 |
|
1054 | | - @pytest.mark.skipif( |
1055 | | - sys.platform.startswith("win"), |
1056 | | - reason="The lock file only exists when it's locked in windows", |
1057 | | - ) |
1058 | | - def test_offset_lock_created(self, traj): |
1059 | | - assert os.path.exists(XDR.offsets_filename(traj, ending="lock")) |
| 1051 | + def test_offset_lock_created(self): |
| 1052 | + lock_file_path = XDR.offsets_filename(self.filename, ending="lock") |
| 1053 | + |
| 1054 | + with FileLock(lock_file_path) as lock: |
| 1055 | + # Lock acquired in context manager, so lock file should exist |
| 1056 | + assert lock.is_locked |
| 1057 | + assert os.path.exists(lock_file_path) |
| 1058 | + |
| 1059 | + # Explicitly release lock, file should be deleted on UNIX |
| 1060 | + lock.release() |
| 1061 | + assert not lock.is_locked |
| 1062 | + if not sys.platform.startswith("win"): |
| 1063 | + # As of filelock>=3.21.0, filelock explicitly deletes lockfile |
| 1064 | + # upon release on UNIX. filelock does not do that on windows. |
| 1065 | + assert not os.path.exists(lock_file_path) |
1060 | 1066 |
|
1061 | 1067 |
|
1062 | 1068 | class TestXTCReader_offsets(_GromacsReader_offsets): |
|
0 commit comments