Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 1 | """Unittests for test.script_helper. Who tests the test helper?""" |
| 2 | |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 3 | import subprocess |
| 4 | import sys |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 5 | from test import script_helper |
| 6 | import unittest |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 7 | from unittest import mock |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class 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 Stinner | 268225f | 2015-03-20 14:02:33 +0100 | [diff] [blame] | 25 | self.assertIn('command line:', error_msg) |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 26 | 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 Stinner | 268225f | 2015-03-20 14:02:33 +0100 | [diff] [blame] | 32 | self.assertIn('Process return code is 0\n', error_msg) |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 33 | self.assertIn('import sys; sys.exit(0)', error_msg, |
| 34 | msg='unexpected command line.') |
| 35 | |
Gregory P. Smith | c3493aa | 2015-02-04 17:10:19 -0800 | [diff] [blame] | 36 | @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. Smith | 7c60eb8 | 2015-02-04 17:16:30 -0800 | [diff] [blame] | 39 | 'interpreter_requires_environment', |
Gregory P. Smith | c3493aa | 2015-02-04 17:10:19 -0800 | [diff] [blame] | 40 | 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. Smith | 7c60eb8 | 2015-02-04 17:16:30 -0800 | [diff] [blame] | 58 | 'interpreter_requires_environment', |
Gregory P. Smith | c3493aa | 2015-02-04 17:10:19 -0800 | [diff] [blame] | 59 | 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. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 69 | |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 70 | class TestScriptHelperEnvironment(unittest.TestCase): |
Gregory P. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 71 | """Code coverage for interpreter_requires_environment().""" |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 72 | |
| 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. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 86 | self.assertTrue(script_helper.interpreter_requires_environment()) |
| 87 | self.assertTrue(script_helper.interpreter_requires_environment()) |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 88 | 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. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 93 | script_helper.interpreter_requires_environment() |
| 94 | self.assertFalse(script_helper.interpreter_requires_environment()) |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 95 | 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. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 99 | script_helper.interpreter_requires_environment() |
| 100 | self.assertFalse(script_helper.interpreter_requires_environment()) |
| 101 | self.assertFalse(script_helper.interpreter_requires_environment()) |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 102 | 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. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 108 | if __name__ == '__main__': |
| 109 | unittest.main() |