blob: f40c2e9f21eff9aabb449c70846f2a1457ce74fd [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
Kuang-che Wu68f022d2019-11-29 14:38:48 +080011import six
12
Kuang-che Wu88875db2017-07-20 10:47:53 +080013from bisect_kit import cli
Kuang-che Wu88875db2017-07-20 10:47:53 +080014
15
16class 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 Wu603cdad2019-01-18 21:32:55 +080030 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 Wu88875db2017-07-20 10:47:53 +080037 def test_argtype_multiplexer(self):
Kuang-che Wu603cdad2019-01-18 21:32:55 +080038 argtype = cli.argtype_multiplexer(cli.argtype_int,
39 cli.argtype_re('foobar', 'foobar'),
40 cli.argtype_re(r'^r\d+$', 'r123'))
Kuang-che Wu88875db2017-07-20 10:47:53 +080041 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 Wu68f022d2019-11-29 14:38:48 +080054 with six.assertRaisesRegex(self, cli.ArgTypeError, r'\d+\*\d+'):
Kuang-che Wu88875db2017-07-20 10:47:53 +080055 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 Wu88518882017-09-22 16:57:25 +080068 def test_check_executable(self):
69 self.assertEqual(cli.check_executable('/bin/true'), None)
70
Kuang-che Wu68f022d2019-11-29 14:38:48 +080071 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 Wu88518882017-09-22 16:57:25 +080075
Kuang-che Wubc7bfce2019-05-21 18:43:16 +080076 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 Wufe1e88a2019-09-10 21:52:25 +080080 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 Wu88875db2017-07-20 10:47:53 +0800107
Kuang-che Wu88875db2017-07-20 10:47:53 +0800108if __name__ == '__main__':
109 unittest.main()