blob: 7d4f2abf7158900cc30ad2f1a07940d13381fc1e [file] [log] [blame]
Neal Norwitz11bd1192005-10-03 00:54:56 +00001
2import test.test_support, unittest
3import sys
Walter Dörwald9356fb92005-11-25 15:22:10 +00004import subprocess
Neal Norwitz11bd1192005-10-03 00:54:56 +00005
Thomas Woutersed03b412007-08-28 21:37:11 +00006def _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
12def _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 Norwitz11bd1192005-10-03 00:54:56 +000021class CmdLineTest(unittest.TestCase):
Thomas Woutersed03b412007-08-28 21:37:11 +000022 def start_python(self, *args):
23 p = _spawn_python(*args)
24 return _kill_python(p)
Neal Norwitz11bd1192005-10-03 00:54:56 +000025
Thomas Wouters477c8d52006-05-27 19:21:47 +000026 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örwald9356fb92005-11-25 15:22:10 +000031
Neal Norwitz11bd1192005-10-03 00:54:56 +000032 def test_directories(self):
Neal Norwitz72c2c062006-03-09 05:58:11 +000033 self.assertNotEqual(self.exit_code('.'), 0)
34 self.assertNotEqual(self.exit_code('< .'), 0)
Neal Norwitz11bd1192005-10-03 00:54:56 +000035
36 def verify_valid_flag(self, cmd_line):
37 data = self.start_python(cmd_line)
Guido van Rossuma1c42a92007-08-29 03:47:36 +000038 self.assertTrue(data == b'' or data.endswith(b'\n'))
Guido van Rossumf074b642007-07-11 06:56:16 +000039 self.assertTrue(b'Traceback' not in data)
Neal Norwitz11bd1192005-10-03 00:54:56 +000040
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 Rossumf074b642007-07-11 06:56:16 +000058 self.assertTrue(b'usage' in self.start_python('-h'))
Neal Norwitz11bd1192005-10-03 00:54:56 +000059
60 def test_version(self):
Guido van Rossuma1c42a92007-08-29 03:47:36 +000061 version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
Neal Norwitz11bd1192005-10-03 00:54:56 +000062 self.assertTrue(self.start_python('-V').startswith(version))
63
Thomas Wouters477c8d52006-05-27 19:21:47 +000064 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 Woutersed03b412007-08-28 21:37:11 +000082 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 Rossuma1c42a92007-08-29 03:47:36 +000087 p.stdin.write(b'Timer\n')
88 p.stdin.write(b'exit()\n')
Thomas Woutersed03b412007-08-28 21:37:11 +000089 data = _kill_python(p)
90 self.assertTrue(data.find(b'1 loop') != -1)
91 self.assertTrue(data.find(b'__main__.Timer') != -1)
92
Thomas Wouters477c8d52006-05-27 19:21:47 +000093 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 Norwitz11bd1192005-10-03 00:54:56 +0000107def test_main():
108 test.test_support.run_unittest(CmdLineTest)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000109 test.test_support.reap_children()
Neal Norwitz11bd1192005-10-03 00:54:56 +0000110
111if __name__ == "__main__":
112 test_main()