blob: 3f82362f7e3fc9ff6ab0ef72dfc1df8358118782 [file] [log] [blame]
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08001# -*- coding: utf-8 -*-
Kuang-che Wu88875db2017-07-20 10:47:53 +08002# 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
7from __future__ import print_function
Kuang-che Wufe1e88a2019-09-10 21:52:25 +08008import argparse
Kuang-che Wu88875db2017-07-20 10:47:53 +08009import unittest
Kuang-che Wu88875db2017-07-20 10:47:53 +080010
11from bisect_kit import cli
Kuang-che Wu88875db2017-07-20 10:47:53 +080012
13
14class 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 Wu603cdad2019-01-18 21:32:55 +080028 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 Wu88875db2017-07-20 10:47:53 +080035 def test_argtype_multiplexer(self):
Kuang-che Wu603cdad2019-01-18 21:32:55 +080036 argtype = cli.argtype_multiplexer(cli.argtype_int,
37 cli.argtype_re('foobar', 'foobar'),
38 cli.argtype_re(r'^r\d+$', 'r123'))
Kuang-che Wu88875db2017-07-20 10:47:53 +080039 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 Wu88518882017-09-22 16:57:25 +080066 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 Wubc7bfce2019-05-21 18:43:16 +080074 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 Wufe1e88a2019-09-10 21:52:25 +080078 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 Wu88875db2017-07-20 10:47:53 +0800105
Kuang-che Wu88875db2017-07-20 10:47:53 +0800106if __name__ == '__main__':
107 unittest.main()