/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to git/tests/test_objects.py

  • Committer: James Westby
  • Date: 2007-03-25 11:45:49 UTC
  • mto: (0.215.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jw+debian@jameswestby.net-20070325114549-jl2t51a668wssrhb
Start the python-git project.

Aims to give an interface to git repos that doesn't call out to git directly.
Probably going to be pure python.

Currently can read blobs, trees and commits from the files. It reads both
legacy and new headers. However it is untested for anything but the simple
case.

Can also understand a little about the repository format.

The testsuite uses the nosetests program from Turbogears, as I got annoyed
trying to set up unittest.

Open up a repo by passing it the path to the .git dir. You can then ask for
HEAD with repo.head() or a ref with repo.ref(name). Both return the SHA id
they currently point to. You can then grab this object with
repo.get_object(sha).

For the actual objects the ShaFile.from_file(filename) will return the object
stored in the file whatever it is. To ensure you get the correct type then
call {Blob,Tree,Commit}.from_file(filename). I will add repo methods to do
this for you with file lookup soon.

There is also support for creating blobs. Blob.from_string(string) will create
a blob object from the string. You can then call blob.sha() to get the sha
object for this blob, and hexdigest() on that will get its ID. There is
currently no method that allows you to write it out though.

Everything is currently done with assertions, where much of it should probably
be exceptions. This was merely done for expediency. If you hit an assertion,
it either means you have done something wrong, there is corruption, or
you are trying an unsupported operation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import unittest
 
3
 
 
4
from git.objects import (Blob,
 
5
                         Tree,
 
6
                         Commit,
 
7
                         )
 
8
 
 
9
a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
 
10
b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
 
11
c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
 
12
tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
 
13
 
 
14
class BlobReadTests(unittest.TestCase):
 
15
  """Test decompression of blobs"""
 
16
 
 
17
  def get_sha_file(self, obj, base, sha):
 
18
    return obj.from_file(os.path.join(os.path.dirname(__file__),
 
19
                                      'data', base, sha))
 
20
 
 
21
  def get_blob(self, sha):
 
22
    """Return the blob named sha from the test data dir"""
 
23
    return self.get_sha_file(Blob, 'blobs', sha)
 
24
 
 
25
  def get_tree(self, sha):
 
26
    return self.get_sha_file(Tree, 'trees', sha)
 
27
 
 
28
  def get_commit(self, sha):
 
29
    return self.get_sha_file(Commit, 'commits', sha)
 
30
 
 
31
  def test_decompress_simple_blob(self):
 
32
    b = self.get_blob(a_sha)
 
33
    self.assertEqual(b.text(), 'test 1\n')
 
34
    self.assertEqual(b.sha().hexdigest(), a_sha)
 
35
 
 
36
  def test_parse_empty_blob_object(self):
 
37
    sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
 
38
    b = self.get_blob(sha)
 
39
    self.assertEqual(b.text(), '')
 
40
    self.assertEqual(b.sha().hexdigest(), sha)
 
41
 
 
42
  def test_create_blob_from_string(self):
 
43
    string = 'test 2\n'
 
44
    b = Blob.from_string(string)
 
45
    self.assertEqual(b.text(), string)
 
46
    self.assertEqual(b.sha().hexdigest(), b_sha)
 
47
 
 
48
  def test_parse_legacy_blob(self):
 
49
    string = 'test 3\n'
 
50
    b = self.get_blob(c_sha)
 
51
    self.assertEqual(b.text(), string)
 
52
    self.assertEqual(b.sha().hexdigest(), c_sha)
 
53
 
 
54
  def test_read_tree_from_file(self):
 
55
    t = self.get_tree(tree_sha)
 
56
    self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
 
57
    self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
 
58
 
 
59
  def test_read_commit_from_file(self):
 
60
    sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
 
61
    c = self.get_commit(sha)
 
62
    self.assertEqual(c.tree(), tree_sha)
 
63
    self.assertEqual(c.parents(), ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
 
64
    self.assertEqual(c.author(),
 
65
        'James Westby <jw+debian@jameswestby.net> 1174759230 +0000')
 
66
    self.assertEqual(c.committer(),
 
67
        'James Westby <jw+debian@jameswestby.net> 1174759230 +0000')
 
68
    self.assertEqual(c.message(), 'Test commit\n')
 
69
 
 
70
  def test_read_commit_no_parents(self):
 
71
    sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
 
72
    c = self.get_commit(sha)
 
73
    self.assertEqual(c.tree(), '90182552c4a85a45ec2a835cadc3451bebdfe870')
 
74
    self.assertEqual(c.parents(), [])
 
75
    self.assertEqual(c.author(),
 
76
        'James Westby <jw+debian@jameswestby.net> 1174758034 +0000')
 
77
    self.assertEqual(c.committer(),
 
78
        'James Westby <jw+debian@jameswestby.net> 1174758034 +0000')
 
79
    self.assertEqual(c.message(), 'Test commit\n')
 
80
 
 
81
  def test_read_commit_two_parents(self):
 
82
    sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
 
83
    c = self.get_commit(sha)
 
84
    self.assertEqual(c.tree(), 'd80c186a03f423a81b39df39dc87fd269736ca86')
 
85
    self.assertEqual(c.parents(), ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
 
86
                                   '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
 
87
    self.assertEqual(c.author(),
 
88
        'James Westby <jw+debian@jameswestby.net> 1174773719 +0000')
 
89
    self.assertEqual(c.committer(),
 
90
        'James Westby <jw+debian@jameswestby.net> 1174773719 +0000')
 
91
    self.assertEqual(c.message(), 'Merge ../b\n')
 
92