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 util module.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | import subprocess |
| 9 | import unittest |
| 10 | |
| 11 | from bisect_kit import util |
| 12 | |
| 13 | |
| 14 | class TestUtilFunctions(unittest.TestCase): |
| 15 | """Test functions in util module.""" |
| 16 | |
| 17 | def test_is_version_lesseq(self): |
| 18 | assert util.is_version_lesseq('1.1', '1.1') |
| 19 | assert util.is_version_lesseq('1.1.1', '1.1.1') |
| 20 | |
| 21 | assert util.is_version_lesseq('1.1.1', '1.1.2') |
| 22 | assert not util.is_version_lesseq('1.1.2', '1.1.0') |
| 23 | |
| 24 | assert util.is_version_lesseq('1.1.1', '2.0.0') |
| 25 | assert not util.is_version_lesseq('2.0.0', '1.1.1') |
| 26 | |
| 27 | def test_is_direct_relative_version(self): |
| 28 | assert util.is_direct_relative_version('9123.0.0', '9100.0.0') |
| 29 | assert util.is_direct_relative_version('9123.3.0', '9100.0.0') |
| 30 | assert util.is_direct_relative_version('9123.0.5', '9100.0.0') |
| 31 | |
| 32 | assert not util.is_direct_relative_version('9123.0.0', '9100.1.0') |
| 33 | assert not util.is_direct_relative_version('9123.0.0', '9100.1.2') |
| 34 | |
| 35 | assert not util.is_direct_relative_version('1.0.1', '2.0.9') |
| 36 | |
| 37 | |
| 38 | class TestPopen(unittest.TestCase): |
| 39 | """Test util.Popen.""" |
| 40 | |
| 41 | def test_check_output(self): |
| 42 | self.assertEqual(util.check_output('echo', 'foobar'), 'foobar\n') |
| 43 | self.assertEqual( |
| 44 | util.check_output('sh', '-c', 'echo 1; sleep 0.05; echo 2'), '1\n2\n') |
| 45 | self.assertEqual(util.check_output('echo 1; echo 2', shell=True), '1\n2\n') |
| 46 | |
| 47 | with self.assertRaises(subprocess.CalledProcessError): |
| 48 | util.check_output('false') |
| 49 | |
| 50 | self.assertEqual(util.check_output('env', env=dict(foo='bar')), 'foo=bar\n') |
| 51 | |
| 52 | def test_check_call(self): |
| 53 | util.check_call('true') |
| 54 | with self.assertRaises(subprocess.CalledProcessError): |
| 55 | util.check_call('false') |
| 56 | |
| 57 | def test_call(self): |
| 58 | self.assertEqual(util.call('true'), 0) |
| 59 | self.assertEqual(util.call('false'), 1) |
| 60 | |
| 61 | |
| 62 | if __name__ == '__main__': |
| 63 | unittest.main() |