ยปCore Development>Code coverage>Lib/test/test_script_helper.py

Python code coverage for Lib/test/test_script_helper.py

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