Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest4: use tmpdir fixture #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions bimdp/test/conftest.py
Expand Up @@ -26,10 +26,6 @@ def pytest_configure(config):


def pytest_unconfigure(config):
# remove garbage created during tests
# note that usage of TemporaryDirectory is not enough to assure
# that all garbage is removed, expacially because we use subprocesses
shutil.rmtree(pytest.mdp_tempdirname, ignore_errors=True)
# if pp was monkey-patched, remove any stale pp4mdp directories
if hasattr(mdp.config, 'pp_monkeypatch_dirname'):
monkey_dirs = os.path.join(mdp.config.pp_monkeypatch_dirname,
Expand Down
4 changes: 0 additions & 4 deletions mdp/test/conftest.py
Expand Up @@ -26,10 +26,6 @@ def pytest_configure(config):


def pytest_unconfigure(config):
# remove garbage created during tests
# note that usage of TemporaryDirectory is not enough to assure
# that all garbage is removed, expacially because we use subprocesses
shutil.rmtree(pytest.mdp_tempdirname, ignore_errors=True)
# if pp was monkey-patched, remove any stale pp4mdp directories
if hasattr(mdp.config, 'pp_monkeypatch_dirname'):
monkey_dirs = os.path.join(mdp.config.pp_monkeypatch_dirname,
Expand Down
23 changes: 9 additions & 14 deletions mdp/test/test_caching.py
Expand Up @@ -24,7 +24,7 @@ def _execute(self, x):
return x

@requires_joblib
def test_caching_extension():
def test_caching_extension(tmpdir):
"""Test that the caching extension is working at the global level."""

global _counter
Expand All @@ -43,8 +43,7 @@ def test_caching_extension():
# reset counter
_counter = 0
# activate the extension
cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
dir=pytest.mdp_tempdirname)
cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.', dir=tmpdir)
mdp.caching.activate_caching(cachedir=cachedir)
assert mdp.get_active_extensions() == ['cache_execute']

Expand All @@ -70,12 +69,11 @@ def test_caching_extension():
assert _counter == k

@requires_joblib
def test_different_instances_same_content():
def test_different_instances_same_content(tmpdir):
global _counter
x = mdp.numx.array([[100.]], dtype='d')

cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
dir=pytest.mdp_tempdirname)
cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.', dir=tmpdir)
mdp.caching.activate_caching(cachedir=cachedir)
node = _CounterNode()
_counter = 0
Expand All @@ -101,14 +99,13 @@ def test_different_instances_same_content():
mdp.caching.deactivate_caching()

@requires_joblib
def test_caching_context_manager():
def test_caching_context_manager(tmpdir):
global _counter
node = _CounterNode()
_counter = 0

assert mdp.get_active_extensions() == []
cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
dir=pytest.mdp_tempdirname)
cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.', dir=tmpdir)
with mdp.caching.cache(cachedir=cachedir):
assert mdp.get_active_extensions() == ['cache_execute']

Expand Down Expand Up @@ -206,14 +203,12 @@ def test_preexecution_problem():
assert _counter == 1

@requires_joblib
def test_switch_cache():
def test_switch_cache(tmpdir):
"""Test changing cache directory while extension is active."""
global _counter

dir1 = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
dir=pytest.mdp_tempdirname)
dir2 = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
dir=pytest.mdp_tempdirname)
dir1 = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.', dir=tmpdir)
dir2 = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.', dir=tmpdir)
x = mdp.numx.array([[10]], dtype='d')

mdp.caching.activate_caching(cachedir=dir1)
Expand Down
10 changes: 4 additions & 6 deletions mdp/test/test_flows.py
Expand Up @@ -54,7 +54,7 @@ def test_Flow_copy_with_lambda():
generic_flow = mdp.Flow([generic_node])
generic_flow.copy()

def testFlow_save():
def testFlow_save(tmpdir):
dummy_list = [1,2,3]
flow = _get_default_flow()
flow[0].dummy_attr = dummy_list
Expand All @@ -67,8 +67,7 @@ def testFlow_save():
assert flow[0].dummy_attr != copy_flow[0].dummy_attr, \
'Flow save (string) method did not work'
# test file save
dummy_file = tempfile.mktemp(prefix='MDP_', suffix=".pic",
dir=pytest.mdp_tempdirname)
dummy_file = tempfile.mktemp(prefix='MDP_', suffix=".pic", dir=tmpdir)
flow.save(dummy_file, protocol=1)
dummy_file = open(dummy_file, 'rb')
copy_flow = pickle.load(dummy_file)
Expand Down Expand Up @@ -299,14 +298,13 @@ def testCrashRecovery():
assert isinstance(e,mdp.FlowExceptionCR)
assert not hasattr(e,'filename')

def testCrashRecoveryException():
def testCrashRecoveryException(tmpdir):
a = 3
try:
raise mdp.CrashRecoveryException('bogus errstr', a, Exception())
except mdp.CrashRecoveryException as e:
filename1 = e.dump()
filename2 = e.dump(tempfile.mkstemp(prefix='MDP_',
dir=pytest.mdp_tempdirname)[1])
filename2 = e.dump(tempfile.mkstemp(prefix='MDP_', dir=tmpdir)[1])
assert isinstance(e.parent_exception, Exception)

for fname in filename1, filename2:
Expand Down
5 changes: 2 additions & 3 deletions mdp/test/test_node_operations.py
Expand Up @@ -35,7 +35,7 @@ def test_Node_copy_with_lambdas():
generic_node.lambda_function = lambda: 1
generic_node.copy()

def test_Node_save():
def test_Node_save(tmpdir):
test_list = [1,2,3]
generic_node = mdp.Node()
generic_node.dummy_attr = test_list
Expand All @@ -48,8 +48,7 @@ def test_Node_save():
assert generic_node.dummy_attr != copy_node.dummy_attr,\
'Node save (string) method did not work'
# test file save
dummy_file = tempfile.mktemp(prefix='MDP_', suffix=".pic",
dir=pytest.mdp_tempdirname)
dummy_file = tempfile.mktemp(prefix='MDP_', suffix=".pic", dir=tmpdir)
generic_node.save(dummy_file, protocol=1)
dummy_file = open(dummy_file, 'rb')
copy_node = pickle.load(dummy_file)
Expand Down
15 changes: 0 additions & 15 deletions mdp/test/test_tempdir.py

This file was deleted.