blob: 7f5d65456ce8f792010f568b7409be3e410c83bd [file] [log] [blame]
Gregory P. Smithb5684c42015-01-20 17:19:47 -08001"""Unittests for test.script_helper. Who tests the test helper?"""
2
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -08003import subprocess
4import sys
Gregory P. Smithb5684c42015-01-20 17:19:47 -08005from test import script_helper
6import unittest
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -08007from unittest import mock
Gregory P. Smithb5684c42015-01-20 17:19:47 -08008
9
10class TestScriptHelper(unittest.TestCase):
11 def test_assert_python_expect_success(self):
12 t = script_helper._assert_python(True, '-c', 'import sys; sys.exit(0)')
13 self.assertEqual(0, t[0], 'return code was not 0')
14
15 def test_assert_python_expect_failure(self):
16 # I didn't import the sys module so this child will fail.
17 rc, out, err = script_helper._assert_python(False, '-c', 'sys.exit(0)')
18 self.assertNotEqual(0, rc, 'return code should not be 0')
19
20 def test_assert_python_raises_expect_success(self):
21 # I didn't import the sys module so this child will fail.
22 with self.assertRaises(AssertionError) as error_context:
23 script_helper._assert_python(True, '-c', 'sys.exit(0)')
24 error_msg = str(error_context.exception)
Victor Stinner268225f2015-03-20 14:02:33 +010025 self.assertIn('command line:', error_msg)
Gregory P. Smithb5684c42015-01-20 17:19:47 -080026 self.assertIn('sys.exit(0)', error_msg, msg='unexpected command line')
27
28 def test_assert_python_raises_expect_failure(self):
29 with self.assertRaises(AssertionError) as error_context:
30 script_helper._assert_python(False, '-c', 'import sys; sys.exit(0)')
31 error_msg = str(error_context.exception)
Victor Stinner268225f2015-03-20 14:02:33 +010032 self.assertIn('Process return code is 0\n', error_msg)
Gregory P. Smithb5684c42015-01-20 17:19:47 -080033 self.assertIn('import sys; sys.exit(0)', error_msg,
34 msg='unexpected command line.')
35
Gregory P. Smithc3493aa2015-02-04 17:10:19 -080036 @mock.patch('subprocess.Popen')
37 def test_assert_python_isolated_when_env_not_required(self, mock_popen):
38 with mock.patch.object(script_helper,
Gregory P. Smith7c60eb82015-02-04 17:16:30 -080039 'interpreter_requires_environment',
Gregory P. Smithc3493aa2015-02-04 17:10:19 -080040 return_value=False) as mock_ire_func:
41 mock_popen.side_effect = RuntimeError('bail out of unittest')
42 try:
43 script_helper._assert_python(True, '-c', 'None')
44 except RuntimeError as err:
45 self.assertEqual('bail out of unittest', err.args[0])
46 self.assertEqual(1, mock_popen.call_count)
47 self.assertEqual(1, mock_ire_func.call_count)
48 popen_command = mock_popen.call_args[0][0]
49 self.assertEqual(sys.executable, popen_command[0])
50 self.assertIn('None', popen_command)
51 self.assertIn('-I', popen_command)
52 self.assertNotIn('-E', popen_command) # -I overrides this
53
54 @mock.patch('subprocess.Popen')
55 def test_assert_python_not_isolated_when_env_is_required(self, mock_popen):
56 """Ensure that -I is not passed when the environment is required."""
57 with mock.patch.object(script_helper,
Gregory P. Smith7c60eb82015-02-04 17:16:30 -080058 'interpreter_requires_environment',
Gregory P. Smithc3493aa2015-02-04 17:10:19 -080059 return_value=True) as mock_ire_func:
60 mock_popen.side_effect = RuntimeError('bail out of unittest')
61 try:
62 script_helper._assert_python(True, '-c', 'None')
63 except RuntimeError as err:
64 self.assertEqual('bail out of unittest', err.args[0])
65 popen_command = mock_popen.call_args[0][0]
66 self.assertNotIn('-I', popen_command)
67 self.assertNotIn('-E', popen_command)
68
Gregory P. Smithb5684c42015-01-20 17:19:47 -080069
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -080070class TestScriptHelperEnvironment(unittest.TestCase):
Gregory P. Smith8f2fae12015-02-04 01:04:31 -080071 """Code coverage for interpreter_requires_environment()."""
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -080072
73 def setUp(self):
74 self.assertTrue(
75 hasattr(script_helper, '__cached_interp_requires_environment'))
76 # Reset the private cached state.
77 script_helper.__dict__['__cached_interp_requires_environment'] = None
78
79 def tearDown(self):
80 # Reset the private cached state.
81 script_helper.__dict__['__cached_interp_requires_environment'] = None
82
83 @mock.patch('subprocess.check_call')
84 def test_interpreter_requires_environment_true(self, mock_check_call):
85 mock_check_call.side_effect = subprocess.CalledProcessError('', '')
Gregory P. Smith8f2fae12015-02-04 01:04:31 -080086 self.assertTrue(script_helper.interpreter_requires_environment())
87 self.assertTrue(script_helper.interpreter_requires_environment())
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -080088 self.assertEqual(1, mock_check_call.call_count)
89
90 @mock.patch('subprocess.check_call')
91 def test_interpreter_requires_environment_false(self, mock_check_call):
92 # The mocked subprocess.check_call fakes a no-error process.
Gregory P. Smith8f2fae12015-02-04 01:04:31 -080093 script_helper.interpreter_requires_environment()
94 self.assertFalse(script_helper.interpreter_requires_environment())
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -080095 self.assertEqual(1, mock_check_call.call_count)
96
97 @mock.patch('subprocess.check_call')
98 def test_interpreter_requires_environment_details(self, mock_check_call):
Gregory P. Smith8f2fae12015-02-04 01:04:31 -080099 script_helper.interpreter_requires_environment()
100 self.assertFalse(script_helper.interpreter_requires_environment())
101 self.assertFalse(script_helper.interpreter_requires_environment())
Gregory P. Smithb9a3dd92015-02-04 00:59:40 -0800102 self.assertEqual(1, mock_check_call.call_count)
103 check_call_command = mock_check_call.call_args[0][0]
104 self.assertEqual(sys.executable, check_call_command[0])
105 self.assertIn('-E', check_call_command)
106
107
Gregory P. Smithb5684c42015-01-20 17:19:47 -0800108if __name__ == '__main__':
109 unittest.main()