diff --git a/commonTools/framework/clean_workspace/clean_all_jobs.py b/commonTools/framework/clean_workspace/clean_all_jobs.py index c85a541084ae..1fa670916d2b 100755 --- a/commonTools/framework/clean_workspace/clean_all_jobs.py +++ b/commonTools/framework/clean_workspace/clean_all_jobs.py @@ -23,7 +23,7 @@ def run(self): """Run the actual routine in clean_sentinel""" try: set_reference_date() - except StandardError as e: + except BaseException as e: print(e.args, file=sys.stderr) sys.exit() except pickle.PicklingError: diff --git a/commonTools/framework/clean_workspace/unittests/test_clean_all_jobs.py b/commonTools/framework/clean_workspace/unittests/test_clean_all_jobs.py index 3c1a0930f2c7..4577551e39aa 100755 --- a/commonTools/framework/clean_workspace/unittests/test_clean_all_jobs.py +++ b/commonTools/framework/clean_workspace/unittests/test_clean_all_jobs.py @@ -8,10 +8,16 @@ import os sys.path.insert(1, os.path.dirname(os.path.dirname(__file__))) -from cStringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from io import StringIO import unittest -import mock +try: + import mock +except ImportError: + import unittest.mock as mock import pickle @@ -21,11 +27,11 @@ class TestRun(unittest.TestCase): def test_clean_run(self): - """If there is no dir attribute in the args raise SystemExit""" + """A clean run should call set_reference_date""" cleanRefInst = CleanReference() with mock.patch('clean_all_jobs.set_reference_date') as refDate: cleanRefInst.run() - refDate.assert_called_once() + refDate.assert_called_once_with() def test_pickling_exception(self): """A Pickling exception should give an error and exit""" diff --git a/commonTools/framework/clean_workspace/unittests/test_clean_sentinel.py b/commonTools/framework/clean_workspace/unittests/test_clean_sentinel.py index 28a76b84693d..6987ab12df03 100755 --- a/commonTools/framework/clean_workspace/unittests/test_clean_sentinel.py +++ b/commonTools/framework/clean_workspace/unittests/test_clean_sentinel.py @@ -11,7 +11,11 @@ import unittest -import mock +try: + import mock +except ImportError: + import unittest.mock as mock + from datetime import datetime import pickle @@ -113,12 +117,14 @@ class TestUpdateLastCleanDate(unittest.TestCase): def test_update_last_clean_date_writes(self): """Test that the current date is stored""" with mock.patch('clean_sentinel.pickle.dump') as p_save, \ + mock.patch('clean_sentinel.datetime') as dt, \ mock.patch('clean_sentinel.open') as pFile, \ mock.patch.dict('os.environ', {'WORKSPACE': '/scratch/trilinos/foo/bar'}): self.assertEqual(None, update_last_clean_date()) - p_save.assert_called_once() - pFile.assert_called_once() + p_save.assert_called_once_with(dt.now(), + pFile.return_value.__enter__()) + pFile.assert_called_once_with('/scratch/trilinos/foo/bar/lastCleanDate', 'w') def test_update_last_clean_date_raises_with_no_file(self): """Test that the current date is stored""" diff --git a/commonTools/framework/clean_workspace/unittests/test_clean_workspace.py b/commonTools/framework/clean_workspace/unittests/test_clean_workspace.py index 9172b5115469..3a50fa5a2d3d 100755 --- a/commonTools/framework/clean_workspace/unittests/test_clean_workspace.py +++ b/commonTools/framework/clean_workspace/unittests/test_clean_workspace.py @@ -8,10 +8,16 @@ import os sys.path.insert(1, os.path.dirname(os.path.dirname(__file__))) -from cStringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from io import StringIO import unittest -import mock +try: + import mock +except ImportError: + import unittest.mock as mock from argparse import Namespace from datetime import datetime @@ -28,8 +34,12 @@ def test_no_directory_raises(self): setattr(test_args, 'force_clean', False) with mock.patch.object(Cleaner, 'parse_args', return_value=test_args): cleanerInst = Cleaner() - with self.assertRaisesRegexp(SystemExit, "No directory passed - exiting!"): - cleanerInst.run() + if sys.version_info.major is not 3: + with self.assertRaisesRegexp(SystemExit, "No directory passed - exiting!"): + cleanerInst.run() + else: + with self.assertRaisesRegex(SystemExit, "No directory passed - exiting!"): + cleanerInst.run() def test_force_calls_clean(self): @@ -42,7 +52,7 @@ def test_force_calls_clean(self): with mock.patch.object(Cleaner, 'force_clean_space') as force_clean, \ mock.patch('clean_workspace.print') as m_print: cleanerInst.run() - force_clean.assert_called_once() + force_clean.assert_called_once_with() m_print.assert_called_once_with("Cleaning directory /dev/null/force_cleaned " "due to command line option") @@ -55,15 +65,19 @@ def test_dir_calls_clean_by_date(self): cleanerInst = Cleaner() with mock.patch.object(Cleaner, 'clean_space_by_date') as force_clean: cleanerInst.run() - force_clean.assert_called_once() + force_clean.assert_called_once_with() class TestParseArgs(unittest.TestCase): def test_no_args_gives_help_and_exits(self): """Test that the function does the right thing when given no arguments""" - usage_message = ('usage: programName [-h] [--force-clean] dir\n' - 'programName: error: too few arguments\n') + if sys.version_info.major is not 3: + usage_message = ('usage: programName [-h] [--force-clean] dir\n' + 'programName: error: too few arguments\n') + else: + usage_message = ('usage: programName [-h] [--force-clean] dir\n' + 'programName: error: the following arguments are required: dir\n') with self.assertRaises(SystemExit), \ mock.patch.object(sys, 'argv', ['programName']), \ mock.patch('sys.stderr', new_callable=StringIO) as cleanOut: @@ -144,8 +158,8 @@ def test_newer_reference_does_clean(self): mock.patch('clean_workspace.update_last_clean_date') as update, \ mock.patch('clean_workspace.print') as m_print: cleanerInst.clean_space_by_date() - force_clean.assert_called_once() - update.assert_called_once() + force_clean.assert_called_once_with() + update.assert_called_once_with() m_print.assert_called_once_with("Cleaning directory /dev/null/fake_directory " "due to newer reference date") @@ -162,8 +176,8 @@ def test_older_date_does_clean(self): mock.patch('clean_workspace.update_last_clean_date') as update, \ mock.patch('clean_workspace.print') as m_print: cleanerInst.clean_space_by_date() - force_clean.assert_called_once() - update.assert_called_once() + force_clean.assert_called_once_with() + update.assert_called_once_with() m_print.assert_called_once_with("Cleaning directory /dev/null/will_clean " "due to newer reference date")