public
Fork of mojombo/grit
Description: Grit is a Ruby library for extracting information from a git repository in an object oriented manner.
Homepage: http://grit.rubyforge.org/
Clone URL: git://github.com/technoweenie/grit.git
grit / test / test_repo.rb
100644 271 lines (194 sloc) 7.053 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require File.dirname(__FILE__) + '/helper'
 
class TestRepo < Test::Unit::TestCase
  def setup
    @r = Repo.new(GRIT_REPO)
  end
  
  # new
  
  def test_new_should_raise_on_invalid_repo_location
    assert_raise(InvalidGitRepositoryError) do
      Repo.new("/tmp")
    end
  end
  
  def test_new_should_raise_on_non_existant_path
    assert_raise(NoSuchPathError) do
      Repo.new("/foobar")
    end
  end
  
  # descriptions
  
  def test_description
    assert_equal "Unnamed repository; edit this file to name it for gitweb.", @r.description
  end
  
  # heads
  
  def test_heads_should_return_array_of_head_objects
    @r.heads.each do |head|
      assert_equal Grit::Head, head.class
    end
  end
  
  def test_heads_should_populate_head_data
    Git.any_instance.expects(:for_each_ref).returns(fixture('for_each_ref'))
    
    head = @r.heads.first
    
    assert_equal 'master', head.name
    assert_equal '634396b2f541a9f2d58b00be1a07f0c358b999b3', head.commit.id
  end
  
  # branches
  
  def test_branches
    # same as heads
  end
  
  # commits
  
  def test_commits
    Git.any_instance.expects(:rev_list).returns(fixture('rev_list'))
    
    commits = @r.commits('master', 10)
    
    c = commits[0]
    assert_equal '4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id
    assert_equal ["634396b2f541a9f2d58b00be1a07f0c358b999b3"], c.parents.map { |p| p.id }
    assert_equal "672eca9b7f9e09c22dcb128c283e8c3c8d7697a4", c.tree.id
    assert_equal "Tom Preston-Werner", c.author.name
    assert_equal "tom@mojombo.com", c.author.email
    assert_equal Time.at(1191999972), c.authored_date
    assert_equal "Tom Preston-Werner", c.committer.name
    assert_equal "tom@mojombo.com", c.committer.email
    assert_equal Time.at(1191999972), c.committed_date
    assert_equal "implement Grit#heads", c.message
    
    c = commits[1]
    assert_equal [], c.parents
    
    c = commits[2]
    assert_equal ["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], c.parents.map { |p| p.id }
    assert_equal "Merge branch 'site'\n\n * Some other stuff\n * just one more", c.message
    assert_equal "Merge branch 'site'", c.short_message
  end
  
  # commit_count
  
  def test_commit_count
    Git.any_instance.expects(:rev_list).with({}, 'master').returns(fixture('rev_list_count'))
    
    assert_equal 655, @r.commit_count('master')
  end
  
  # commit
  
  def test_commit
    commit = @r.commit('634396b2f541a9f2d58b00be1a07f0c358b999b3')
    
    assert_equal "634396b2f541a9f2d58b00be1a07f0c358b999b3", commit.id
  end
  
  # tree
  
  def test_tree
    Git.any_instance.expects(:ls_tree).returns(fixture('ls_tree_a'))
    tree = @r.tree('master')
    
    assert_equal 4, tree.contents.select { |c| c.instance_of?(Blob) }.size
    assert_equal 3, tree.contents.select { |c| c.instance_of?(Tree) }.size
  end
  
  # blob
  
  def test_blob
    Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob'))
    blob = @r.blob("abc")
    assert_equal "Hello world", blob.data
  end
  
  # init_bare
  
  def test_init_bare
    Git.any_instance.expects(:init).returns(true)
    Repo.expects(:new).with("/foo/bar.git")
    Repo.init_bare("/foo/bar.git")
  end
  
  def test_init_bare_with_options
    Git.any_instance.expects(:init).with(
      :template => "/baz/sweet").returns(true)
    Repo.expects(:new).with("/foo/bar.git")
    Repo.init_bare("/foo/bar.git", :template => "/baz/sweet")
  end
  
  # fork_bare
  
  def test_fork_bare
    Git.any_instance.expects(:clone).with(
      {:bare => true, :shared => true},
      "#{absolute_project_path}/.git",
      "/foo/bar.git").returns(nil)
    Repo.expects(:new)
      
    @r.fork_bare("/foo/bar.git")
  end
  
  def test_fork_bare_with_options
    Git.any_instance.expects(:clone).with(
      {:bare => true, :shared => true, :template => '/awesome'},
      "#{absolute_project_path}/.git",
      "/foo/bar.git").returns(nil)
    Repo.expects(:new)
      
    @r.fork_bare("/foo/bar.git", :template => '/awesome')
  end
  
  # diff
  
  def test_diff
    Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--')
    @r.diff('master^', 'master')
    
    Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar')
    @r.diff('master^', 'master', 'foo/bar')
    
    Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar', 'foo/baz')
    @r.diff('master^', 'master', 'foo/bar', 'foo/baz')
  end
  
  # commit_diff
  
  def test_diff
    Git.any_instance.expects(:diff).returns(fixture('diff_p'))
    diffs = @r.commit_diff('master')
    
    assert_equal 15, diffs.size
  end
  
  # init bare
  
  # archive
  
  def test_archive_tar
    @r.archive_tar
  end
  
  # archive_tar_gz
  
  def test_archive_tar_gz
    @r.archive_tar_gz
  end
  
  # enable_daemon_serve
  
  def test_enable_daemon_serve
    FileUtils.expects(:touch).with(File.join(@r.path, '.git', 'git-daemon-export-ok'))
    @r.enable_daemon_serve
  end
  
  # disable_daemon_serve
  
  def test_disable_daemon_serve
    FileUtils.expects(:rm_f).with(File.join(@r.path, '.git', 'git-daemon-export-ok'))
    @r.disable_daemon_serve
  end
  
  # alternates
  
  def test_alternates_with_two_alternates
    File.expects(:exist?).with("#{absolute_project_path}/.git/objects/info/alternates").returns(true)
    File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")
    
    assert_equal ["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], @r.alternates
  end
  
  def test_alternates_no_file
    File.expects(:exist?).returns(false)
    
    assert_equal [], @r.alternates
  end
  
  # alternates=
  
  def test_alternates_setter_ok
    alts = %w{/path/to/repo.git/objects /path/to/repo2.git/objects}
    
    alts.each do |alt|
      File.expects(:exist?).with(alt).returns(true)
    end
    
    File.any_instance.expects(:write).with(alts.join("\n"))
    
    assert_nothing_raised do
      @r.alternates = alts
    end
  end
  
  def test_alternates_setter_bad
    alts = %w{/path/to/repo.git/objects}
    
    alts.each do |alt|
      File.expects(:exist?).with(alt).returns(false)
    end
    
    File.any_instance.expects(:write).never
    
    assert_raise RuntimeError do
      @r.alternates = alts
    end
  end
  
  def test_alternates_setter_empty
    File.expects(:delete)
    
    @r.alternates = []
  end
  
  # inspect
  
  def test_inspect
    assert_equal %Q{#<Grit::Repo "#{File.expand_path(GRIT_REPO)}/.git">}, @r.inspect
  end
 
  # log
 
  def test_log
    Git.any_instance.expects(:log).times(2).with({:pretty => 'raw'}, 'master').returns(fixture('rev_list'))
 
    assert_equal '4c8124ffcf4039d292442eeccabdeca5af5c5017', @r.log.first.id
    assert_equal 'ab25fd8483882c3bda8a458ad2965d2248654335', @r.log.last.id
  end
 
  def test_log_with_path_and_options
    Git.any_instance.expects(:log).with({:pretty => 'raw', :max_count => 1}, 'master', '--', 'file.rb').returns(fixture('rev_list'))
    @r.log('master', 'file.rb', :max_count => 1)
  end
end