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