diff --git a/testsuite/CHANGELOG b/testsuite/CHANGELOG index f11987c9226..1561dd04580 100644 --- a/testsuite/CHANGELOG +++ b/testsuite/CHANGELOG @@ -17,6 +17,8 @@ and https://github.com/MDAnalysis/mdanalysis/wiki/UnitTests * 0.14.0 + - Added the cleanup plugin (--with-mda_cleanup) to delete offset files + after tests have run (Issue 669) - Made memleak testing python 3 compliant (Issue 662). It may be a moot point since python 3.4 now cleverly deals with leaks. diff --git a/testsuite/MDAnalysisTests/plugins/__init__.py b/testsuite/MDAnalysisTests/plugins/__init__.py index a44f60b5edd..51f981bb74a 100644 --- a/testsuite/MDAnalysisTests/plugins/__init__.py +++ b/testsuite/MDAnalysisTests/plugins/__init__.py @@ -61,7 +61,7 @@ # code won't be run again under coverage's watch. # Don't forget to also add your plugin to the import further ahead -__all__ = ['memleak', 'capture_err', 'knownfailure'] +__all__ = ['memleak', 'capture_err', 'knownfailure', 'cleanup'] import distutils.version try: @@ -103,7 +103,7 @@ def _check_plugins_loaded(): loaded_plugins = dict() # ADD HERE your plugin import -from . import memleak, capture_err, knownfailure +from . import memleak, capture_err, knownfailure, cleanup plugin_classes = [] for plugin in __all__: diff --git a/testsuite/MDAnalysisTests/plugins/capture_err.py b/testsuite/MDAnalysisTests/plugins/capture_err.py index 3cc1bc9f0df..653eb737352 100644 --- a/testsuite/MDAnalysisTests/plugins/capture_err.py +++ b/testsuite/MDAnalysisTests/plugins/capture_err.py @@ -46,13 +46,13 @@ def __init__(self): self._buf = None def options(self, parser, env): - """Register commandline options + """Registers the commandline option, defaulting to enabled. """ parser.add_option( "--no-errorcapture", action="store_false", default=not env.get(self.env_opt), dest="capture_error", help="Don't capture stderr (any stderr output " - "will be printed immediately) [NOSE_NO_ERRORCAPTURE]") + "will be printed immediately) [{}]".format(self.env_opt)) def configure(self, options, conf): """Configure plugin. Plugin is enabled by default. diff --git a/testsuite/MDAnalysisTests/plugins/cleanup.py b/testsuite/MDAnalysisTests/plugins/cleanup.py new file mode 100644 index 00000000000..0a32c929e60 --- /dev/null +++ b/testsuite/MDAnalysisTests/plugins/cleanup.py @@ -0,0 +1,74 @@ +# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 +# +# MDAnalysis --- http://www.MDAnalysis.org +# Copyright (c) 2006-2015 Naveen Michaud-Agrawal, Elizabeth J. Denning, Oliver Beckstein +# and contributors (see AUTHORS for the full list) +# +# Released under the GNU Public Licence, v2 or any higher version +# +# Please cite your use of MDAnalysis in published work: +# +# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein. +# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations. +# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 +# + +"""Plugin that cleans up all .npz created during testing. +""" +# We may want to make the glob pattern an option, for extensibility. + +import nose +from nose.plugins.base import Plugin +from os import walk, unlink, path +from pkg_resources import resource_filename +import warnings + +class Cleanup(Plugin): + """Removes XDR offset files after all testing is done.""" + enabled = True + name = "mda_cleanup" + env_opt = 'NOSE_NO_MDA_CLEANUP' + score = 2000 + + def options(self, parser, env): + """Registers the commandline option, defaulting to enabled. + """ + parser.add_option("--no-%s" % self.name, + action="store_false", + dest="do_mda_cleanup", + default=not env.get(self.env_opt), + help="Disables the cleanup of MDAnalysis offset " + "(*.npz) files. [{}]".format(self.env_opt)) + + def configure(self, options, conf): + super(Cleanup, self).configure(options, conf) + self.config = conf # This will let other tests know about config settings. + try: + self.enabled = options.do_mda_cleanup + except AttributeError: + self.enabled = False + self.verbosity = options.verbosity + + def report(self, stream): + from .. import __name__ as mdatestsname + dirname = resource_filename(mdatestsname, 'data') + if self.verbosity > 0: + stream.write("Cleanup: deleting offset files in " + "{}\n".format(dirname)) + for root, dirs, fnames in walk(dirname): + for fname in fnames: + if fname.endswith('_offsets.npz'): + fullname = path.join(root, fname) + if self.verbosity > 1: + stream.write("Cleanup: deleting offset file " + "{}\n".format(fullname)) + try: + unlink(fullname) + except OSError: + warnings.warn("Cleanup couldn't delete offset file " + "{}".format(fullname)) + + +plugin_class = Cleanup +