Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 2 | |
| 3 | """Tests for autoserv_parser.""" |
| 4 | |
| 5 | import sys |
| 6 | import unittest |
| 7 | |
| 8 | import common |
| 9 | from autotest_lib.server import autoserv_parser |
| 10 | |
| 11 | |
| 12 | class autoserv_parser_test(unittest.TestCase): |
Dan Shi | 888cfca | 2015-07-31 15:49:00 -0700 | [diff] [blame] | 13 | """Test autoserv_parser can parse command line correctly. |
| 14 | """ |
| 15 | |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 16 | def setUp(self): |
| 17 | self.orig_sys_argv = sys.argv |
| 18 | |
| 19 | def tearDown(self): |
| 20 | sys.argv = self.orig_sys_argv |
| 21 | |
| 22 | def _get_parser(self): |
Dan Shi | 888cfca | 2015-07-31 15:49:00 -0700 | [diff] [blame] | 23 | """Get the parser class. |
| 24 | """ |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 25 | # We resort to this vile hack because autoserv_parser is bad |
| 26 | # enough to instantiate itself and replace its own class definition |
| 27 | # with its instance at module import time. Disgusting. |
| 28 | return autoserv_parser.autoserv_parser.__class__() |
| 29 | |
| 30 | def test_control_file_args(self): |
Dan Shi | 888cfca | 2015-07-31 15:49:00 -0700 | [diff] [blame] | 31 | """Test the parser can parse args and unknown args correctly. |
| 32 | """ |
| 33 | sys.argv = ['autoserv', '--args', '-y -z foo --hello', 'controlfile'] |
| 34 | parser = self._get_parser() |
| 35 | parser.parse_args() |
| 36 | self.assertEqual(['controlfile', '-y', '-z', 'foo', '--hello'], |
| 37 | parser.args) |
| 38 | |
| 39 | sys.argv = ['autoserv', '--args', '-y -z foo --hello', '--unknown_arg', |
| 40 | 'unknown_arg_val', 'controlfile'] |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 41 | parser = self._get_parser() |
| 42 | parser.parse_args() |
| 43 | self.assertEqual(['controlfile', '-y', '-z', 'foo', '--hello'], |
| 44 | parser.args) |
| 45 | |
| 46 | |
| 47 | if __name__ == '__main__': |
| 48 | unittest.main() |