| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import test.test_support |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
from test.script_helper import ( |
| 9 |
assert_python_ok, assert_python_failure, spawn_python, kill_python, |
| 10 |
python_exit_code |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class CmdLineTest(unittest.TestCase): |
| 15 |
def start_python(self, *args): |
| 16 |
p = spawn_python(*args) |
| 17 |
return kill_python(p) |
| 18 |
|
| 19 |
def exit_code(self, *args): |
| 20 |
return python_exit_code(*args) |
| 21 |
|
| 22 |
def test_directories(self): |
| 23 |
self.assertNotEqual(self.exit_code('.'), 0) |
| 24 |
self.assertNotEqual(self.exit_code('< .'), 0) |
| 25 |
|
| 26 |
def verify_valid_flag(self, cmd_line): |
| 27 |
data = self.start_python(cmd_line) |
| 28 |
self.assertTrue(data == '' or data.endswith('\n')) |
| 29 |
self.assertNotIn('Traceback', data) |
| 30 |
|
| 31 |
def test_optimize(self): |
| 32 |
self.verify_valid_flag('-O') |
| 33 |
self.verify_valid_flag('-OO') |
| 34 |
|
| 35 |
def test_q(self): |
| 36 |
self.verify_valid_flag('-Qold') |
| 37 |
self.verify_valid_flag('-Qnew') |
| 38 |
self.verify_valid_flag('-Qwarn') |
| 39 |
self.verify_valid_flag('-Qwarnall') |
| 40 |
|
| 41 |
def test_site_flag(self): |
| 42 |
self.verify_valid_flag('-S') |
| 43 |
|
| 44 |
def test_usage(self): |
| 45 |
self.assertIn('usage', self.start_python('-h')) |
| 46 |
|
| 47 |
def test_version(self): |
| 48 |
version = 'Python %d.%d' % sys.version_info[:2] |
| 49 |
self.assertTrue(self.start_python('-V').startswith(version)) |
| 50 |
|
| 51 |
def test_run_module(self): |
| 52 |
|
| 53 |
|
| 54 |
self.assertNotEqual(self.exit_code('-m'), 0) |
| 55 |
|
| 56 |
self.assertNotEqual( |
| 57 |
self.exit_code('-m', 'fnord43520xyz'), |
| 58 |
0) |
| 59 |
|
| 60 |
|
| 61 |
self.assertNotEqual( |
| 62 |
self.exit_code('-m', 'runpy', 'fnord43520xyz'), |
| 63 |
0) |
| 64 |
|
| 65 |
self.assertEqual( |
| 66 |
self.exit_code('-m', 'timeit', '-n', '1'), |
| 67 |
0) |
| 68 |
|
| 69 |
def test_run_module_bug1764407(self): |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
p = spawn_python('-i', '-m', 'timeit', '-n', '1') |
| 74 |
p.stdin.write('Timer\n') |
| 75 |
p.stdin.write('exit()\n') |
| 76 |
data = kill_python(p) |
| 77 |
self.assertTrue(data.startswith('1 loop')) |
| 78 |
self.assertIn('__main__.Timer', data) |
| 79 |
|
| 80 |
def test_run_code(self): |
| 81 |
|
| 82 |
|
| 83 |
self.assertNotEqual(self.exit_code('-c'), 0) |
| 84 |
|
| 85 |
self.assertNotEqual( |
| 86 |
self.exit_code('-c', 'raise Exception'), |
| 87 |
0) |
| 88 |
|
| 89 |
self.assertEqual( |
| 90 |
self.exit_code('-c', 'pass'), |
| 91 |
0) |
| 92 |
|
| 93 |
def test_hash_randomization(self): |
| 94 |
|
| 95 |
self.verify_valid_flag('-R') |
| 96 |
hashes = [] |
| 97 |
for i in range(2): |
| 98 |
code = 'print(hash("spam"))' |
| 99 |
data = self.start_python('-R', '-c', code) |
| 100 |
hashes.append(data) |
| 101 |
self.assertNotEqual(hashes[0], hashes[1]) |
| 102 |
|
| 103 |
|
| 104 |
code = 'import sys; print sys.flags' |
| 105 |
data = self.start_python('-R', '-c', code) |
| 106 |
self.assertTrue('hash_randomization=1' in data) |
| 107 |
|
| 108 |
def test_del___main__(self): |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
filename = test.test_support.TESTFN |
| 113 |
self.addCleanup(test.test_support.unlink, filename) |
| 114 |
with open(filename, "w") as script: |
| 115 |
print >>script, "import sys" |
| 116 |
print >>script, "del sys.modules['__main__']" |
| 117 |
assert_python_ok(filename) |
| 118 |
|
| 119 |
def test_unknown_options(self): |
| 120 |
rc, out, err = assert_python_failure('-E', '-z') |
| 121 |
self.assertIn(b'Unknown option: -z', err) |
| 122 |
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
| 123 |
self.assertEqual(b'', out) |
| 124 |
|
| 125 |
|
| 126 |
rc, out, err = assert_python_failure('-z', without='-E') |
| 127 |
self.assertIn(b'Unknown option: -z', err) |
| 128 |
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
| 129 |
self.assertEqual(b'', out) |
| 130 |
rc, out, err = assert_python_failure('-a', '-z', without='-E') |
| 131 |
self.assertIn(b'Unknown option: -a', err) |
| 132 |
|
| 133 |
self.assertNotIn(b'Unknown option: -z', err) |
| 134 |
self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1) |
| 135 |
self.assertEqual(b'', out) |
| 136 |
|
| 137 |
|
| 138 |
def test_main(): |
| 139 |
test.test_support.run_unittest(CmdLineTest) |
| 140 |
test.test_support.reap_children() |
| 141 |
|
| 142 |
if __name__ == "__main__": |
| 143 |
test_main() |