55import sys
66import MDAnalysis as mda
77import numpy as np
8- import contextlib
98from pathlib import Path
109from basicrta .tests .datafiles import PDB , XTC
10+ from basicrta .tests .utils import work_in
1111from basicrta .contacts import MapContacts
1212
1313@pytest .fixture (scope = "module" )
@@ -18,13 +18,9 @@ def setup_mapcontacts(tmp_path_factory):
1818
1919 tmp_path_factory .mktemp ("data" )
2020 datadir = tmp_path_factory .mktemp ("data" )
21- prev_cwd = Path .cwd ()
22- os .chdir (datadir )
2321
24- try :
22+ with work_in ( datadir ) :
2523 MapContacts (u , P88 , chol , nslices = 1 ).run ()
26- finally :
27- os .chdir (prev_cwd )
2824
2925 fn = datadir / "contacts_max10.0.pkl"
3026 assert fn .exists (), "Failed to locate {str(fn)}"
@@ -33,14 +29,10 @@ def setup_mapcontacts(tmp_path_factory):
3329@pytest .fixture
3430def setup_processcontacts (setup_mapcontacts , tmp_path_factory ):
3531 from basicrta .contacts import ProcessContacts
36- prev_cwd = Path .cwd ()
3732 datadir = setup_mapcontacts .parents [0 ]
38- os .chdir (datadir )
3933
40- try :
34+ with work_in ( datadir ) :
4135 ProcessContacts (7.0 , map_name = 'contacts_max10.0.pkl' ).run ()
42- finally :
43- os .chdir (prev_cwd )
4436
4537 fn = datadir / "contacts_7.0.pkl"
4638 assert fn .exists (), "Failed to locate {str(fn)}"
@@ -58,16 +50,16 @@ def test_mapcontacts(setup_mapcontacts):
5850
5951@pytest .mark .filterwarnings ("ignore::UserWarning" )
6052def test_max_cutoff (tmp_path ):
61- os . chdir (tmp_path )
62- u = mda .Universe (PDB , XTC )
63- P88 = u .select_atoms ('resname PRO and resid 88' )
64- chol = u .select_atoms ('resname CHOL and resid 309' )
65- MapContacts (u , P88 , chol , nslices = 1 , max_cutoff = 12.0 ).run ()
53+ with work_in (tmp_path ):
54+ u = mda .Universe (PDB , XTC )
55+ P88 = u .select_atoms ('resname PRO and resid 88' )
56+ chol = u .select_atoms ('resname CHOL and resid 309' )
57+ MapContacts (u , P88 , chol , nslices = 1 , max_cutoff = 12.0 ).run ()
6658
67- with open ('contacts_max12.0.pkl' , 'rb' ) as p :
68- contacts = pickle .load (p )
59+ with open ('contacts_max12.0.pkl' , 'rb' ) as p :
60+ contacts = pickle .load (p )
6961
70- assert os .path .exists ("contacts_max12.0.pkl" ), "contacts_max12.0.pkl was not generated"
62+ assert os .path .exists ("contacts_max12.0.pkl" ), "contacts_max12.0.pkl was not generated"
7163 assert len (contacts ) == 30
7264 assert (contacts [:, 0 ] == np .delete (np .arange (69 ,101 ), [4 ,5 ])).all ()
7365
0 commit comments