blob: bf4838ab137bbba0895cdcbe15b622467734c9a0 [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 Wu88875db2017-07-20 10:47:53 +08008import unittest
Kuang-che Wu88875db2017-07-20 10:47:53 +08009
10from bisect_kit import cli
Kuang-che Wu88875db2017-07-20 10:47:53 +080011
12
13class TestCli(unittest.TestCase):
14 """Test functions in cli module."""
15
16 def test_argtype_int(self):
17 self.assertEqual(cli.argtype_int('123456'), '123456')
18 self.assertEqual(cli.argtype_int('-123456'), '-123456')
19 with self.assertRaises(cli.ArgTypeError):
20 cli.argtype_int('foobar')
21
22 def test_argtype_notempty(self):
23 self.assertEqual(cli.argtype_notempty('123456'), '123456')
24 with self.assertRaises(cli.ArgTypeError):
25 cli.argtype_notempty('')
26
Kuang-che Wu603cdad2019-01-18 21:32:55 +080027 def test_argtype_re(self):
28 argtype = cli.argtype_re(r'^r\d+$', 'r123')
29 self.assertEqual(argtype('r123'), 'r123')
30
31 with self.assertRaises(cli.ArgTypeError):
32 argtype('hello')
33
Kuang-che Wu88875db2017-07-20 10:47:53 +080034 def test_argtype_multiplexer(self):
Kuang-che Wu603cdad2019-01-18 21:32:55 +080035 argtype = cli.argtype_multiplexer(cli.argtype_int,
36 cli.argtype_re('foobar', 'foobar'),
37 cli.argtype_re(r'^r\d+$', 'r123'))
Kuang-che Wu88875db2017-07-20 10:47:53 +080038 self.assertEqual(argtype('123456'), '123456')
39 self.assertEqual(argtype('foobar'), 'foobar')
40 self.assertEqual(argtype('r123'), 'r123')
41
42 with self.assertRaises(cli.ArgTypeError):
43 argtype('hello')
44
45 def test_argtype_multiplier(self):
46 argtype = cli.argtype_multiplier(cli.argtype_int)
47 self.assertEqual(argtype('123'), ('123', 1))
48 self.assertEqual(argtype('123*5'), ('123', 5))
49
50 # Make sure there is multiplier example in the message.
51 with self.assertRaisesRegexp(cli.ArgTypeError, r'\d+\*\d+'):
52 argtype('hello')
53
54 def test_argtype_path(self):
55 self.assertEqual(cli.argtype_dir_path('/'), '/')
56 self.assertEqual(cli.argtype_dir_path('/etc/'), '/etc')
57
58 with self.assertRaises(cli.ArgTypeError):
59 cli.argtype_dir_path('')
60 with self.assertRaises(cli.ArgTypeError):
61 cli.argtype_dir_path('/foo/bar/not/exist/path')
62 with self.assertRaises(cli.ArgTypeError):
63 cli.argtype_dir_path('/etc/passwd')
64
Kuang-che Wu88518882017-09-22 16:57:25 +080065 def test_check_executable(self):
66 self.assertEqual(cli.check_executable('/bin/true'), None)
67
68 self.assertRegexpMatches(cli.check_executable('/'), r'Not a file')
69 self.assertRegexpMatches(cli.check_executable('LICENSE'), r'chmod')
70 self.assertRegexpMatches(cli.check_executable('what'), r'PATH')
71 self.assertRegexpMatches(cli.check_executable('eval-manually.sh'), r'\./')
72
Kuang-che Wubc7bfce2019-05-21 18:43:16 +080073 def test_lookup_signal_name(self):
74 self.assertEqual(cli.lookup_signal_name(15), 'SIGTERM')
75 self.assertEqual(cli.lookup_signal_name(99), 'Unknown')
76
Kuang-che Wu88875db2017-07-20 10:47:53 +080077
Kuang-che Wu88875db2017-07-20 10:47:53 +080078if __name__ == '__main__':
79 unittest.main()