Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Test cli module.""" |
| 6 | |
| 7 | from __future__ import print_function |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 8 | import argparse |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 9 | import unittest |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 10 | |
Kuang-che Wu | 68f022d | 2019-11-29 14:38:48 +0800 | [diff] [blame^] | 11 | import six |
| 12 | |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 13 | from bisect_kit import cli |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 14 | |
| 15 | |
| 16 | class TestCli(unittest.TestCase): |
| 17 | """Test functions in cli module.""" |
| 18 | |
| 19 | def test_argtype_int(self): |
| 20 | self.assertEqual(cli.argtype_int('123456'), '123456') |
| 21 | self.assertEqual(cli.argtype_int('-123456'), '-123456') |
| 22 | with self.assertRaises(cli.ArgTypeError): |
| 23 | cli.argtype_int('foobar') |
| 24 | |
| 25 | def test_argtype_notempty(self): |
| 26 | self.assertEqual(cli.argtype_notempty('123456'), '123456') |
| 27 | with self.assertRaises(cli.ArgTypeError): |
| 28 | cli.argtype_notempty('') |
| 29 | |
Kuang-che Wu | 603cdad | 2019-01-18 21:32:55 +0800 | [diff] [blame] | 30 | def test_argtype_re(self): |
| 31 | argtype = cli.argtype_re(r'^r\d+$', 'r123') |
| 32 | self.assertEqual(argtype('r123'), 'r123') |
| 33 | |
| 34 | with self.assertRaises(cli.ArgTypeError): |
| 35 | argtype('hello') |
| 36 | |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 37 | def test_argtype_multiplexer(self): |
Kuang-che Wu | 603cdad | 2019-01-18 21:32:55 +0800 | [diff] [blame] | 38 | argtype = cli.argtype_multiplexer(cli.argtype_int, |
| 39 | cli.argtype_re('foobar', 'foobar'), |
| 40 | cli.argtype_re(r'^r\d+$', 'r123')) |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 41 | self.assertEqual(argtype('123456'), '123456') |
| 42 | self.assertEqual(argtype('foobar'), 'foobar') |
| 43 | self.assertEqual(argtype('r123'), 'r123') |
| 44 | |
| 45 | with self.assertRaises(cli.ArgTypeError): |
| 46 | argtype('hello') |
| 47 | |
| 48 | def test_argtype_multiplier(self): |
| 49 | argtype = cli.argtype_multiplier(cli.argtype_int) |
| 50 | self.assertEqual(argtype('123'), ('123', 1)) |
| 51 | self.assertEqual(argtype('123*5'), ('123', 5)) |
| 52 | |
| 53 | # Make sure there is multiplier example in the message. |
Kuang-che Wu | 68f022d | 2019-11-29 14:38:48 +0800 | [diff] [blame^] | 54 | with six.assertRaisesRegex(self, cli.ArgTypeError, r'\d+\*\d+'): |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 55 | argtype('hello') |
| 56 | |
| 57 | def test_argtype_path(self): |
| 58 | self.assertEqual(cli.argtype_dir_path('/'), '/') |
| 59 | self.assertEqual(cli.argtype_dir_path('/etc/'), '/etc') |
| 60 | |
| 61 | with self.assertRaises(cli.ArgTypeError): |
| 62 | cli.argtype_dir_path('') |
| 63 | with self.assertRaises(cli.ArgTypeError): |
| 64 | cli.argtype_dir_path('/foo/bar/not/exist/path') |
| 65 | with self.assertRaises(cli.ArgTypeError): |
| 66 | cli.argtype_dir_path('/etc/passwd') |
| 67 | |
Kuang-che Wu | 8851888 | 2017-09-22 16:57:25 +0800 | [diff] [blame] | 68 | def test_check_executable(self): |
| 69 | self.assertEqual(cli.check_executable('/bin/true'), None) |
| 70 | |
Kuang-che Wu | 68f022d | 2019-11-29 14:38:48 +0800 | [diff] [blame^] | 71 | six.assertRegex(self, cli.check_executable('/'), r'Not a file') |
| 72 | six.assertRegex(self, cli.check_executable('LICENSE'), r'chmod') |
| 73 | six.assertRegex(self, cli.check_executable('what'), r'PATH') |
| 74 | six.assertRegex(self, cli.check_executable('eval-manually.sh'), r'\./') |
Kuang-che Wu | 8851888 | 2017-09-22 16:57:25 +0800 | [diff] [blame] | 75 | |
Kuang-che Wu | bc7bfce | 2019-05-21 18:43:16 +0800 | [diff] [blame] | 76 | def test_lookup_signal_name(self): |
| 77 | self.assertEqual(cli.lookup_signal_name(15), 'SIGTERM') |
| 78 | self.assertEqual(cli.lookup_signal_name(99), 'Unknown') |
| 79 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 80 | def test_patching_argparser_exit(self): |
| 81 | parser = argparse.ArgumentParser() |
| 82 | cli.patching_argparser_exit(parser) |
| 83 | parser.add_argument('--value', type=int) |
| 84 | parser.add_argument('--necessary', required=True) |
| 85 | |
| 86 | # Nothing happened. |
| 87 | parser.parse_args(['--necessary', 'foo']) |
| 88 | |
| 89 | with self.assertRaises(SystemExit) as e: |
| 90 | parser.parse_args(['foo']) |
| 91 | self.assertEqual(e.exception.code, cli.EXIT_CODE_FATAL) |
| 92 | |
| 93 | with self.assertRaises(SystemExit) as e: |
| 94 | parser.parse_args(['--necessary', 'foo', '--value', 'bar']) |
| 95 | self.assertEqual(e.exception.code, cli.EXIT_CODE_FATAL) |
| 96 | |
| 97 | def test_fatal_error_handler(self): |
| 98 | |
| 99 | @cli.fatal_error_handler |
| 100 | def test_func(): |
| 101 | assert 0 |
| 102 | |
| 103 | with self.assertRaises(SystemExit) as e: |
| 104 | test_func() |
| 105 | self.assertEqual(e.exception.code, cli.EXIT_CODE_FATAL) |
| 106 | |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 107 | |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 108 | if __name__ == '__main__': |
| 109 | unittest.main() |