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 |
Kuang-che Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 8 | import shutil |
| 9 | import tempfile |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 10 | import unittest |
Kuang-che Wu | 23192ad | 2020-03-11 18:12:46 +0800 | [diff] [blame] | 11 | from unittest import mock |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 12 | |
Kuang-che Wu | 2526a67 | 2019-09-10 16:23:59 +0800 | [diff] [blame] | 13 | from bisect_kit import bisector_cli |
Kuang-che Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 14 | from bisect_kit import git_util_test |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 15 | import bisect_git |
| 16 | |
| 17 | |
| 18 | @mock.patch('bisect_kit.common.config_logging', mock.Mock()) |
| 19 | class TestGitDomain(unittest.TestCase): |
| 20 | """Test GitDomain class.""" |
| 21 | |
| 22 | def setUp(self): |
Kuang-che Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 23 | self.git_repo = tempfile.mkdtemp() |
| 24 | self.git = git_util_test.GitOperation(self.git_repo) |
| 25 | self.git.init() |
| 26 | |
| 27 | def tearDown(self): |
| 28 | shutil.rmtree(self.git_repo) |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 29 | |
| 30 | def test_basic(self): |
| 31 | """Tests basic functionality.""" |
Kuang-che Wu | 2526a67 | 2019-09-10 16:23:59 +0800 | [diff] [blame] | 32 | bisector = bisector_cli.BisectorCommandLine(bisect_git.GitDomain) |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 33 | |
Kuang-che Wu | 5e7c9b0 | 2019-01-03 21:16:01 +0800 | [diff] [blame] | 34 | self.git.create_commits(10) |
| 35 | commits = self.git.commits |
| 36 | old, new = commits[1], commits[-1] |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 37 | |
Kuang-che Wu | 5e7c9b0 | 2019-01-03 21:16:01 +0800 | [diff] [blame] | 38 | bisector.main('init', '--old', old, '--new', new, '--git_repo', |
| 39 | self.git_repo) |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 40 | |
Kuang-che Wu | 8851888 | 2017-09-22 16:57:25 +0800 | [diff] [blame] | 41 | bisector.main('config', 'switch', 'true') |
| 42 | bisector.main('config', 'eval', 'true') |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 43 | |
| 44 | with mock.patch('bisect_kit.util.Popen') as mock_popen: |
| 45 | run_mocks = [mock.Mock(), mock.Mock()] |
| 46 | run_mocks[0].wait.return_value = 0 |
| 47 | run_mocks[1].wait.return_value = 0 |
| 48 | mock_popen.side_effect = run_mocks |
| 49 | bisector.main('run', old) |
| 50 | |
| 51 | switch_env = mock_popen.call_args_list[0][1]['env'] |
| 52 | self.assertEqual(switch_env.get('GIT_REV'), old) |
| 53 | self.assertEqual(switch_env.get('GIT_REPO'), self.git_repo) |
| 54 | |
| 55 | eval_env = mock_popen.call_args_list[1][1]['env'] |
| 56 | self.assertEqual(eval_env.get('GIT_REV'), old) |
| 57 | self.assertEqual(eval_env.get('GIT_REPO'), self.git_repo) |
| 58 | bisector.main('view') |
| 59 | |
| 60 | |
| 61 | if __name__ == '__main__': |
| 62 | unittest.main() |