Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 2 | # 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 | |
| 7 | from __future__ import print_function |
| 8 | import unittest |
| 9 | |
| 10 | import mock |
| 11 | |
| 12 | from bisect_kit import cli |
| 13 | import bisect_git |
| 14 | |
| 15 | |
| 16 | @mock.patch('bisect_kit.common.config_logging', mock.Mock()) |
| 17 | class 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 Wu | 68db08a | 2018-03-30 11:50:34 +0800 | [diff] [blame] | 27 | bisector = cli.BisectorCommandLine(bisect_git.GitDomain) |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 28 | |
| 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 Wu | 8851888 | 2017-09-22 16:57:25 +0800 | [diff] [blame] | 51 | bisector.main('config', 'switch', 'true') |
| 52 | bisector.main('config', 'eval', 'true') |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 53 | |
| 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 | |
| 71 | if __name__ == '__main__': |
| 72 | unittest.main() |