Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1 | # tests command line execution of scripts |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 2 | |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 3 | import importlib |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 4 | import unittest |
Georg Brandl | f5247e3 | 2010-10-14 08:08:56 +0000 | [diff] [blame] | 5 | import sys |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6 | import os |
| 7 | import os.path |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 8 | import py_compile |
| 9 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 10 | from test import support |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 11 | from test.script_helper import ( |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 12 | make_pkg, make_script, make_zip_pkg, make_zip_script, |
| 13 | assert_python_ok, assert_python_failure, temp_dir) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 14 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 15 | verbose = support.verbose |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 16 | |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 17 | example_args = ['test1', 'test2', 'test3'] |
| 18 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 19 | test_source = """\ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 20 | # Script may be run with optimisation enabled, so don't rely on assert |
| 21 | # statements being executed |
| 22 | def assertEqual(lhs, rhs): |
| 23 | if lhs != rhs: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 24 | raise AssertionError('%r != %r' % (lhs, rhs)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 25 | def assertIdentical(lhs, rhs): |
| 26 | if lhs is not rhs: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 27 | raise AssertionError('%r is not %r' % (lhs, rhs)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 28 | # Check basic code execution |
| 29 | result = ['Top level assignment'] |
| 30 | def f(): |
| 31 | result.append('Lower level reference') |
| 32 | f() |
| 33 | assertEqual(result, ['Top level assignment', 'Lower level reference']) |
| 34 | # Check population of magic variables |
| 35 | assertEqual(__name__, '__main__') |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 36 | print('__file__==%a' % __file__) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 37 | assertEqual(__cached__, None) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 38 | print('__package__==%r' % __package__) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 39 | # Check the sys module |
| 40 | import sys |
| 41 | assertIdentical(globals(), sys.modules[__name__].__dict__) |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 42 | from test import test_cmd_line_script |
| 43 | example_args_list = test_cmd_line_script.example_args |
| 44 | assertEqual(sys.argv[1:], example_args_list) |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 45 | print('sys.argv[0]==%a' % sys.argv[0]) |
| 46 | print('sys.path[0]==%a' % sys.path[0]) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 47 | # Check the working directory |
| 48 | import os |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 49 | print('cwd==%a' % os.getcwd()) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 50 | """ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 51 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 52 | def _make_test_script(script_dir, script_basename, source=test_source): |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 53 | to_return = make_script(script_dir, script_basename, source) |
| 54 | importlib.invalidate_caches() |
| 55 | return to_return |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 56 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 57 | def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 58 | source=test_source, depth=1): |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 59 | to_return = make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 60 | source, depth) |
| 61 | importlib.invalidate_caches() |
| 62 | return to_return |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 63 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 64 | # There's no easy way to pass the script directory in to get |
| 65 | # -m to work (avoiding that is the whole point of making |
| 66 | # directories and zipfiles executable!) |
| 67 | # So we fake it for testing purposes with a custom launch script |
| 68 | launch_source = """\ |
| 69 | import sys, os.path, runpy |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 70 | sys.path.insert(0, %s) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 71 | runpy._run_module_as_main(%r) |
| 72 | """ |
| 73 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 74 | def _make_launch_script(script_dir, script_basename, module_name, path=None): |
| 75 | if path is None: |
| 76 | path = "os.path.dirname(__file__)" |
| 77 | else: |
| 78 | path = repr(path) |
| 79 | source = launch_source % (path, module_name) |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 80 | to_return = make_script(script_dir, script_basename, source) |
| 81 | importlib.invalidate_caches() |
| 82 | return to_return |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 83 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 84 | class CmdLineTest(unittest.TestCase): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 85 | def _check_output(self, script_name, exit_code, data, |
| 86 | expected_file, expected_argv0, |
| 87 | expected_path0, expected_package): |
| 88 | if verbose > 1: |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 89 | print("Output from test script %r:" % script_name) |
| 90 | print(data) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 91 | self.assertEqual(exit_code, 0) |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 92 | printed_file = '__file__==%a' % expected_file |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 93 | printed_package = '__package__==%r' % expected_package |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 94 | printed_argv0 = 'sys.argv[0]==%a' % expected_argv0 |
| 95 | printed_path0 = 'sys.path[0]==%a' % expected_path0 |
| 96 | printed_cwd = 'cwd==%a' % os.getcwd() |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 97 | if verbose > 1: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 98 | print('Expected output:') |
| 99 | print(printed_file) |
| 100 | print(printed_package) |
| 101 | print(printed_argv0) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 102 | print(printed_cwd) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 103 | self.assertIn(printed_file.encode('utf-8'), data) |
| 104 | self.assertIn(printed_package.encode('utf-8'), data) |
| 105 | self.assertIn(printed_argv0.encode('utf-8'), data) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 106 | self.assertIn(printed_path0.encode('utf-8'), data) |
| 107 | self.assertIn(printed_cwd.encode('utf-8'), data) |
| 108 | |
| 109 | def _check_script(self, script_name, expected_file, |
| 110 | expected_argv0, expected_path0, |
| 111 | expected_package, |
| 112 | *cmd_line_switches): |
Georg Brandl | f5247e3 | 2010-10-14 08:08:56 +0000 | [diff] [blame] | 113 | if not __debug__: |
| 114 | cmd_line_switches += ('-' + 'O' * sys.flags.optimize,) |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 115 | run_args = cmd_line_switches + (script_name,) + tuple(example_args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 116 | rc, out, err = assert_python_ok(*run_args) |
| 117 | self._check_output(script_name, rc, out + err, expected_file, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 118 | expected_argv0, expected_path0, expected_package) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 119 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 120 | def _check_import_error(self, script_name, expected_msg, |
| 121 | *cmd_line_switches): |
| 122 | run_args = cmd_line_switches + (script_name,) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 123 | rc, out, err = assert_python_failure(*run_args) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 124 | if verbose > 1: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 125 | print('Output from test script %r:' % script_name) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 126 | print(err) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 127 | print('Expected output: %r' % expected_msg) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 128 | self.assertIn(expected_msg.encode('utf-8'), err) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 129 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 130 | def test_basic_script(self): |
| 131 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 132 | script_name = _make_test_script(script_dir, 'script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 133 | self._check_script(script_name, script_name, script_name, |
| 134 | script_dir, None) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 135 | |
| 136 | def test_script_compiled(self): |
| 137 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 138 | script_name = _make_test_script(script_dir, 'script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 139 | py_compile.compile(script_name, doraise=True) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 140 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 141 | pyc_file = support.make_legacy_pyc(script_name) |
| 142 | self._check_script(pyc_file, pyc_file, |
| 143 | pyc_file, script_dir, None) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 144 | |
| 145 | def test_directory(self): |
| 146 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 147 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 148 | self._check_script(script_dir, script_name, script_dir, |
| 149 | script_dir, '') |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 150 | |
| 151 | def test_directory_compiled(self): |
| 152 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 153 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 154 | py_compile.compile(script_name, doraise=True) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 155 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 156 | pyc_file = support.make_legacy_pyc(script_name) |
| 157 | self._check_script(script_dir, pyc_file, script_dir, |
| 158 | script_dir, '') |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 159 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 160 | def test_directory_error(self): |
| 161 | with temp_dir() as script_dir: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 162 | msg = "can't find '__main__' module in %r" % script_dir |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 163 | self._check_import_error(script_dir, msg) |
| 164 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 165 | def test_zipfile(self): |
| 166 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 167 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 168 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 169 | self._check_script(zip_name, run_name, zip_name, zip_name, '') |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 170 | |
| 171 | def test_zipfile_compiled(self): |
| 172 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 173 | script_name = _make_test_script(script_dir, '__main__') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 174 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 175 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 176 | self._check_script(zip_name, run_name, zip_name, zip_name, '') |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 177 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 178 | def test_zipfile_error(self): |
| 179 | with temp_dir() as script_dir: |
| 180 | script_name = _make_test_script(script_dir, 'not_main') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 181 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
| 182 | msg = "can't find '__main__' module in %r" % zip_name |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 183 | self._check_import_error(zip_name, msg) |
| 184 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 185 | def test_module_in_package(self): |
| 186 | with temp_dir() as script_dir: |
| 187 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 188 | make_pkg(pkg_dir) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 189 | script_name = _make_test_script(pkg_dir, 'script') |
| 190 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 191 | self._check_script(launch_name, script_name, script_name, script_dir, 'test_pkg') |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 192 | |
| 193 | def test_module_in_package_in_zipfile(self): |
| 194 | with temp_dir() as script_dir: |
| 195 | zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') |
| 196 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 197 | self._check_script(launch_name, run_name, run_name, zip_name, 'test_pkg') |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 198 | |
| 199 | def test_module_in_subpackage_in_zipfile(self): |
| 200 | with temp_dir() as script_dir: |
| 201 | zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) |
| 202 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 203 | self._check_script(launch_name, run_name, run_name, zip_name, 'test_pkg.test_pkg') |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 204 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 205 | def test_package(self): |
| 206 | with temp_dir() as script_dir: |
| 207 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 208 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 209 | script_name = _make_test_script(pkg_dir, '__main__') |
| 210 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 211 | self._check_script(launch_name, script_name, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 212 | script_name, script_dir, 'test_pkg') |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 213 | |
| 214 | def test_package_compiled(self): |
| 215 | with temp_dir() as script_dir: |
| 216 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 217 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 218 | script_name = _make_test_script(pkg_dir, '__main__') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 219 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 220 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 221 | pyc_file = support.make_legacy_pyc(script_name) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 222 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 223 | self._check_script(launch_name, pyc_file, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 224 | pyc_file, script_dir, 'test_pkg') |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 225 | |
| 226 | def test_package_error(self): |
| 227 | with temp_dir() as script_dir: |
| 228 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 229 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 230 | msg = ("'test_pkg' is a package and cannot " |
| 231 | "be directly executed") |
| 232 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 233 | self._check_import_error(launch_name, msg) |
| 234 | |
| 235 | def test_package_recursion(self): |
| 236 | with temp_dir() as script_dir: |
| 237 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 238 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 239 | main_dir = os.path.join(pkg_dir, '__main__') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 240 | make_pkg(main_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 241 | msg = ("Cannot use package as __main__ module; " |
| 242 | "'test_pkg' is a package and cannot " |
| 243 | "be directly executed") |
| 244 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 245 | self._check_import_error(launch_name, msg) |
| 246 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 247 | def test_issue8202(self): |
| 248 | # Make sure package __init__ modules see "-m" in sys.argv0 while |
| 249 | # searching for the module to execute |
| 250 | with temp_dir() as script_dir: |
| 251 | with support.temp_cwd(path=script_dir): |
| 252 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
| 253 | make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])") |
| 254 | script_name = _make_test_script(pkg_dir, 'script') |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 255 | rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 256 | if verbose > 1: |
Nick Coghlan | 10ac77d | 2012-04-19 22:19:36 +1000 | [diff] [blame] | 257 | print(out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 258 | expected = "init_argv0==%r" % '-m' |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 259 | self.assertIn(expected.encode('utf-8'), out) |
| 260 | self._check_output(script_name, rc, out, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 261 | script_name, script_name, '', 'test_pkg') |
| 262 | |
| 263 | def test_issue8202_dash_c_file_ignored(self): |
| 264 | # Make sure a "-c" file in the current directory |
| 265 | # does not alter the value of sys.path[0] |
| 266 | with temp_dir() as script_dir: |
| 267 | with support.temp_cwd(path=script_dir): |
| 268 | with open("-c", "w") as f: |
| 269 | f.write("data") |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 270 | rc, out, err = assert_python_ok('-c', |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 271 | 'import sys; print("sys.path[0]==%r" % sys.path[0])') |
| 272 | if verbose > 1: |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 273 | print(out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 274 | expected = "sys.path[0]==%r" % '' |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 275 | self.assertIn(expected.encode('utf-8'), out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 276 | |
| 277 | def test_issue8202_dash_m_file_ignored(self): |
| 278 | # Make sure a "-m" file in the current directory |
| 279 | # does not alter the value of sys.path[0] |
| 280 | with temp_dir() as script_dir: |
| 281 | script_name = _make_test_script(script_dir, 'other') |
| 282 | with support.temp_cwd(path=script_dir): |
| 283 | with open("-m", "w") as f: |
| 284 | f.write("data") |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 285 | rc, out, err = assert_python_ok('-m', 'other', *example_args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 286 | self._check_output(script_name, rc, out, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 287 | script_name, script_name, '', '') |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 288 | |
| 289 | def test_main(): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 290 | support.run_unittest(CmdLineTest) |
| 291 | support.reap_children() |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 292 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 293 | if __name__ == '__main__': |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 294 | test_main() |