Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 1 | """Unittests for test.support.script_helper. Who tests the test helper?""" |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 2 | |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 3 | import subprocess |
| 4 | import sys |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 5 | from test.support import script_helper |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 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): |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 11 | |
| 12 | def test_assert_python_ok(self): |
| 13 | t = script_helper.assert_python_ok('-c', 'import sys; sys.exit(0)') |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 14 | self.assertEqual(0, t[0], 'return code was not 0') |
| 15 | |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 16 | def test_assert_python_failure(self): |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 17 | # I didn't import the sys module so this child will fail. |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 18 | rc, out, err = script_helper.assert_python_failure('-c', 'sys.exit(0)') |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 19 | self.assertNotEqual(0, rc, 'return code should not be 0') |
| 20 | |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 21 | def test_assert_python_ok_raises(self): |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 22 | # I didn't import the sys module so this child will fail. |
| 23 | with self.assertRaises(AssertionError) as error_context: |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 24 | script_helper.assert_python_ok('-c', 'sys.exit(0)') |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 25 | error_msg = str(error_context.exception) |
Victor Stinner | 268225f | 2015-03-20 14:02:33 +0100 | [diff] [blame] | 26 | self.assertIn('command line:', error_msg) |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 27 | self.assertIn('sys.exit(0)', error_msg, msg='unexpected command line') |
| 28 | |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 29 | def test_assert_python_failure_raises(self): |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 30 | with self.assertRaises(AssertionError) as error_context: |
Antoine Pitrou | 25f85d4 | 2015-04-13 19:41:47 +0200 | [diff] [blame] | 31 | script_helper.assert_python_failure('-c', 'import sys; sys.exit(0)') |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 32 | error_msg = str(error_context.exception) |
Victor Stinner | 268225f | 2015-03-20 14:02:33 +0100 | [diff] [blame] | 33 | self.assertIn('Process return code is 0\n', error_msg) |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 34 | self.assertIn('import sys; sys.exit(0)', error_msg, |
| 35 | msg='unexpected command line.') |
| 36 | |
Gregory P. Smith | c3493aa | 2015-02-04 17:10:19 -0800 | [diff] [blame] | 37 | @mock.patch('subprocess.Popen') |
| 38 | def test_assert_python_isolated_when_env_not_required(self, mock_popen): |
| 39 | with mock.patch.object(script_helper, |
Gregory P. Smith | 7c60eb8 | 2015-02-04 17:16:30 -0800 | [diff] [blame] | 40 | 'interpreter_requires_environment', |
Gregory P. Smith | c3493aa | 2015-02-04 17:10:19 -0800 | [diff] [blame] | 41 | return_value=False) as mock_ire_func: |
| 42 | mock_popen.side_effect = RuntimeError('bail out of unittest') |
| 43 | try: |
| 44 | script_helper._assert_python(True, '-c', 'None') |
| 45 | except RuntimeError as err: |
| 46 | self.assertEqual('bail out of unittest', err.args[0]) |
| 47 | self.assertEqual(1, mock_popen.call_count) |
| 48 | self.assertEqual(1, mock_ire_func.call_count) |
| 49 | popen_command = mock_popen.call_args[0][0] |
| 50 | self.assertEqual(sys.executable, popen_command[0]) |
| 51 | self.assertIn('None', popen_command) |
| 52 | self.assertIn('-I', popen_command) |
| 53 | self.assertNotIn('-E', popen_command) # -I overrides this |
| 54 | |
| 55 | @mock.patch('subprocess.Popen') |
| 56 | def test_assert_python_not_isolated_when_env_is_required(self, mock_popen): |
| 57 | """Ensure that -I is not passed when the environment is required.""" |
| 58 | with mock.patch.object(script_helper, |
Gregory P. Smith | 7c60eb8 | 2015-02-04 17:16:30 -0800 | [diff] [blame] | 59 | 'interpreter_requires_environment', |
Gregory P. Smith | c3493aa | 2015-02-04 17:10:19 -0800 | [diff] [blame] | 60 | return_value=True) as mock_ire_func: |
| 61 | mock_popen.side_effect = RuntimeError('bail out of unittest') |
| 62 | try: |
| 63 | script_helper._assert_python(True, '-c', 'None') |
| 64 | except RuntimeError as err: |
| 65 | self.assertEqual('bail out of unittest', err.args[0]) |
| 66 | popen_command = mock_popen.call_args[0][0] |
| 67 | self.assertNotIn('-I', popen_command) |
| 68 | self.assertNotIn('-E', popen_command) |
| 69 | |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 70 | |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 71 | class TestScriptHelperEnvironment(unittest.TestCase): |
Gregory P. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 72 | """Code coverage for interpreter_requires_environment().""" |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 73 | |
| 74 | def setUp(self): |
| 75 | self.assertTrue( |
| 76 | hasattr(script_helper, '__cached_interp_requires_environment')) |
| 77 | # Reset the private cached state. |
| 78 | script_helper.__dict__['__cached_interp_requires_environment'] = None |
| 79 | |
| 80 | def tearDown(self): |
| 81 | # Reset the private cached state. |
| 82 | script_helper.__dict__['__cached_interp_requires_environment'] = None |
| 83 | |
| 84 | @mock.patch('subprocess.check_call') |
| 85 | def test_interpreter_requires_environment_true(self, mock_check_call): |
| 86 | mock_check_call.side_effect = subprocess.CalledProcessError('', '') |
Gregory P. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 87 | self.assertTrue(script_helper.interpreter_requires_environment()) |
| 88 | self.assertTrue(script_helper.interpreter_requires_environment()) |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 89 | self.assertEqual(1, mock_check_call.call_count) |
| 90 | |
| 91 | @mock.patch('subprocess.check_call') |
| 92 | def test_interpreter_requires_environment_false(self, mock_check_call): |
| 93 | # The mocked subprocess.check_call fakes a no-error process. |
Gregory P. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 94 | script_helper.interpreter_requires_environment() |
| 95 | self.assertFalse(script_helper.interpreter_requires_environment()) |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 96 | self.assertEqual(1, mock_check_call.call_count) |
| 97 | |
| 98 | @mock.patch('subprocess.check_call') |
| 99 | def test_interpreter_requires_environment_details(self, mock_check_call): |
Gregory P. Smith | 8f2fae1 | 2015-02-04 01:04:31 -0800 | [diff] [blame] | 100 | script_helper.interpreter_requires_environment() |
| 101 | self.assertFalse(script_helper.interpreter_requires_environment()) |
| 102 | self.assertFalse(script_helper.interpreter_requires_environment()) |
Gregory P. Smith | b9a3dd9 | 2015-02-04 00:59:40 -0800 | [diff] [blame] | 103 | self.assertEqual(1, mock_check_call.call_count) |
| 104 | check_call_command = mock_check_call.call_args[0][0] |
| 105 | self.assertEqual(sys.executable, check_call_command[0]) |
| 106 | self.assertIn('-E', check_call_command) |
| 107 | |
| 108 | |
Gregory P. Smith | b5684c4 | 2015-01-20 17:19:47 -0800 | [diff] [blame] | 109 | if __name__ == '__main__': |
| 110 | unittest.main() |