Better Python spawning primitives in test.script_helper, for

easier writing of unit tests and better error reporting.
This commit is contained in:
Antoine Pitrou 2010-10-08 18:05:42 +00:00
parent 0ee4c9f274
commit f51d8d3a2e
4 changed files with 68 additions and 85 deletions

View file

@ -7,8 +7,8 @@ import py_compile
from test import support
from test.script_helper import (
make_pkg, make_script, make_zip_pkg, make_zip_script, run_python,
temp_dir)
make_pkg, make_script, make_zip_pkg, make_zip_script,
assert_python_ok, assert_python_failure, temp_dir)
verbose = support.verbose
@ -98,19 +98,19 @@ class CmdLineTest(unittest.TestCase):
expected_package,
*cmd_line_switches):
run_args = cmd_line_switches + (script_name,)
exit_code, data = run_python(*run_args)
self._check_output(script_name, exit_code, data, expected_file,
rc, out, err = assert_python_ok(*run_args)
self._check_output(script_name, rc, out + err, expected_file,
expected_argv0, expected_path0, expected_package)
def _check_import_error(self, script_name, expected_msg,
*cmd_line_switches):
run_args = cmd_line_switches + (script_name,)
exit_code, data = run_python(*run_args)
rc, out, err = assert_python_failure(*run_args)
if verbose > 1:
print('Output from test script %r:' % script_name)
print(data)
print(err)
print('Expected output: %r' % expected_msg)
self.assertIn(expected_msg.encode('utf-8'), data)
self.assertIn(expected_msg.encode('utf-8'), err)
def test_basic_script(self):
with temp_dir() as script_dir:
@ -237,13 +237,12 @@ class CmdLineTest(unittest.TestCase):
pkg_dir = os.path.join(script_dir, 'test_pkg')
make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
script_name = _make_test_script(pkg_dir, 'script')
exit_code, data = run_python('-m', 'test_pkg.script')
rc, out, err = assert_python_ok('-m', 'test_pkg.script')
if verbose > 1:
print(data)
self.assertEqual(exit_code, 0)
expected = "init_argv0==%r" % '-m'
self.assertIn(expected.encode('utf-8'), data)
self._check_output(script_name, exit_code, data,
self.assertIn(expected.encode('utf-8'), out)
self._check_output(script_name, rc, out,
script_name, script_name, '', 'test_pkg')
def test_issue8202_dash_c_file_ignored(self):
@ -253,13 +252,12 @@ class CmdLineTest(unittest.TestCase):
with support.temp_cwd(path=script_dir):
with open("-c", "w") as f:
f.write("data")
exit_code, data = run_python('-c',
rc, out, err = assert_python_ok('-c',
'import sys; print("sys.path[0]==%r" % sys.path[0])')
if verbose > 1:
print(data)
self.assertEqual(exit_code, 0)
print(out)
expected = "sys.path[0]==%r" % ''
self.assertIn(expected.encode('utf-8'), data)
self.assertIn(expected.encode('utf-8'), out)
def test_issue8202_dash_m_file_ignored(self):
# Make sure a "-m" file in the current directory
@ -269,8 +267,8 @@ class CmdLineTest(unittest.TestCase):
with support.temp_cwd(path=script_dir):
with open("-m", "w") as f:
f.write("data")
exit_code, data = run_python('-m', 'other')
self._check_output(script_name, exit_code, data,
rc, out, err = assert_python_ok('-m', 'other')
self._check_output(script_name, rc, out,
script_name, script_name, '', '')
def test_main():