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