Skip to content

Commit

Permalink
test_Modules.py: re-work to run under both python 2 and 3
Browse files Browse the repository at this point in the history
Note that this still fails in python 3 not due to anything we
can correct, but due to the underlying python interface to
the Modules package.
  • Loading branch information
prwolfe committed Apr 24, 2019
1 parent 9777098 commit 65c8735
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
5 changes: 3 additions & 2 deletions commonTools/framework/clean_workspace/Modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self, name=None):
self.command = self._setup_command()
self.init_file = self._module_setup(name)
if name is None and self.init_file:
execfile(self.init_file)
exec(compile(open(self.init_file, 'r').read(),
self.init_file, 'exec'))

@staticmethod
def _setup_paths():
Expand Down Expand Up @@ -144,7 +145,7 @@ def module(self, command, *arguments):
# The modules return a string of python commands to be executed on
# stdout. Execute those commands now.
if output:
exec (output) # pylint: disable=exec-used
exec(output) # pylint: disable=exec-used

# Check stderr for anything that looks like an error.
if ":ERROR:" in stderr:
Expand Down
54 changes: 39 additions & 15 deletions commonTools/framework/clean_workspace/unittests/test_Modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@

import os
import unittest
import mock
from cStringIO import StringIO
try:
import mock
except ImportError:
import unittest.mock as mock

try:
from cStringIO import StringIO
except ImportError:
from io import StringIO


sys.path.insert(1, os.path.join(os.path.dirname(__file__), os.pardir))
import Modules
Expand All @@ -23,15 +31,29 @@ def setUp(self):
modules_home = {'MODULESHOME': '/dummy/path/1'}
which_side_effects = ['/path/to/modulecmd', None, None]
find_side_effects = [None, '/fake/path/modules/init/python.py']
with mock.patch.dict(os.environ, modules_home), \
mock.patch('Modules.which',
side_effect=which_side_effects), \
mock.patch('Modules.find_first_binary',
return_value='/fake/path/modulecmd'), \
mock.patch('Modules.find_file_in_list',
side_effect=find_side_effects), \
mock.patch('__builtin__.execfile'):
self.module_obj = Modules.Module()
major_version = sys.version_info[0]
if major_version is 3:
with mock.patch.dict(os.environ, modules_home), \
mock.patch('Modules.which',
side_effect=which_side_effects), \
mock.patch('Modules.find_first_binary',
return_value='/fake/path/modulecmd'), \
mock.patch('Modules.find_file_in_list',
side_effect=find_side_effects), \
mock.patch('Modules.open'), \
mock.patch('Modules.compile', return_value=''):
self.module_obj = Modules.Module()
elif major_version is 2:
with mock.patch.dict(os.environ, modules_home), \
mock.patch('Modules.which',
side_effect=which_side_effects), \
mock.patch('Modules.find_first_binary',
return_value='/fake/path/modulecmd'), \
mock.patch('Modules.find_file_in_list',
side_effect=find_side_effects), \
mock.patch('Modules.open'), \
mock.patch('Modules.compile', return_value=''):
self.module_obj = Modules.Module()

def test_module_setup(self):
"""Test ability to instantiate the class"""
Expand All @@ -45,9 +67,10 @@ def test_module_setup(self):
return_value='/fake/path/modulecmd'), \
mock.patch('Modules.find_file_in_list',
side_effect=find_side_effects), \
mock.patch('__builtin__.execfile') as mock_exec:
mock.patch('Modules.open') as mock_open, \
mock.patch('Modules.compile', return_value=""):
result = Modules.Module()
mock_exec.assert_called_once_with(find_side_effects[-1])
mock_open.assert_called_once_with(find_side_effects[-1], 'r')
self.assertEqual('/fake/path/modulecmd', result.command)
self.assertEqual('modulecmd', result.command_name)
self.assertEqual('/fake/path/modules/init/python.py', result.init_file)
Expand All @@ -64,9 +87,10 @@ def test_module_setup_lmod(self):
return_value='/fake/path/lmodcmd'), \
mock.patch('Modules.find_file_in_list',
side_effect=find_side_effects), \
mock.patch('__builtin__.execfile') as mock_exec:
mock.patch('Modules.open') as mock_open, \
mock.patch('Modules.compile', return_value=""):
result = Modules.Module()
mock_exec.assert_not_called()
mock_open.assert_not_called()
self.assertEqual('lmodcmd', result.command)
self.assertEqual('lmodcmd', result.command_name)
self.assertEqual(None, result.init_file)
Expand Down

0 comments on commit 65c8735

Please sign in to comment.