Skip to content

Commit

Permalink
This commit allows all of the clean_workspace scripts and test to run…
Browse files Browse the repository at this point in the history
… under both pyython 2 and 3
  • Loading branch information
prwolfe committed Apr 29, 2019
1 parent 65c8735 commit d0b15b9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion commonTools/framework/clean_workspace/clean_all_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@


import unittest
import mock
try:
import mock
except ImportError:
import unittest.mock as mock

from datetime import datetime
import pickle

Expand Down Expand Up @@ -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"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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")

Expand All @@ -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:
Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand Down

0 comments on commit d0b15b9

Please sign in to comment.