blob: 538f32d40a992488f265aa7af25429a55edabaa0 [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):
Kuang-che Wu430c5282021-01-27 21:10:25 +080028 assert util.is_direct_relative_version('9123.0.0', '9123.0.0')
Kuang-che Wu88875db2017-07-20 10:47:53 +080029 assert util.is_direct_relative_version('9123.0.0', '9100.0.0')
30 assert util.is_direct_relative_version('9123.3.0', '9100.0.0')
31 assert util.is_direct_relative_version('9123.0.5', '9100.0.0')
32
33 assert not util.is_direct_relative_version('9123.0.0', '9100.1.0')
34 assert not util.is_direct_relative_version('9123.0.0', '9100.1.2')
35
36 assert not util.is_direct_relative_version('1.0.1', '2.0.9')
37
38
39class TestPopen(unittest.TestCase):
40 """Test util.Popen."""
41
42 def test_check_output(self):
43 self.assertEqual(util.check_output('echo', 'foobar'), 'foobar\n')
44 self.assertEqual(
45 util.check_output('sh', '-c', 'echo 1; sleep 0.05; echo 2'), '1\n2\n')
46 self.assertEqual(util.check_output('echo 1; echo 2', shell=True), '1\n2\n')
47
48 with self.assertRaises(subprocess.CalledProcessError):
49 util.check_output('false')
50
51 self.assertEqual(util.check_output('env', env=dict(foo='bar')), 'foo=bar\n')
52
53 def test_check_call(self):
54 util.check_call('true')
55 with self.assertRaises(subprocess.CalledProcessError):
56 util.check_call('false')
57
58 def test_call(self):
59 self.assertEqual(util.call('true'), 0)
60 self.assertEqual(util.call('false'), 1)
61
62
63if __name__ == '__main__':
64 unittest.main()