Kuang-che Wu | 88875db | 2017-07-20 10:47:53 +0800 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """Test util module.""" |
| 5 | |
| 6 | from __future__ import print_function |
| 7 | import subprocess |
| 8 | import unittest |
| 9 | |
| 10 | from bisect_kit import util |
| 11 | |
| 12 | |
| 13 | class TestUtilFunctions(unittest.TestCase): |
| 14 | """Test functions in util module.""" |
| 15 | |
| 16 | def test_is_version_lesseq(self): |
| 17 | assert util.is_version_lesseq('1.1', '1.1') |
| 18 | assert util.is_version_lesseq('1.1.1', '1.1.1') |
| 19 | |
| 20 | assert util.is_version_lesseq('1.1.1', '1.1.2') |
| 21 | assert not util.is_version_lesseq('1.1.2', '1.1.0') |
| 22 | |
| 23 | assert util.is_version_lesseq('1.1.1', '2.0.0') |
| 24 | assert not util.is_version_lesseq('2.0.0', '1.1.1') |
| 25 | |
| 26 | def test_is_direct_relative_version(self): |
| 27 | assert util.is_direct_relative_version('9123.0.0', '9100.0.0') |
| 28 | assert util.is_direct_relative_version('9123.3.0', '9100.0.0') |
| 29 | assert util.is_direct_relative_version('9123.0.5', '9100.0.0') |
| 30 | |
| 31 | assert not util.is_direct_relative_version('9123.0.0', '9100.1.0') |
| 32 | assert not util.is_direct_relative_version('9123.0.0', '9100.1.2') |
| 33 | |
| 34 | assert not util.is_direct_relative_version('1.0.1', '2.0.9') |
| 35 | |
| 36 | |
| 37 | class TestPopen(unittest.TestCase): |
| 38 | """Test util.Popen.""" |
| 39 | |
| 40 | def test_check_output(self): |
| 41 | self.assertEqual(util.check_output('echo', 'foobar'), 'foobar\n') |
| 42 | self.assertEqual( |
| 43 | util.check_output('sh', '-c', 'echo 1; sleep 0.05; echo 2'), '1\n2\n') |
| 44 | self.assertEqual(util.check_output('echo 1; echo 2', shell=True), '1\n2\n') |
| 45 | |
| 46 | with self.assertRaises(subprocess.CalledProcessError): |
| 47 | util.check_output('false') |
| 48 | |
| 49 | self.assertEqual(util.check_output('env', env=dict(foo='bar')), 'foo=bar\n') |
| 50 | |
| 51 | def test_check_call(self): |
| 52 | util.check_call('true') |
| 53 | with self.assertRaises(subprocess.CalledProcessError): |
| 54 | util.check_call('false') |
| 55 | |
| 56 | def test_call(self): |
| 57 | self.assertEqual(util.call('true'), 0) |
| 58 | self.assertEqual(util.call('false'), 1) |
| 59 | |
| 60 | |
| 61 | if __name__ == '__main__': |
| 62 | unittest.main() |