added tests for Repository.create_blob()

This commit is contained in:
Nico von Geyso
2012-05-11 14:03:35 +02:00
parent 57dd20c5f0
commit 632750565b
2 changed files with 26 additions and 0 deletions

View File

@@ -38,6 +38,7 @@ from . import utils
__author__ = 'dborowitz@google.com (Dave Borowitz)'
BLOB_SHA = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
BLOB_NEW_CONTENT = 'foo bar\n'
class BlobTest(utils.BareRepoTestCase):
@@ -52,6 +53,22 @@ class BlobTest(utils.BareRepoTestCase):
self.assertEqual(b'a contents\n', blob.data)
self.assertEqual(b'a contents\n', blob.read_raw())
def test_create_blob(self):
blob_oid = self.repo.create_blob(BLOB_NEW_CONTENT)
blob = self.repo[blob_oid]
self.assertTrue(isinstance(blob, pygit2.Blob))
self.assertEqual(pygit2.GIT_OBJ_BLOB, blob.type)
self.assertEqual(blob_oid, blob.oid)
self.assertEqual(
utils.gen_blob_sha1(BLOB_NEW_CONTENT),
utils.oid_to_hex(blob_oid)
)
self.assertEqual(BLOB_NEW_CONTENT, blob.data)
self.assertEqual(BLOB_NEW_CONTENT.decode('ascii'), blob.read_raw())
if __name__ == '__main__':
unittest.main()

View File

@@ -34,6 +34,7 @@ import stat
import tarfile
import tempfile
import unittest
import hashlib
import pygit2
@@ -50,6 +51,14 @@ def force_rm_handle(remove_path, path, excinfo):
def oid_to_hex(oid):
return b2a_hex(oid).decode('ascii')
def gen_blob_sha1(data):
m = hashlib.sha1()
# http://stackoverflow.com/questions/552659/assigning-git-sha1s-without-git
git_sha1_format = 'blob %d\0%s' % (len(data), data)
m.update(git_sha1_format)
return m.hexdigest().decode('ascii')
def rmtree(path):
"""In Windows a read-only file cannot be removed, and shutil.rmtree fails.