blob: a8e50c36f6c49d67fc7abaefec7a2c1e69b37ecc [file] [log] [blame]
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08001# -*- coding: utf-8 -*-
Kuang-che Wue41e0062017-09-01 19:04:14 +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 bisect_git script."""
6
7from __future__ import print_function
Kuang-che Wue80bb872018-11-15 19:45:25 +08008import shutil
9import tempfile
Kuang-che Wue41e0062017-09-01 19:04:14 +080010import unittest
11
Kuang-che Wua5723492019-11-25 20:59:34 +080012try:
13 from unittest import mock
14except ImportError:
15 # TODO(kcwu): remove once migrated to python3
16 import mock
Kuang-che Wue41e0062017-09-01 19:04:14 +080017
Kuang-che Wu2526a672019-09-10 16:23:59 +080018from bisect_kit import bisector_cli
Kuang-che Wue80bb872018-11-15 19:45:25 +080019from bisect_kit import git_util_test
Kuang-che Wue41e0062017-09-01 19:04:14 +080020import bisect_git
21
22
23@mock.patch('bisect_kit.common.config_logging', mock.Mock())
24class TestGitDomain(unittest.TestCase):
25 """Test GitDomain class."""
26
27 def setUp(self):
Kuang-che Wue80bb872018-11-15 19:45:25 +080028 self.git_repo = tempfile.mkdtemp()
29 self.git = git_util_test.GitOperation(self.git_repo)
30 self.git.init()
31
32 def tearDown(self):
33 shutil.rmtree(self.git_repo)
Kuang-che Wue41e0062017-09-01 19:04:14 +080034
35 def test_basic(self):
36 """Tests basic functionality."""
Kuang-che Wu2526a672019-09-10 16:23:59 +080037 bisector = bisector_cli.BisectorCommandLine(bisect_git.GitDomain)
Kuang-che Wue41e0062017-09-01 19:04:14 +080038
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080039 self.git.create_commits(10)
40 commits = self.git.commits
41 old, new = commits[1], commits[-1]
Kuang-che Wue41e0062017-09-01 19:04:14 +080042
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080043 bisector.main('init', '--old', old, '--new', new, '--git_repo',
44 self.git_repo)
Kuang-che Wue41e0062017-09-01 19:04:14 +080045
Kuang-che Wu88518882017-09-22 16:57:25 +080046 bisector.main('config', 'switch', 'true')
47 bisector.main('config', 'eval', 'true')
Kuang-che Wue41e0062017-09-01 19:04:14 +080048
49 with mock.patch('bisect_kit.util.Popen') as mock_popen:
50 run_mocks = [mock.Mock(), mock.Mock()]
51 run_mocks[0].wait.return_value = 0
52 run_mocks[1].wait.return_value = 0
53 mock_popen.side_effect = run_mocks
54 bisector.main('run', old)
55
56 switch_env = mock_popen.call_args_list[0][1]['env']
57 self.assertEqual(switch_env.get('GIT_REV'), old)
58 self.assertEqual(switch_env.get('GIT_REPO'), self.git_repo)
59
60 eval_env = mock_popen.call_args_list[1][1]['env']
61 self.assertEqual(eval_env.get('GIT_REV'), old)
62 self.assertEqual(eval_env.get('GIT_REPO'), self.git_repo)
63 bisector.main('view')
64
65
66if __name__ == '__main__':
67 unittest.main()