You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.4KB

  1. import os
  2. import tempfile
  3. from git.objects import IndexObject
  4. from git.refs import (
  5. RefLogEntry,
  6. RefLog
  7. )
  8. from git.test.lib import (
  9. TestBase,
  10. fixture_path
  11. )
  12. from git.util import Actor, rmtree, hex_to_bin
  13. import os.path as osp
  14. class TestRefLog(TestBase):
  15. def test_reflogentry(self):
  16. nullhexsha = IndexObject.NULL_HEX_SHA
  17. hexsha = 'F' * 40
  18. actor = Actor('name', 'email')
  19. msg = "message"
  20. self.failUnlessRaises(ValueError, RefLogEntry.new, nullhexsha, hexsha, 'noactor', 0, 0, "")
  21. e = RefLogEntry.new(nullhexsha, hexsha, actor, 0, 1, msg)
  22. assert e.oldhexsha == nullhexsha
  23. assert e.newhexsha == hexsha
  24. assert e.actor == actor
  25. assert e.time[0] == 0
  26. assert e.time[1] == 1
  27. assert e.message == msg
  28. # check representation (roughly)
  29. assert repr(e).startswith(nullhexsha)
  30. def test_base(self):
  31. rlp_head = fixture_path('reflog_HEAD')
  32. rlp_master = fixture_path('reflog_master')
  33. tdir = tempfile.mktemp(suffix="test_reflogs")
  34. os.mkdir(tdir)
  35. rlp_master_ro = RefLog.path(self.rorepo.head)
  36. assert osp.isfile(rlp_master_ro)
  37. # simple read
  38. reflog = RefLog.from_file(rlp_master_ro)
  39. assert reflog._path is not None
  40. assert isinstance(reflog, RefLog)
  41. assert len(reflog)
  42. # iter_entries works with path and with stream
  43. assert len(list(RefLog.iter_entries(open(rlp_master, 'rb'))))
  44. assert len(list(RefLog.iter_entries(rlp_master)))
  45. # raise on invalid revlog
  46. # TODO: Try multiple corrupted ones !
  47. pp = 'reflog_invalid_'
  48. for suffix in ('oldsha', 'newsha', 'email', 'date', 'sep'):
  49. self.failUnlessRaises(ValueError, RefLog.from_file, fixture_path(pp + suffix))
  50. # END for each invalid file
  51. # cannot write an uninitialized reflog
  52. self.failUnlessRaises(ValueError, RefLog().write)
  53. # test serialize and deserialize - results must match exactly
  54. binsha = hex_to_bin(('f' * 40).encode('ascii'))
  55. msg = "my reflog message"
  56. cr = self.rorepo.config_reader()
  57. for rlp in (rlp_head, rlp_master):
  58. reflog = RefLog.from_file(rlp)
  59. tfile = osp.join(tdir, osp.basename(rlp))
  60. reflog.to_file(tfile)
  61. assert reflog.write() is reflog
  62. # parsed result must match ...
  63. treflog = RefLog.from_file(tfile)
  64. assert treflog == reflog
  65. # ... as well as each bytes of the written stream
  66. assert open(tfile).read() == open(rlp).read()
  67. # append an entry
  68. entry = RefLog.append_entry(cr, tfile, IndexObject.NULL_BIN_SHA, binsha, msg)
  69. assert entry.oldhexsha == IndexObject.NULL_HEX_SHA
  70. assert entry.newhexsha == 'f' * 40
  71. assert entry.message == msg
  72. assert RefLog.from_file(tfile)[-1] == entry
  73. # index entry
  74. # raises on invalid index
  75. self.failUnlessRaises(IndexError, RefLog.entry_at, rlp, 10000)
  76. # indices can be positive ...
  77. assert isinstance(RefLog.entry_at(rlp, 0), RefLogEntry)
  78. RefLog.entry_at(rlp, 23)
  79. # ... and negative
  80. for idx in (-1, -24):
  81. RefLog.entry_at(rlp, idx)
  82. # END for each index to read
  83. # END for each reflog
  84. # finally remove our temporary data
  85. rmtree(tdir)