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