test_script_helper.py 5.8 KB

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