blob: 19740077c21ea97c38cf1adc1e50cdb5a316b1ae [file] [log] [blame]
Kuang-che Wue41e0062017-09-01 19:04:14 +08001# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Test bisect_git script."""
5
6from __future__ import print_function
7import unittest
8
9import mock
10
11from bisect_kit import cli
12import bisect_git
13
14
15@mock.patch('bisect_kit.common.config_logging', mock.Mock())
16class TestGitDomain(unittest.TestCase):
17 """Test GitDomain class."""
18
19 def setUp(self):
20 # All git operations should have been mocked, nobody will access this path
21 # on disk.
22 self.git_repo = '/non/existing/git/repo/path'
23
24 def test_basic(self):
25 """Tests basic functionality."""
26 bisector = cli.BisectorCommandLineInterface(bisect_git.GitDomain)
27
28 start = 12345678000
29 fake_commits = map(str, range(start, start + 10))
30 old, new = fake_commits[0], fake_commits[-1]
31
32 def is_containing_commit(git_repo, rev):
33 self.assertEqual(git_repo, self.git_repo)
34 return rev in fake_commits
35
36 def get_revlist(git_repo, old, new):
37 self.assertEqual(git_repo, self.git_repo)
38 self.assertEqual(old, fake_commits[0])
39 self.assertEqual(new, fake_commits[-1])
40 return fake_commits
41
42 with mock.patch('bisect_kit.cli.argtype_dir_path', lambda x: x):
43 with mock.patch.multiple(
44 'bisect_kit.git_util',
45 is_containing_commit=is_containing_commit,
46 get_revlist=get_revlist):
47 bisector.main('init', '--old', old, '--new', new, '--git_repo',
48 self.git_repo)
49
Kuang-che Wu88518882017-09-22 16:57:25 +080050 bisector.main('config', 'switch', 'true')
51 bisector.main('config', 'eval', 'true')
Kuang-che Wue41e0062017-09-01 19:04:14 +080052
53 with mock.patch('bisect_kit.util.Popen') as mock_popen:
54 run_mocks = [mock.Mock(), mock.Mock()]
55 run_mocks[0].wait.return_value = 0
56 run_mocks[1].wait.return_value = 0
57 mock_popen.side_effect = run_mocks
58 bisector.main('run', old)
59
60 switch_env = mock_popen.call_args_list[0][1]['env']
61 self.assertEqual(switch_env.get('GIT_REV'), old)
62 self.assertEqual(switch_env.get('GIT_REPO'), self.git_repo)
63
64 eval_env = mock_popen.call_args_list[1][1]['env']
65 self.assertEqual(eval_env.get('GIT_REV'), old)
66 self.assertEqual(eval_env.get('GIT_REPO'), self.git_repo)
67 bisector.main('view')
68
69
70if __name__ == '__main__':
71 unittest.main()