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