blob: 4f68063f36fa43852ef42e8f3236cdc086a31b97 [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
12import mock
13
14from bisect_kit import cli
Kuang-che Wue80bb872018-11-15 19:45:25 +080015from bisect_kit import git_util_test
Kuang-che Wue41e0062017-09-01 19:04:14 +080016import bisect_git
17
18
19@mock.patch('bisect_kit.common.config_logging', mock.Mock())
20class TestGitDomain(unittest.TestCase):
21 """Test GitDomain class."""
22
23 def setUp(self):
Kuang-che Wue80bb872018-11-15 19:45:25 +080024 self.git_repo = tempfile.mkdtemp()
25 self.git = git_util_test.GitOperation(self.git_repo)
26 self.git.init()
27
28 def tearDown(self):
29 shutil.rmtree(self.git_repo)
Kuang-che Wue41e0062017-09-01 19:04:14 +080030
31 def test_basic(self):
32 """Tests basic functionality."""
Kuang-che Wu68db08a2018-03-30 11:50:34 +080033 bisector = cli.BisectorCommandLine(bisect_git.GitDomain)
Kuang-che Wue41e0062017-09-01 19:04:14 +080034
35 start = 12345678000
36 fake_commits = map(str, range(start, start + 10))
37 old, new = fake_commits[0], fake_commits[-1]
38
39 def is_containing_commit(git_repo, rev):
40 self.assertEqual(git_repo, self.git_repo)
41 return rev in fake_commits
42
43 def get_revlist(git_repo, old, new):
44 self.assertEqual(git_repo, self.git_repo)
45 self.assertEqual(old, fake_commits[0])
46 self.assertEqual(new, fake_commits[-1])
47 return fake_commits
48
Kuang-che Wue80bb872018-11-15 19:45:25 +080049 with mock.patch.multiple(
50 'bisect_kit.git_util',
51 is_containing_commit=is_containing_commit,
52 get_revlist=get_revlist):
53 bisector.main('init', '--old', old, '--new', new, '--git_repo',
54 self.git_repo)
Kuang-che Wue41e0062017-09-01 19:04:14 +080055
Kuang-che Wu88518882017-09-22 16:57:25 +080056 bisector.main('config', 'switch', 'true')
57 bisector.main('config', 'eval', 'true')
Kuang-che Wue41e0062017-09-01 19:04:14 +080058
59 with mock.patch('bisect_kit.util.Popen') as mock_popen:
60 run_mocks = [mock.Mock(), mock.Mock()]
61 run_mocks[0].wait.return_value = 0
62 run_mocks[1].wait.return_value = 0
63 mock_popen.side_effect = run_mocks
64 bisector.main('run', old)
65
66 switch_env = mock_popen.call_args_list[0][1]['env']
67 self.assertEqual(switch_env.get('GIT_REV'), old)
68 self.assertEqual(switch_env.get('GIT_REPO'), self.git_repo)
69
70 eval_env = mock_popen.call_args_list[1][1]['env']
71 self.assertEqual(eval_env.get('GIT_REV'), old)
72 self.assertEqual(eval_env.get('GIT_REPO'), self.git_repo)
73 bisector.main('view')
74
75
76if __name__ == '__main__':
77 unittest.main()