Home | History | Annotate | Download | only in test
      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 from test.script_helper import spawn_python, kill_python, python_exit_code
      8 
      9 
     10 class CmdLineTest(unittest.TestCase):
     11     def start_python(self, *args):
     12         p = spawn_python(*args)
     13         return kill_python(p)
     14 
     15     def exit_code(self, *args):
     16         return python_exit_code(*args)
     17 
     18     def test_directories(self):
     19         self.assertNotEqual(self.exit_code('.'), 0)
     20         self.assertNotEqual(self.exit_code('< .'), 0)
     21 
     22     def verify_valid_flag(self, cmd_line):
     23         data = self.start_python(cmd_line)
     24         self.assertTrue(data == '' or data.endswith('\n'))
     25         self.assertNotIn('Traceback', data)
     26 
     27     def test_optimize(self):
     28         self.verify_valid_flag('-O')
     29         self.verify_valid_flag('-OO')
     30 
     31     def test_q(self):
     32         self.verify_valid_flag('-Qold')
     33         self.verify_valid_flag('-Qnew')
     34         self.verify_valid_flag('-Qwarn')
     35         self.verify_valid_flag('-Qwarnall')
     36 
     37     def test_site_flag(self):
     38         self.verify_valid_flag('-S')
     39 
     40     def test_usage(self):
     41         self.assertIn('usage', self.start_python('-h'))
     42 
     43     def test_version(self):
     44         version = 'Python %d.%d' % sys.version_info[:2]
     45         self.assertTrue(self.start_python('-V').startswith(version))
     46 
     47     def test_run_module(self):
     48         # Test expected operation of the '-m' switch

     49         # Switch needs an argument

     50         self.assertNotEqual(self.exit_code('-m'), 0)
     51         # Check we get an error for a nonexistent module

     52         self.assertNotEqual(
     53             self.exit_code('-m', 'fnord43520xyz'),
     54             0)
     55         # Check the runpy module also gives an error for

     56         # a nonexistent module

     57         self.assertNotEqual(
     58             self.exit_code('-m', 'runpy', 'fnord43520xyz'),
     59             0)
     60         # All good if module is located and run successfully

     61         self.assertEqual(
     62             self.exit_code('-m', 'timeit', '-n', '1'),
     63             0)
     64 
     65     def test_run_module_bug1764407(self):
     66         # -m and -i need to play well together

     67         # Runs the timeit module and checks the __main__

     68         # namespace has been populated appropriately

     69         p = spawn_python('-i', '-m', 'timeit', '-n', '1')
     70         p.stdin.write('Timer\n')
     71         p.stdin.write('exit()\n')
     72         data = kill_python(p)
     73         self.assertTrue(data.startswith('1 loop'))
     74         self.assertIn('__main__.Timer', data)
     75 
     76     def test_run_code(self):
     77         # Test expected operation of the '-c' switch

     78         # Switch needs an argument

     79         self.assertNotEqual(self.exit_code('-c'), 0)
     80         # Check we get an error for an uncaught exception

     81         self.assertNotEqual(
     82             self.exit_code('-c', 'raise Exception'),
     83             0)
     84         # All good if execution is successful

     85         self.assertEqual(
     86             self.exit_code('-c', 'pass'),
     87             0)
     88 
     89 
     90 def test_main():
     91     test.test_support.run_unittest(CmdLineTest)
     92     test.test_support.reap_children()
     93 
     94 if __name__ == "__main__":
     95     test_main()
     96