blob: 17917a1f0010754a8419c93971a7b5336694337d [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
8import unittest
9
10import mock
11
12from bisect_kit import cli
13import bisect_git
14
15
16@mock.patch('bisect_kit.common.config_logging', mock.Mock())
17class TestGitDomain(unittest.TestCase):
18 """Test GitDomain class."""
19
20 def setUp(self):
21 # All git operations should have been mocked, nobody will access this path
22 # on disk.
23 self.git_repo = '/non/existing/git/repo/path'
24
25 def test_basic(self):
26 """Tests basic functionality."""
Kuang-che Wu68db08a2018-03-30 11:50:34 +080027 bisector = cli.BisectorCommandLine(bisect_git.GitDomain)
Kuang-che Wue41e0062017-09-01 19:04:14 +080028
29 start = 12345678000
30 fake_commits = map(str, range(start, start + 10))
31 old, new = fake_commits[0], fake_commits[-1]
32
33 def is_containing_commit(git_repo, rev):
34 self.assertEqual(git_repo, self.git_repo)
35 return rev in fake_commits
36
37 def get_revlist(git_repo, old, new):
38 self.assertEqual(git_repo, self.git_repo)
39 self.assertEqual(old, fake_commits[0])
40 self.assertEqual(new, fake_commits[-1])
41 return fake_commits
42
43 with mock.patch('bisect_kit.cli.argtype_dir_path', lambda x: x):
44 with mock.patch.multiple(
45 'bisect_kit.git_util',
46 is_containing_commit=is_containing_commit,
47 get_revlist=get_revlist):
48 bisector.main('init', '--old', old, '--new', new, '--git_repo',
49 self.git_repo)
50
Kuang-che Wu88518882017-09-22 16:57:25 +080051 bisector.main('config', 'switch', 'true')
52 bisector.main('config', 'eval', 'true')
Kuang-che Wue41e0062017-09-01 19:04:14 +080053
54 with mock.patch('bisect_kit.util.Popen') as mock_popen:
55 run_mocks = [mock.Mock(), mock.Mock()]
56 run_mocks[0].wait.return_value = 0
57 run_mocks[1].wait.return_value = 0
58 mock_popen.side_effect = run_mocks
59 bisector.main('run', old)
60
61 switch_env = mock_popen.call_args_list[0][1]['env']
62 self.assertEqual(switch_env.get('GIT_REV'), old)
63 self.assertEqual(switch_env.get('GIT_REPO'), self.git_repo)
64
65 eval_env = mock_popen.call_args_list[1][1]['env']
66 self.assertEqual(eval_env.get('GIT_REV'), old)
67 self.assertEqual(eval_env.get('GIT_REPO'), self.git_repo)
68 bisector.main('view')
69
70
71if __name__ == '__main__':
72 unittest.main()