blob: 86668a053dd03dde005e3dda1d928fd2ca1b660f [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 util module."""
6
7from __future__ import print_function
8import subprocess
9import unittest
10
11from bisect_kit import util
12
13
14class 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
38class 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
62if __name__ == '__main__':
63 unittest.main()