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