09_test_write_tree.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # vim: set fileencoding=utf-8 :
  2. """Test L{GitRepository}'s write_tree method"""
  3. from __future__ import print_function
  4. from . import context
  5. from . import testutils
  6. import os
  7. import gbp.log
  8. import gbp.git
  9. import gbp.errors
  10. class TestWriteTree(testutils.DebianGitTestRepo):
  11. def _write_testtree(self):
  12. """Write a test tree"""
  13. paths = []
  14. for i in range(4):
  15. path = os.path.join(self.repo.path, 'testfile%d' % i)
  16. with open(path, 'w') as f:
  17. print("testdata %d" % i, file=f)
  18. paths.append(path)
  19. return paths
  20. def test_write_tree_index_nonexistant(self):
  21. """Write out index file to nonexistant dir"""
  22. paths = self._write_testtree()
  23. self.repo.add_files(paths)
  24. self.assertRaises(gbp.git.GitRepositoryError,
  25. self.repo.write_tree,
  26. '/does/not/exist')
  27. def test_write_tree(self):
  28. """Write out index file to alternate index file"""
  29. index = os.path.join(self.repo.git_dir, 'gbp_index')
  30. expected_sha1 = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
  31. paths = self._write_testtree()
  32. self.repo.add_files(paths)
  33. sha1 = self.repo.write_tree(index)
  34. self.assertTrue(os.path.exists(index))
  35. self.assertEqual(sha1, expected_sha1)
  36. self.assertTrue(self.repo.has_treeish(expected_sha1))
  37. def test_commit_tree(self):
  38. """Commit a tree"""
  39. expected_sha1 = 'ea63fcee40675a5f82ea6bedbf29ca86d89c5f63'
  40. paths = self._write_testtree()
  41. self.repo.add_files(paths)
  42. sha1 = self.repo.write_tree()
  43. self.assertEqual(sha1, expected_sha1)
  44. self.assertTrue(self.repo.has_treeish(expected_sha1))
  45. commit = self.repo.commit_tree(sha1, "first commit", parents=[],
  46. committer=dict(name='foo',
  47. email='foo@example.com'),
  48. author=dict(name='bar',
  49. email='bar@example.com'),
  50. )
  51. self.assertEqual(len(commit), 40)
  52. # commit the same tree again using the previous commit as parent
  53. self.repo.commit_tree(sha1, "second commit", parents=[commit])
  54. # commit the same tree again using a non existant parent
  55. self.assertRaises(gbp.errors.GbpError,
  56. self.repo.commit_tree,
  57. sha1,
  58. "failed commit",
  59. ['doesnotexist'])
  60. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: