7
7
from test .testlib import *
8
8
from git import *
9
9
10
- class TestTree (object ):
11
- def setup (self ):
10
+ class TestTree (TestCase ):
11
+
12
+ def setUp (self ):
12
13
self .repo = Repo (GIT_REPO )
13
14
14
15
@patch_object (Git , '_call_process' )
15
16
def test_contents_should_cache (self , git ):
16
17
git .return_value = fixture ('ls_tree_a' ) + fixture ('ls_tree_b' )
17
18
18
- tree = self .repo .tree ('master' )
19
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
19
20
20
21
child = tree ['grit' ]
21
- child . items ( )
22
- child . items ( )
22
+ len ( child )
23
+ len ( child )
23
24
24
25
assert_true (git .called )
25
26
assert_equal (2 , git .call_count )
26
27
assert_equal (git .call_args , (('ls_tree' , '34868e6e7384cb5ee51c543a8187fdff2675b5a7' ), {}))
27
28
28
29
def test_content_from_string_tree_should_return_tree (self ):
29
30
text = fixture ('ls_tree_a' ).splitlines ()[- 1 ]
30
- tree = Tree .content__from_string (None , text )
31
+ tree = Tree .content_from_string (None , text )
31
32
32
33
assert_equal (Tree , tree .__class__ )
33
34
assert_equal ("650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44" , tree .id )
@@ -37,7 +38,7 @@ def test_content_from_string_tree_should_return_tree(self):
37
38
def test_content_from_string_tree_should_return_blob (self ):
38
39
text = fixture ('ls_tree_b' ).split ("\n " )[0 ]
39
40
40
- tree = Tree .content__from_string (None , text )
41
+ tree = Tree .content_from_string (None , text )
41
42
42
43
assert_equal (Blob , tree .__class__ )
43
44
assert_equal ("aa94e396335d2957ca92606f909e53e7beaf3fbb" , tree .id )
@@ -47,101 +48,120 @@ def test_content_from_string_tree_should_return_blob(self):
47
48
def test_content_from_string_tree_should_return_commit (self ):
48
49
text = fixture ('ls_tree_commit' ).split ("\n " )[1 ]
49
50
50
- tree = Tree .content__from_string (None , text )
51
+ tree = Tree .content_from_string (None , text )
51
52
assert_none (tree )
52
53
53
54
@raises (TypeError )
54
55
def test_content_from_string_invalid_type_should_raise (self ):
55
- Tree .content__from_string (None , "040000 bogus 650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44 test" )
56
+ Tree .content_from_string (None , "040000 bogus 650fa3f0c17f1edb4ae53d8dcca4ac59d86e6c44 test" )
56
57
57
58
@patch_object (Blob , 'size' )
58
59
@patch_object (Git , '_call_process' )
59
60
def test_slash (self , git , blob ):
60
61
git .return_value = fixture ('ls_tree_a' )
61
62
blob .return_value = 1
62
63
63
- tree = self .repo .tree ('master' )
64
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
64
65
65
66
assert_equal ('aa06ba24b4e3f463b3c4a85469d0fb9e5b421cf8' , (tree / 'lib' ).id )
66
67
assert_equal ('8b1e02c0fb554eed2ce2ef737a68bb369d7527df' , (tree / 'README.txt' ).id )
67
68
68
69
assert_true (git .called )
69
- assert_equal (git .call_args , (('ls_tree' , 'master' ), {}))
70
+
71
+ def test_traverse (self ):
72
+ root = self .repo .tree ()
73
+ num_recursive = 0
74
+ all_items = list ()
75
+ for obj in root .traverse ():
76
+ if "/" in obj .path :
77
+ num_recursive += 1
78
+
79
+ assert isinstance (obj , (Blob , Tree ))
80
+ all_items .append (obj )
81
+ # END for each object
82
+ # limit recursion level to 0 - should be same as default iteration
83
+ assert all_items
84
+ assert 'CHANGES' in root
85
+ assert len (list (root )) == len (list (root .traverse (max_depth = 0 )))
86
+
87
+ # only choose trees
88
+ trees_only = lambda i : i .type == "tree"
89
+ trees = list (root .traverse (predicate = trees_only ))
90
+ assert len (trees ) == len (list ( i for i in root .traverse () if trees_only (i ) ))
91
+
92
+ # trees and blobs
93
+ assert len (set (trees )| set (root .trees )) == len (trees )
94
+ assert len (set (b for b in root if isinstance (b , Blob )) | set (root .blobs )) == len ( root .blobs )
70
95
71
96
@patch_object (Blob , 'size' )
72
97
@patch_object (Git , '_call_process' )
73
98
def test_slash_with_zero_length_file (self , git , blob ):
74
99
git .return_value = fixture ('ls_tree_a' )
75
100
blob .return_value = 0
76
101
77
- tree = self .repo .tree ('master' )
102
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
78
103
79
104
assert_not_none (tree / 'README.txt' )
80
105
assert_equal ('8b1e02c0fb554eed2ce2ef737a68bb369d7527df' , (tree / 'README.txt' ).id )
81
106
82
107
assert_true (git .called )
83
- assert_equal (git .call_args , (('ls_tree' , 'master' ), {}))
84
108
85
109
@patch_object (Git , '_call_process' )
86
110
def test_slash_with_commits (self , git ):
87
111
git .return_value = fixture ('ls_tree_commit' )
88
112
89
- tree = self .repo .tree ('master' )
113
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
90
114
91
- assert_none ( tree / 'bar' )
115
+ self . failUnlessRaises ( KeyError , tree . __div__ , 'bar' )
92
116
assert_equal ('2afb47bcedf21663580d5e6d2f406f08f3f65f19' , (tree / 'foo' ).id )
93
117
assert_equal ('f623ee576a09ca491c4a27e48c0dfe04be5f4a2e' , (tree / 'baz' ).id )
94
118
95
119
assert_true (git .called )
96
- assert_equal (git .call_args , (('ls_tree' , 'master' ), {}))
97
120
98
121
@patch_object (Blob , 'size' )
99
122
@patch_object (Git , '_call_process' )
100
123
def test_dict (self , git , blob ):
101
124
git .return_value = fixture ('ls_tree_a' )
102
125
blob .return_value = 1
103
126
104
- tree = self .repo .tree ('master' )
127
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
105
128
106
129
assert_equal ('aa06ba24b4e3f463b3c4a85469d0fb9e5b421cf8' , tree ['lib' ].id )
107
130
assert_equal ('8b1e02c0fb554eed2ce2ef737a68bb369d7527df' , tree ['README.txt' ].id )
108
131
109
132
assert_true (git .called )
110
- assert_equal (git .call_args , (('ls_tree' , 'master' ), {}))
111
133
112
134
@patch_object (Blob , 'size' )
113
135
@patch_object (Git , '_call_process' )
114
136
def test_dict_with_zero_length_file (self , git , blob ):
115
137
git .return_value = fixture ('ls_tree_a' )
116
138
blob .return_value = 0
117
139
118
- tree = self .repo .tree ('master' )
140
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
119
141
120
142
assert_not_none (tree ['README.txt' ])
121
143
assert_equal ('8b1e02c0fb554eed2ce2ef737a68bb369d7527df' , tree ['README.txt' ].id )
122
144
123
145
assert_true (git .called )
124
- assert_equal (git .call_args , (('ls_tree' , 'master' ), {}))
125
146
126
147
@patch_object (Git , '_call_process' )
127
148
def test_dict_with_commits (self , git ):
128
149
git .return_value = fixture ('ls_tree_commit' )
129
150
130
- tree = self .repo .tree ('master' )
151
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
131
152
132
- assert_none ( tree .get ( 'bar' ) )
153
+ self . failUnlessRaises ( KeyError , tree .__getitem__ , 'bar' )
133
154
assert_equal ('2afb47bcedf21663580d5e6d2f406f08f3f65f19' , tree ['foo' ].id )
134
155
assert_equal ('f623ee576a09ca491c4a27e48c0dfe04be5f4a2e' , tree ['baz' ].id )
135
156
136
157
assert_true (git .called )
137
- assert_equal (git .call_args , (('ls_tree' , 'master' ), {}))
138
158
139
159
@patch_object (Git , '_call_process' )
140
160
@raises (KeyError )
141
161
def test_dict_with_non_existant_file (self , git ):
142
162
git .return_value = fixture ('ls_tree_commit' )
143
163
144
- tree = self .repo .tree ('master' )
164
+ tree = self .repo .tree (Head ( self . repo , 'master' ) )
145
165
tree ['bar' ]
146
166
147
167
def test_repr (self ):
0 commit comments