|
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 |
|
4 |
|
5 import test.test_support, unittest |
|
6 import sys |
|
7 import subprocess |
|
8 |
|
9 def _spawn_python(*args): |
|
10 cmd_line = [sys.executable, '-E'] |
|
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): |
|
16 p.stdin.close() |
|
17 data = p.stdout.read() |
|
18 p.stdout.close() |
|
19 # try to cleanup the child so we don't appear to leak when running |
|
20 # with regrtest -R. This should be a no-op on Windows. |
|
21 subprocess._cleanup() |
|
22 return data |
|
23 |
|
24 class CmdLineTest(unittest.TestCase): |
|
25 def start_python(self, *args): |
|
26 p = _spawn_python(*args) |
|
27 return _kill_python(p) |
|
28 |
|
29 def exit_code(self, *args): |
|
30 cmd_line = [sys.executable, '-E'] |
|
31 cmd_line.extend(args) |
|
32 return subprocess.call(cmd_line, stdout=subprocess.PIPE, |
|
33 stderr=subprocess.PIPE) |
|
34 |
|
35 def test_directories(self): |
|
36 self.assertNotEqual(self.exit_code('.'), 0) |
|
37 self.assertNotEqual(self.exit_code('< .'), 0) |
|
38 |
|
39 def verify_valid_flag(self, cmd_line): |
|
40 data = self.start_python(cmd_line) |
|
41 self.assertTrue(data == '' or data.endswith('\n')) |
|
42 self.assertTrue('Traceback' not in data) |
|
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): |
|
58 self.assertTrue('usage' in self.start_python('-h')) |
|
59 |
|
60 def test_version(self): |
|
61 version = 'Python %d.%d' % sys.version_info[:2] |
|
62 self.assertTrue(self.start_python('-V').startswith(version)) |
|
63 |
|
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 |
|
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') |
|
87 p.stdin.write('Timer\n') |
|
88 p.stdin.write('exit()\n') |
|
89 data = _kill_python(p) |
|
90 self.assertTrue(data.startswith('1 loop')) |
|
91 self.assertTrue('__main__.Timer' in data) |
|
92 |
|
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 |
|
107 def test_main(): |
|
108 test.test_support.run_unittest(CmdLineTest) |
|
109 test.test_support.reap_children() |
|
110 |
|
111 if __name__ == "__main__": |
|
112 test_main() |