blob: 19089a44aa6d5a9ffe35227e1d1a6c6d7ac5232d [file] [log] [blame]
Christian Heimes9cd17752007-11-18 19:35:23 +00001# 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 Norwitz11bd1192005-10-03 00:54:56 +00004
5import test.test_support, unittest
6import sys
Walter Dörwald9356fb92005-11-25 15:22:10 +00007import subprocess
Neal Norwitz11bd1192005-10-03 00:54:56 +00008
Thomas Woutersed03b412007-08-28 21:37:11 +00009def _spawn_python(*args):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000010 cmd_line = [sys.executable, '-E']
Thomas Woutersed03b412007-08-28 21:37:11 +000011 cmd_line.extend(args)
12 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
13 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
14
15def _kill_python(p):
Trent Nelson39e307e2008-03-19 06:45:48 +000016 return _kill_python_and_exit_code(p)[0]
17
18def _kill_python_and_exit_code(p):
Thomas Woutersed03b412007-08-28 21:37:11 +000019 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 Nelson39e307e2008-03-19 06:45:48 +000025 returncode = p.wait()
26 return data, returncode
Thomas Woutersed03b412007-08-28 21:37:11 +000027
Neal Norwitz11bd1192005-10-03 00:54:56 +000028class CmdLineTest(unittest.TestCase):
Thomas Woutersed03b412007-08-28 21:37:11 +000029 def start_python(self, *args):
Trent Nelson39e307e2008-03-19 06:45:48 +000030 return self.start_python_and_exit_code(*args)[0]
31
32 def start_python_and_exit_code(self, *args):
Thomas Woutersed03b412007-08-28 21:37:11 +000033 p = _spawn_python(*args)
Trent Nelson39e307e2008-03-19 06:45:48 +000034 return _kill_python_and_exit_code(p)
Neal Norwitz11bd1192005-10-03 00:54:56 +000035
Thomas Wouters477c8d52006-05-27 19:21:47 +000036 def exit_code(self, *args):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000037 cmd_line = [sys.executable, '-E']
Thomas Wouters477c8d52006-05-27 19:21:47 +000038 cmd_line.extend(args)
39 return subprocess.call(cmd_line, stdout=subprocess.PIPE,
40 stderr=subprocess.PIPE)
Walter Dörwald9356fb92005-11-25 15:22:10 +000041
Neal Norwitz11bd1192005-10-03 00:54:56 +000042 def test_directories(self):
Neal Norwitz72c2c062006-03-09 05:58:11 +000043 self.assertNotEqual(self.exit_code('.'), 0)
44 self.assertNotEqual(self.exit_code('< .'), 0)
Neal Norwitz11bd1192005-10-03 00:54:56 +000045
46 def verify_valid_flag(self, cmd_line):
47 data = self.start_python(cmd_line)
Guido van Rossuma1c42a92007-08-29 03:47:36 +000048 self.assertTrue(data == b'' or data.endswith(b'\n'))
Guido van Rossumf074b642007-07-11 06:56:16 +000049 self.assertTrue(b'Traceback' not in data)
Neal Norwitz11bd1192005-10-03 00:54:56 +000050
Neal Norwitz11bd1192005-10-03 00:54:56 +000051 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 Rossumf074b642007-07-11 06:56:16 +000065 self.assertTrue(b'usage' in self.start_python('-h'))
Neal Norwitz11bd1192005-10-03 00:54:56 +000066
67 def test_version(self):
Guido van Rossuma1c42a92007-08-29 03:47:36 +000068 version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
Neal Norwitz11bd1192005-10-03 00:54:56 +000069 self.assertTrue(self.start_python('-V').startswith(version))
70
Trent Nelson39e307e2008-03-19 06:45:48 +000071 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 Wouters477c8d52006-05-27 19:21:47 +000082 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 Woutersed03b412007-08-28 21:37:11 +0000100 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 Rossuma1c42a92007-08-29 03:47:36 +0000105 p.stdin.write(b'Timer\n')
106 p.stdin.write(b'exit()\n')
Thomas Woutersed03b412007-08-28 21:37:11 +0000107 data = _kill_python(p)
108 self.assertTrue(data.find(b'1 loop') != -1)
109 self.assertTrue(data.find(b'__main__.Timer') != -1)
110
Thomas Wouters477c8d52006-05-27 19:21:47 +0000111 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 Norwitz11bd1192005-10-03 00:54:56 +0000125def test_main():
126 test.test_support.run_unittest(CmdLineTest)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127 test.test_support.reap_children()
Neal Norwitz11bd1192005-10-03 00:54:56 +0000128
129if __name__ == "__main__":
130 test_main()