Author: bugman Date: Tue Aug 6 11:38:56 2013 New Revision: 20547 URL: http://svn.gna.org/viewcvs/relax?rev=20547&view=rev Log: Better handling of temporary file and directory removal in the relax test suite. The new test_suite.clean_up.deletion() function has been created from the recent method of the same name. This is used by the tearDown() method of the system, unit, and GUI tests. It should prevent rare MS Windows errors from appearing due to the OS not releasing a temporary file after a close() call. Added: trunk/test_suite/clean_up.py Modified: trunk/test_suite/gui_tests/base_classes.py trunk/test_suite/system_tests/base_classes.py trunk/test_suite/unit_tests/base_classes.py Added: trunk/test_suite/clean_up.py URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/clean_up.py?rev=20547&view=auto ============================================================================== --- trunk/test_suite/clean_up.py (added) +++ trunk/test_suite/clean_up.py Tue Aug 6 11:38:56 2013 @@ -1,0 +1,75 @@ +############################################################################### +# # +# Copyright (C) 2013 Edward d'Auvergne # +# # +# This file is part of the program relax (http://www.nmr-relax.com). # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <http://www.gnu.org/licenses/>. # +# # +############################################################################### + +# Module docstring. +"""Module of functions for cleaning up after the tests.""" + +# Python module imports. +import exceptions +from shutil import rmtree +from time import sleep + +# relax module imports. +from lib.io import delete + + +def deletion(obj=None, name=None, dir=False): + """Cleanly removing files and directories, handling WindowsErrors. + + The problem of MS Windows not releasing the file handle on a close() call is handled. This method should be resilient to the strange MS Windows behaviour of not releasing the relax state files. It should complete even when this WindowsError occurs. A delay of 3 seconds has been added when the WindowsError occurs to give the OS some time before attempting to delete the directory or file again. If this fails the deletion operation is skipped. + + + @keyword obj: The base object containing the file or directory name variable. + @type obj: Python object + @keyword name: The name of the file or directory name variable. + @type name: str + @keyword dir: A flag which if True indicates that a directory should be removed. Otherwise a file should be deleted. + """ + + # No variable present. + if not hasattr(obj, name): + return + + # The variable. + var = getattr(obj, name) + + # Non-windows systems. + if not hasattr(exceptions, 'WindowsError'): + WindowsError = None + + # Attempt to remove the file or directory as well as the variable. + try: + if dir: + rmtree(var) + else: + delete(var, fail=False) + del var + + # Handle MS Windows strangeness. + except WindowsError: + sleep(3) + try: + if dir: + rmtree(var) + else: + delete(var, fail=False) + finally: + del var Modified: trunk/test_suite/gui_tests/base_classes.py URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/gui_tests/base_classes.py?rev=20547&r1=20546&r2=20547&view=diff ============================================================================== --- trunk/test_suite/gui_tests/base_classes.py (original) +++ trunk/test_suite/gui_tests/base_classes.py Tue Aug 6 11:38:56 2013 @@ -25,7 +25,6 @@ # Python module imports. from math import pi # This is needed for relax scripts as pi is located in the relax prompt namespace. from os import sep -from shutil import rmtree from tempfile import mktemp, mkdtemp from unittest import TestCase import wx @@ -37,8 +36,8 @@ from pipe_control.reset import reset from prompt.interpreter import exec_script from lib.errors import RelaxError -from lib.io import delete from status import Status; status = Status() +from test_suite.clean_up import deletion from user_functions.data import Uf_info; uf_info = Uf_info() @@ -187,37 +186,13 @@ # Flush all wx events prior to the clean up operations of this method. This prevents these events from occurring after the GUI elements have been deleted. wx.Yield() - # Remove the temporary directories. - if hasattr(ds, 'tmpdir'): - # Delete the directory. - rmtree(ds.tmpdir) + # Remove the temporary directory and variable. + deletion(obj=ds, name='tmpdir', dir=True) + deletion(obj=self, name='tmpdir', dir=True) - # Remove the variable. - del ds.tmpdir - - # Remove the temporary directories. - if hasattr(self, 'tmpdir'): - # Delete the directory. - rmtree(self.tmpdir) - - # Remove the variable. - del self.tmpdir - - # Remove temporary files. - if hasattr(ds, 'tmpfile'): - # Delete the file. - delete(ds.tmpfile, fail=False) - - # Remove the variable. - del ds.tmpfile - - # Remove temporary files. - if hasattr(self, 'tmpfile'): - # Delete the file. - delete(self.tmpfile, fail=False) - - # Remove the variable. - del self.tmpfile + # Remove temporary file and variable. + deletion(obj=ds, name='tmpfile', dir=False) + deletion(obj=self, name='tmpfile', dir=False) # Reset relax. reset() Modified: trunk/test_suite/system_tests/base_classes.py URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/system_tests/base_classes.py?rev=20547&r1=20546&r2=20547&view=diff ============================================================================== --- trunk/test_suite/system_tests/base_classes.py (original) +++ trunk/test_suite/system_tests/base_classes.py Tue Aug 6 11:38:56 2013 @@ -23,15 +23,13 @@ """Base classes for the system tests.""" # Python module imports. -from shutil import rmtree -from time import sleep from unittest import TestCase # relax module imports. from data_store import Relax_data_store; ds = Relax_data_store() from pipe_control.reset import reset from prompt.interpreter import Interpreter -from lib.io import delete +from test_suite.clean_up import deletion class SystemTestCase(TestCase): @@ -68,42 +66,12 @@ """Default tearDown operation - delete temp directories and files and reset relax.""" # Remove the temporary directory and variable. - if hasattr(ds, 'tmpdir'): - rmtree(ds.tmpdir) - del ds.tmpdir - - # Remove the temporary directory and variable. - if hasattr(self, 'tmpdir'): - rmtree(self.tmpdir) - del self.tmpdir + deletion(obj=ds, name='tmpdir', dir=True) + deletion(obj=self, name='tmpdir', dir=True) # Remove temporary file and variable. - if hasattr(ds, 'tmpfile'): - try: - delete(ds.tmpfile, fail=False) - del ds.tmpfile - - # Handle MS Windows strangeness. - except WindowsError: - sleep(3) - try: - delete(ds.tmpfile, fail=False) - finally: - del ds.tmpfile - - # Remove temporary file and variable. - if hasattr(self, 'tmpfile'): - try: - delete(self.tmpfile, fail=False) - del self.tmpfile - - # Handle MS Windows strangeness. - except WindowsError: - sleep(3) - try: - delete(ds.tmpfile, fail=False) - finally: - del ds.tmpfile + deletion(obj=ds, name='tmpfile', dir=False) + deletion(obj=self, name='tmpfile', dir=False) # Reset relax. reset() Modified: trunk/test_suite/unit_tests/base_classes.py URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/unit_tests/base_classes.py?rev=20547&r1=20546&r2=20547&view=diff ============================================================================== --- trunk/test_suite/unit_tests/base_classes.py (original) +++ trunk/test_suite/unit_tests/base_classes.py Tue Aug 6 11:38:56 2013 @@ -23,14 +23,12 @@ """Base classes for the system tests.""" # Python module imports. -from shutil import rmtree -from time import sleep from unittest import TestCase # relax module imports. from data_store import Relax_data_store; ds = Relax_data_store() from pipe_control.reset import reset -from lib.io import delete +from test_suite.clean_up import deletion class UnitTestCase(TestCase): @@ -40,42 +38,12 @@ """Default tearDown operation - delete temp directories and files and reset relax.""" # Remove the temporary directory and variable. - if hasattr(ds, 'tmpdir'): - rmtree(ds.tmpdir) - del ds.tmpdir - - # Remove the temporary directory and variable. - if hasattr(self, 'tmpdir'): - rmtree(self.tmpdir) - del self.tmpdir + deletion(obj=ds, name='tmpdir', dir=True) + deletion(obj=self, name='tmpdir', dir=True) # Remove temporary file and variable. - if hasattr(ds, 'tmpfile'): - try: - delete(ds.tmpfile, fail=False) - del ds.tmpfile - - # Handle MS Windows strangeness. - except WindowsError: - sleep(3) - try: - delete(ds.tmpfile, fail=False) - finally: - del ds.tmpfile - - # Remove temporary file and variable. - if hasattr(self, 'tmpfile'): - try: - delete(self.tmpfile, fail=False) - del self.tmpfile - - # Handle MS Windows strangeness. - except WindowsError: - sleep(3) - try: - delete(ds.tmpfile, fail=False) - finally: - del ds.tmpfile + deletion(obj=ds, name='tmpfile', dir=False) + deletion(obj=self, name='tmpfile', dir=False) # Reset relax. reset()