Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1 | # Tests invocation of the interpreter with various command line arguments |
| 2 | # All tests are executed with environment variables ignored |
| 3 | # See test_cmd_line_script.py for testing of script execution |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 4 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame^] | 5 | import test.support, unittest |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 6 | import sys |
Walter Dörwald | 9356fb9 | 2005-11-25 15:22:10 +0000 | [diff] [blame] | 7 | import subprocess |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 9 | def _spawn_python(*args): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 10 | cmd_line = [sys.executable, '-E'] |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11 | cmd_line.extend(args) |
| 12 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 13 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 14 | |
| 15 | def _kill_python(p): |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 16 | return _kill_python_and_exit_code(p)[0] |
| 17 | |
| 18 | def _kill_python_and_exit_code(p): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 19 | p.stdin.close() |
| 20 | data = p.stdout.read() |
| 21 | p.stdout.close() |
| 22 | # try to cleanup the child so we don't appear to leak when running |
| 23 | # with regrtest -R. This should be a no-op on Windows. |
| 24 | subprocess._cleanup() |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 25 | returncode = p.wait() |
| 26 | return data, returncode |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 27 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 28 | class CmdLineTest(unittest.TestCase): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 29 | def start_python(self, *args): |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 30 | return self.start_python_and_exit_code(*args)[0] |
| 31 | |
| 32 | def start_python_and_exit_code(self, *args): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 33 | p = _spawn_python(*args) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 34 | return _kill_python_and_exit_code(p) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 35 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 36 | def exit_code(self, *args): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 37 | cmd_line = [sys.executable, '-E'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 38 | cmd_line.extend(args) |
| 39 | return subprocess.call(cmd_line, stdout=subprocess.PIPE, |
| 40 | stderr=subprocess.PIPE) |
Walter Dörwald | 9356fb9 | 2005-11-25 15:22:10 +0000 | [diff] [blame] | 41 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 42 | def test_directories(self): |
Neal Norwitz | 72c2c06 | 2006-03-09 05:58:11 +0000 | [diff] [blame] | 43 | self.assertNotEqual(self.exit_code('.'), 0) |
| 44 | self.assertNotEqual(self.exit_code('< .'), 0) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 45 | |
| 46 | def verify_valid_flag(self, cmd_line): |
| 47 | data = self.start_python(cmd_line) |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 48 | self.assertTrue(data == b'' or data.endswith(b'\n')) |
Guido van Rossum | f074b64 | 2007-07-11 06:56:16 +0000 | [diff] [blame] | 49 | self.assertTrue(b'Traceback' not in data) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 50 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 51 | def test_optimize(self): |
| 52 | self.verify_valid_flag('-O') |
| 53 | self.verify_valid_flag('-OO') |
| 54 | |
| 55 | def test_q(self): |
| 56 | self.verify_valid_flag('-Qold') |
| 57 | self.verify_valid_flag('-Qnew') |
| 58 | self.verify_valid_flag('-Qwarn') |
| 59 | self.verify_valid_flag('-Qwarnall') |
| 60 | |
| 61 | def test_site_flag(self): |
| 62 | self.verify_valid_flag('-S') |
| 63 | |
| 64 | def test_usage(self): |
Guido van Rossum | f074b64 | 2007-07-11 06:56:16 +0000 | [diff] [blame] | 65 | self.assertTrue(b'usage' in self.start_python('-h')) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 66 | |
| 67 | def test_version(self): |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 68 | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 69 | self.assertTrue(self.start_python('-V').startswith(version)) |
| 70 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 71 | def test_verbose(self): |
| 72 | # -v causes imports to write to stderr. If the write to |
| 73 | # stderr itself causes an import to happen (for the output |
| 74 | # codec), a recursion loop can occur. |
| 75 | data, rc = self.start_python_and_exit_code('-v') |
| 76 | self.assertEqual(rc, 0) |
| 77 | self.assertTrue(b'stack overflow' not in data) |
| 78 | data, rc = self.start_python_and_exit_code('-vv') |
| 79 | self.assertEqual(rc, 0) |
| 80 | self.assertTrue(b'stack overflow' not in data) |
| 81 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 82 | def test_run_module(self): |
| 83 | # Test expected operation of the '-m' switch |
| 84 | # Switch needs an argument |
| 85 | self.assertNotEqual(self.exit_code('-m'), 0) |
| 86 | # Check we get an error for a nonexistent module |
| 87 | self.assertNotEqual( |
| 88 | self.exit_code('-m', 'fnord43520xyz'), |
| 89 | 0) |
| 90 | # Check the runpy module also gives an error for |
| 91 | # a nonexistent module |
| 92 | self.assertNotEqual( |
| 93 | self.exit_code('-m', 'runpy', 'fnord43520xyz'), |
| 94 | 0) |
| 95 | # All good if module is located and run successfully |
| 96 | self.assertEqual( |
| 97 | self.exit_code('-m', 'timeit', '-n', '1'), |
| 98 | 0) |
| 99 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 100 | def test_run_module_bug1764407(self): |
| 101 | # -m and -i need to play well together |
| 102 | # Runs the timeit module and checks the __main__ |
| 103 | # namespace has been populated appropriately |
| 104 | p = _spawn_python('-i', '-m', 'timeit', '-n', '1') |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 105 | p.stdin.write(b'Timer\n') |
| 106 | p.stdin.write(b'exit()\n') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 107 | data = _kill_python(p) |
| 108 | self.assertTrue(data.find(b'1 loop') != -1) |
| 109 | self.assertTrue(data.find(b'__main__.Timer') != -1) |
| 110 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 111 | def test_run_code(self): |
| 112 | # Test expected operation of the '-c' switch |
| 113 | # Switch needs an argument |
| 114 | self.assertNotEqual(self.exit_code('-c'), 0) |
| 115 | # Check we get an error for an uncaught exception |
| 116 | self.assertNotEqual( |
| 117 | self.exit_code('-c', 'raise Exception'), |
| 118 | 0) |
| 119 | # All good if execution is successful |
| 120 | self.assertEqual( |
| 121 | self.exit_code('-c', 'pass'), |
| 122 | 0) |
| 123 | |
| 124 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 125 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame^] | 126 | test.support.run_unittest(CmdLineTest) |
| 127 | test.support.reap_children() |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 128 | |
| 129 | if __name__ == "__main__": |
| 130 | test_main() |