1# Copyright (c) 2012 The Chromium 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
5import unittest
6
7bisect_builds = __import__('bisect-builds')
8
9
10class BisectTest(unittest.TestCase):
11
12  patched = []
13  max_rev = 10000
14
15  def monkey_patch(self, obj, name, new):
16    self.patched.append((obj, name, getattr(obj, name)))
17    setattr(obj, name, new)
18
19  def clear_patching(self):
20    for obj, name, old in self.patched:
21      setattr(obj, name, old)
22    self.patched = []
23
24  def setUp(self):
25    self.monkey_patch(bisect_builds.DownloadJob, 'Start', lambda *args: None)
26    self.monkey_patch(bisect_builds.DownloadJob, 'Stop', lambda *args: None)
27    self.monkey_patch(bisect_builds.DownloadJob, 'WaitFor', lambda *args: None)
28    self.monkey_patch(bisect_builds, 'RunRevision', lambda *args: (0, "", ""))
29    self.monkey_patch(bisect_builds.PathContext, 'ParseDirectoryIndex',
30                      lambda *args: range(self.max_rev))
31
32  def tearDown(self):
33    self.clear_patching()
34
35  def bisect(self, good_rev, bad_rev, evaluate):
36    return bisect_builds.Bisect(good_rev=good_rev,
37                                bad_rev=bad_rev,
38                                evaluate=evaluate,
39                                num_runs=1,
40                                official_builds=False,
41                                platform='linux',
42                                profile=None,
43                                try_args=())
44
45  def testBisectConsistentAnswer(self):
46    self.assertEqual(self.bisect(1000, 100, lambda *args: 'g'), (100, 101))
47    self.assertEqual(self.bisect(100, 1000, lambda *args: 'b'), (100, 101))
48    self.assertEqual(self.bisect(2000, 200, lambda *args: 'b'), (1999, 2000))
49    self.assertEqual(self.bisect(200, 2000, lambda *args: 'g'), (1999, 2000))
50
51
52if __name__ == '__main__':
53  unittest.main()
54