test_walk.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # test_walk.py -- Tests for commit walking functionality.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for commit walking functionality."""
  19. from dulwich.object_store import (
  20. MemoryObjectStore,
  21. )
  22. from dulwich.walk import (
  23. Walker,
  24. )
  25. from dulwich.tests import TestCase
  26. from utils import (
  27. build_commit_graph,
  28. )
  29. class WalkerTest(TestCase):
  30. def setUp(self):
  31. self.store = MemoryObjectStore()
  32. def make_commits(self, commit_spec, **kwargs):
  33. return build_commit_graph(self.store, commit_spec, **kwargs)
  34. def make_linear_commits(self, num_commits, **kwargs):
  35. commit_spec = []
  36. for i in xrange(1, num_commits + 1):
  37. c = [i]
  38. if i > 1:
  39. c.append(i - 1)
  40. commit_spec.append(c)
  41. return self.make_commits(commit_spec, **kwargs)
  42. def assertWalkYields(self, expected, *args, **kwargs):
  43. walker = Walker(self.store, *args, **kwargs)
  44. actual = list(walker)
  45. self.assertEqual(expected, actual)
  46. def test_linear(self):
  47. c1, c2, c3 = self.make_linear_commits(3)
  48. self.assertWalkYields([c1], [c1.id])
  49. self.assertWalkYields([c2, c1], [c2.id])
  50. self.assertWalkYields([c3, c2, c1], [c3.id])
  51. self.assertWalkYields([c3, c2, c1], [c3.id, c1.id])
  52. self.assertWalkYields([c3, c2], [c3.id], exclude=[c1.id])
  53. self.assertWalkYields([c3, c2], [c3.id, c1.id], exclude=[c1.id])
  54. self.assertWalkYields([c3], [c3.id, c1.id], exclude=[c2.id])
  55. def test_branch(self):
  56. c1, x2, x3, y4 = self.make_commits([[1], [2, 1], [3, 2], [4, 1]])
  57. self.assertWalkYields([x3, x2, c1], [x3.id])
  58. self.assertWalkYields([y4, c1], [y4.id])
  59. self.assertWalkYields([y4, x2, c1], [y4.id, x2.id])
  60. self.assertWalkYields([y4, x2], [y4.id, x2.id], exclude=[c1.id])
  61. self.assertWalkYields([y4, x3], [y4.id, x3.id], exclude=[x2.id])
  62. self.assertWalkYields([y4], [y4.id], exclude=[x3.id])
  63. self.assertWalkYields([x3, x2], [x3.id], exclude=[y4.id])
  64. def test_reverse(self):
  65. c1, c2, c3 = self.make_linear_commits(3)
  66. self.assertWalkYields([c1, c2, c3], [c3.id], reverse=True)