Skip to content

Commit fb29627

Browse files
committed
new context manager tests.utils.with_in(dirname) for tests
- added context manager to tests.utils - refactored test_contacts.py to use it
1 parent 6ff3cb7 commit fb29627

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

basicrta/tests/test_contacts.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import sys
66
import MDAnalysis as mda
77
import numpy as np
8-
import contextlib
98
from pathlib import Path
109
from basicrta.tests.datafiles import PDB, XTC
10+
from basicrta.tests.utils import work_in
1111
from 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
3430
def 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")
6052
def 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

basicrta/tests/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
from typing import Tuple
2+
from contextlib import contextmanager
3+
import os
24

35
import MDAnalysis as mda
46
from MDAnalysis.coordinates.memory import MemoryReader
57
import numpy as np
68

79

10+
@contextmanager
11+
def work_in(dirname):
12+
"""
13+
Context manager to temporarily change working directory.
14+
15+
Parameters
16+
----------
17+
dirname : str or Path
18+
Directory to change to temporarily
19+
20+
Examples
21+
--------
22+
>>> with work_in('/tmp'):
23+
... # Do work in /tmp
24+
... print(os.getcwd())
25+
/tmp
26+
>>> # Automatically restored to original directory
27+
"""
28+
original_cwd = os.getcwd()
29+
try:
30+
os.chdir(dirname)
31+
yield
32+
finally:
33+
os.chdir(original_cwd)
34+
35+
836
def make_Universe(
937
extras: Tuple[str] = tuple(),
1038
size: Tuple[int, int, int] = (125, 25, 5),

0 commit comments

Comments
 (0)