]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | # Tests invocation of the interpreter with various command line arguments\r |
2 | # All tests are executed with environment variables ignored\r | |
3 | # See test_cmd_line_script.py for testing of script execution\r | |
4 | \r | |
5 | import test.test_support, unittest\r | |
6 | import sys\r | |
7 | from test.script_helper import spawn_python, kill_python, python_exit_code\r | |
8 | \r | |
9 | \r | |
10 | class CmdLineTest(unittest.TestCase):\r | |
11 | def start_python(self, *args):\r | |
12 | p = spawn_python(*args)\r | |
13 | return kill_python(p)\r | |
14 | \r | |
15 | def exit_code(self, *args):\r | |
16 | return python_exit_code(*args)\r | |
17 | \r | |
18 | def test_directories(self):\r | |
19 | self.assertNotEqual(self.exit_code('.'), 0)\r | |
20 | self.assertNotEqual(self.exit_code('< .'), 0)\r | |
21 | \r | |
22 | def verify_valid_flag(self, cmd_line):\r | |
23 | data = self.start_python(cmd_line)\r | |
24 | self.assertTrue(data == '' or data.endswith('\n'))\r | |
25 | self.assertNotIn('Traceback', data)\r | |
26 | \r | |
27 | def test_optimize(self):\r | |
28 | self.verify_valid_flag('-O')\r | |
29 | self.verify_valid_flag('-OO')\r | |
30 | \r | |
31 | def test_q(self):\r | |
32 | self.verify_valid_flag('-Qold')\r | |
33 | self.verify_valid_flag('-Qnew')\r | |
34 | self.verify_valid_flag('-Qwarn')\r | |
35 | self.verify_valid_flag('-Qwarnall')\r | |
36 | \r | |
37 | def test_site_flag(self):\r | |
38 | self.verify_valid_flag('-S')\r | |
39 | \r | |
40 | def test_usage(self):\r | |
41 | self.assertIn('usage', self.start_python('-h'))\r | |
42 | \r | |
43 | def test_version(self):\r | |
44 | version = 'Python %d.%d' % sys.version_info[:2]\r | |
45 | self.assertTrue(self.start_python('-V').startswith(version))\r | |
46 | \r | |
47 | def test_run_module(self):\r | |
48 | # Test expected operation of the '-m' switch\r | |
49 | # Switch needs an argument\r | |
50 | self.assertNotEqual(self.exit_code('-m'), 0)\r | |
51 | # Check we get an error for a nonexistent module\r | |
52 | self.assertNotEqual(\r | |
53 | self.exit_code('-m', 'fnord43520xyz'),\r | |
54 | 0)\r | |
55 | # Check the runpy module also gives an error for\r | |
56 | # a nonexistent module\r | |
57 | self.assertNotEqual(\r | |
58 | self.exit_code('-m', 'runpy', 'fnord43520xyz'),\r | |
59 | 0)\r | |
60 | # All good if module is located and run successfully\r | |
61 | self.assertEqual(\r | |
62 | self.exit_code('-m', 'timeit', '-n', '1'),\r | |
63 | 0)\r | |
64 | \r | |
65 | def test_run_module_bug1764407(self):\r | |
66 | # -m and -i need to play well together\r | |
67 | # Runs the timeit module and checks the __main__\r | |
68 | # namespace has been populated appropriately\r | |
69 | p = spawn_python('-i', '-m', 'timeit', '-n', '1')\r | |
70 | p.stdin.write('Timer\n')\r | |
71 | p.stdin.write('exit()\n')\r | |
72 | data = kill_python(p)\r | |
73 | self.assertTrue(data.startswith('1 loop'))\r | |
74 | self.assertIn('__main__.Timer', data)\r | |
75 | \r | |
76 | def test_run_code(self):\r | |
77 | # Test expected operation of the '-c' switch\r | |
78 | # Switch needs an argument\r | |
79 | self.assertNotEqual(self.exit_code('-c'), 0)\r | |
80 | # Check we get an error for an uncaught exception\r | |
81 | self.assertNotEqual(\r | |
82 | self.exit_code('-c', 'raise Exception'),\r | |
83 | 0)\r | |
84 | # All good if execution is successful\r | |
85 | self.assertEqual(\r | |
86 | self.exit_code('-c', 'pass'),\r | |
87 | 0)\r | |
88 | \r | |
89 | \r | |
90 | def test_main():\r | |
91 | test.test_support.run_unittest(CmdLineTest)\r | |
92 | test.test_support.reap_children()\r | |
93 | \r | |
94 | if __name__ == "__main__":\r | |
95 | test_main()\r |