summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_cmd_line_script.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2009-02-08 01:58:26 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2009-02-08 01:58:26 (GMT)
commit3f48ae35c710c5e5101d5de6721b1e2ccb56df68 (patch)
tree971feab37df37b7ab70429c75069d36b2c8b9557 /Lib/test/test_cmd_line_script.py
parentf72d9fb02f2ff536c64f1e65780b4a414eb3bb4d (diff)
downloadcpython-3f48ae35c710c5e5101d5de6721b1e2ccb56df68.zip
cpython-3f48ae35c710c5e5101d5de6721b1e2ccb56df68.tar.gz
cpython-3f48ae35c710c5e5101d5de6721b1e2ccb56df68.tar.bz2
Merged revisions 69419-69420 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r69419 | nick.coghlan | 2009-02-08 11:26:34 +1000 (Sun, 08 Feb 2009) | 1 line Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda) ........ r69420 | nick.coghlan | 2009-02-08 11:46:01 +1000 (Sun, 08 Feb 2009) | 1 line Mention patch submitter in NEWS entry for r69419 ........
Diffstat (limited to 'Lib/test/test_cmd_line_script.py')
-rw-r--r--Lib/test/test_cmd_line_script.py65
1 files changed, 64 insertions, 1 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 7fab383..e060ec1 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -113,7 +113,7 @@ def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
os.unlink(script_name)
#if verbose:
# zip_file = zipfile.ZipFile(zip_name, 'r')
- # print 'Contents of %r:' % zip_name
+ # print('Contents of %r:' % zip_name)
# zip_file.printdir()
# zip_file.close()
return zip_name, os.path.join(zip_name, script_name_in_zip)
@@ -158,6 +158,16 @@ class CmdLineTest(unittest.TestCase):
self.assert_(printed_package in data)
self.assert_(printed_argv0 in data)
+ 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)
+ if verbose:
+ print('Output from test script %r:' % script_name)
+ print(data)
+ print('Expected output: %r' % expected_msg)
+ self.assert_(expected_msg in data)
+
def test_basic_script(self):
with temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script')
@@ -182,6 +192,11 @@ class CmdLineTest(unittest.TestCase):
os.remove(script_name)
self._check_script(script_dir, compiled_name, script_dir, '')
+ def test_directory_error(self):
+ with temp_dir() as script_dir:
+ msg = "can't find '__main__.py' in %r" % script_dir
+ self._check_import_error(script_dir, msg)
+
def test_zipfile(self):
with temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__')
@@ -195,6 +210,13 @@ class CmdLineTest(unittest.TestCase):
zip_name, run_name = _make_test_zip(script_dir, 'test_zip', compiled_name)
self._check_script(zip_name, run_name, zip_name, '')
+ def test_zipfile_error(self):
+ with temp_dir() as script_dir:
+ script_name = _make_test_script(script_dir, 'not_main')
+ zip_name, run_name = _make_test_zip(script_dir, 'test_zip', script_name)
+ msg = "can't find '__main__.py' in %r" % zip_name
+ self._check_import_error(zip_name, msg)
+
def test_module_in_package(self):
with temp_dir() as script_dir:
pkg_dir = os.path.join(script_dir, 'test_pkg')
@@ -215,6 +237,47 @@ class CmdLineTest(unittest.TestCase):
launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name)
self._check_script(launch_name, run_name, run_name, 'test_pkg.test_pkg')
+ def test_package(self):
+ with temp_dir() as script_dir:
+ pkg_dir = os.path.join(script_dir, 'test_pkg')
+ _make_test_pkg(pkg_dir)
+ script_name = _make_test_script(pkg_dir, '__main__')
+ launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
+ self._check_script(launch_name, script_name,
+ script_name, 'test_pkg')
+
+ def test_package_compiled(self):
+ with temp_dir() as script_dir:
+ pkg_dir = os.path.join(script_dir, 'test_pkg')
+ _make_test_pkg(pkg_dir)
+ script_name = _make_test_script(pkg_dir, '__main__')
+ compiled_name = _compile_test_script(script_name)
+ os.remove(script_name)
+ launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
+ self._check_script(launch_name, compiled_name,
+ compiled_name, 'test_pkg')
+
+ def test_package_error(self):
+ with temp_dir() as script_dir:
+ pkg_dir = os.path.join(script_dir, 'test_pkg')
+ _make_test_pkg(pkg_dir)
+ msg = ("'test_pkg' is a package and cannot "
+ "be directly executed")
+ launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
+ self._check_import_error(launch_name, msg)
+
+ def test_package_recursion(self):
+ with temp_dir() as script_dir:
+ pkg_dir = os.path.join(script_dir, 'test_pkg')
+ _make_test_pkg(pkg_dir)
+ main_dir = os.path.join(pkg_dir, '__main__')
+ _make_test_pkg(main_dir)
+ msg = ("Cannot use package as __main__ module; "
+ "'test_pkg' is a package and cannot "
+ "be directly executed")
+ launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
+ self._check_import_error(launch_name, msg)
+
def test_main():
test.support.run_unittest(CmdLineTest)