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 | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 8 | import unittest |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 9 | |
| 10 | from bisect_kit import cli |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class 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 Wu | 603cdad | 2019-01-18 21:32:55 +0800 | [diff] [blame] | 27 | 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 Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 34 | def test_argtype_multiplexer(self): |
Kuang-che Wu | 603cdad | 2019-01-18 21:32:55 +0800 | [diff] [blame] | 35 | argtype = cli.argtype_multiplexer(cli.argtype_int, |
| 36 | cli.argtype_re('foobar', 'foobar'), |
| 37 | cli.argtype_re(r'^r\d+$', 'r123')) |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 38 | 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 Wu | 8851888 | 2017-09-22 16:57:25 +0800 | [diff] [blame] | 65 | 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 Wu | bc7bfce | 2019-05-21 18:43:16 +0800 | [diff] [blame] | 73 | 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 Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 77 | |
Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 78 | if __name__ == '__main__': |
| 79 | unittest.main() |