/test/test_repo.rb

http://github.com/mojombo/grit · Ruby · 419 lines · 280 code · 104 blank · 35 comment · 4 complexity · 0216feda5d97e75c912b71f701d74200 MD5 · raw file

  1. require File.dirname(__FILE__) + '/helper'
  2. class TestRepo < Test::Unit::TestCase
  3. def setup
  4. @r = Repo.new(GRIT_REPO)
  5. end
  6. def create_temp_repo(clone_path)
  7. filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
  8. tmp_path = File.join("/tmp/", filename)
  9. FileUtils.mkdir_p(tmp_path)
  10. FileUtils.cp_r(clone_path, tmp_path)
  11. File.join(tmp_path, 'dot_git')
  12. end
  13. def test_update_refs_packed
  14. gpath = create_temp_repo(File.join(File.dirname(__FILE__), *%w[dot_git]))
  15. @git = Grit::Repo.new(gpath, :is_bare => true)
  16. # new and existing
  17. test = 'ac9a30f5a7f0f163bbe3b6f0abf18a6c83b06872'
  18. master = 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a'
  19. @git.update_ref('testref', test)
  20. new_t = @git.get_head('testref').commit.sha
  21. assert new_t != master
  22. @git.update_ref('master', test)
  23. new_m = @git.get_head('master').commit.sha
  24. assert new_m != master
  25. old = @git.get_head('nonpack').commit.sha
  26. @git.update_ref('nonpack', test)
  27. newp = @git.get_head('nonpack').commit.sha
  28. assert newp != old
  29. FileUtils.rm_r(gpath)
  30. end
  31. # new
  32. def test_new_should_raise_on_invalid_repo_location
  33. assert_raise(InvalidGitRepositoryError) do
  34. Repo.new("/tmp")
  35. end
  36. end
  37. def test_new_should_raise_on_non_existant_path
  38. assert_raise(NoSuchPathError) do
  39. Repo.new("/foobar")
  40. end
  41. end
  42. # descriptions
  43. def test_description
  44. assert @r.description.include?("Unnamed repository; edit this file")
  45. end
  46. # refs
  47. def test_refs_should_return_array_of_ref_objects
  48. @r.refs.each do |ref|
  49. assert ref.is_a?(Grit::Ref)
  50. end
  51. end
  52. # heads
  53. def test_current_head
  54. @r = Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git]), :is_bare => true)
  55. head = @r.head
  56. assert_equal Grit::Head, head.class
  57. assert_equal 'master', head.name
  58. assert_equal 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a', @r.commits(head.name).first.id
  59. end
  60. def test_heads_should_return_array_of_head_objects
  61. @r.heads.each do |head|
  62. assert_equal Grit::Head, head.class
  63. end
  64. end
  65. def test_heads_should_populate_head_data
  66. @r = Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git]), :is_bare => true)
  67. head = @r.heads[1]
  68. assert_equal 'test/master', head.name
  69. assert_equal '2d3acf90f35989df8f262dc50beadc4ee3ae1560', head.commit.id
  70. end
  71. # branches
  72. def test_branches
  73. # same as heads
  74. end
  75. # commits
  76. def test_commits
  77. Git.any_instance.expects(:rev_list).returns(fixture('rev_list'))
  78. commits = @r.commits('master', 10)
  79. c = commits[0]
  80. assert_equal '4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id
  81. assert_equal ["634396b2f541a9f2d58b00be1a07f0c358b999b3"], c.parents.map { |p| p.id }
  82. assert_equal "672eca9b7f9e09c22dcb128c283e8c3c8d7697a4", c.tree.id
  83. assert_equal "Tom Preston-Werner", c.author.name
  84. assert_equal "tom@mojombo.com", c.author.email
  85. assert_equal Time.at(1191999972), c.authored_date
  86. assert_equal "Tom Preston-Werner", c.committer.name
  87. assert_equal "tom@mojombo.com", c.committer.email
  88. assert_equal Time.at(1191999972), c.committed_date
  89. assert_equal "implement Grit#heads", c.message
  90. c = commits[1]
  91. assert_equal [], c.parents
  92. c = commits[2]
  93. assert_equal ["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], c.parents.map { |p| p.id }
  94. assert_equal "Merge branch 'site'\n\n * Some other stuff\n * just one more", c.message
  95. assert_equal "Merge branch 'site'", c.short_message
  96. end
  97. def test_commit_batch
  98. commits = @r.batch('4c8124ffcf4039d292442eeccabdeca5af5c5017',
  99. '634396b2f541a9f2d58b00be1a07f0c358b999b3')
  100. assert_equal "4c8124ffcf4039d292442eeccabdeca5af5c5017", commits[0].id
  101. assert_equal "634396b2f541a9f2d58b00be1a07f0c358b999b3", commits[1].id
  102. assert_equal "tom@mojombo.com", commits[0].author.email
  103. assert_equal "tom@mojombo.com", commits[1].author.email
  104. end
  105. def test_commit_batch_with_non_commit_objects
  106. commits = @r.batch(
  107. '4c8124ffcf4039d292442eeccabdeca5af5c5017',
  108. '608b0482499341bd2fe32002192936f7241a8569', # this is a blob SHA1
  109. '634396b2f541a9f2d58b00be1a07f0c358b999b3'
  110. )
  111. assert_equal 3, commits.size
  112. assert_nil commits[1]
  113. assert_equal "4c8124ffcf4039d292442eeccabdeca5af5c5017", commits[0].id
  114. assert_equal "634396b2f541a9f2d58b00be1a07f0c358b999b3", commits[2].id
  115. assert_equal "tom@mojombo.com", commits[0].author.email
  116. assert_equal "tom@mojombo.com", commits[2].author.email
  117. end
  118. # generate enough input to overflow the max pipe input buffer. this will cause
  119. # the git child process hang if stdin is not written at the same time as stdout
  120. # is being read.
  121. #
  122. # The pipe buffer is 32K on Mac, 64K on Linux 2.6.
  123. def test_large_commit_batch
  124. fail if jruby?
  125. n = 1000 # 41K of input
  126. commits = @r.batch(['4c8124ffcf4039d292442eeccabdeca5af5c5017'] * n)
  127. assert_equal n, commits.size
  128. end
  129. # commit_count
  130. def test_commit_count
  131. Git.any_instance.expects(:rev_list).with({}, 'master').returns(fixture('rev_list_count'))
  132. assert_equal 655, @r.commit_count('master')
  133. end
  134. # commit
  135. def test_commit
  136. commit = @r.commit('634396b2f541a9f2d58b00be1a07f0c358b999b3')
  137. assert_equal "634396b2f541a9f2d58b00be1a07f0c358b999b3", commit.id
  138. end
  139. # tree
  140. def test_tree
  141. Git.any_instance.expects(:ls_tree).returns(fixture('ls_tree_a'))
  142. tree = @r.tree('master')
  143. assert_equal 4, tree.contents.select { |c| c.instance_of?(Blob) }.size
  144. assert_equal 3, tree.contents.select { |c| c.instance_of?(Tree) }.size
  145. end
  146. # blob
  147. def test_blob
  148. Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob'))
  149. blob = @r.blob("abc")
  150. assert_equal "Hello world", blob.data
  151. end
  152. # init
  153. def test_init
  154. FileUtils.stubs(:mkdir_p)
  155. Git.any_instance.expects(:init).with({:base => false}, "/foo/bar").returns(true)
  156. Repo.expects(:new).with("/foo/bar", {})
  157. Repo.init("/foo/bar")
  158. end
  159. # init_bare
  160. def test_init_bare
  161. FileUtils.stubs(:mkdir_p)
  162. Git.any_instance.expects(:init).with(:bare => true).returns(true).twice
  163. Repo.expects(:new).with("/foo/bar.git", {:is_bare => true})
  164. Repo.init_bare("/foo/bar.git")
  165. Repo.expects(:new).with("/foo/bar", {:is_bare => true})
  166. Repo.init_bare("/foo/bar")
  167. end
  168. def test_init_bare_with_options
  169. FileUtils.stubs(:mkdir_p)
  170. Git.any_instance.expects(:init).with(
  171. :bare => true, :template => "/baz/sweet").returns(true)
  172. Repo.expects(:new).with("/foo/bar.git", {:is_bare => true})
  173. Repo.init_bare("/foo/bar.git", :template => "/baz/sweet")
  174. end
  175. # fork_bare
  176. def test_fork_bare
  177. FileUtils.stubs(:mkdir_p)
  178. Git.any_instance.expects(:clone).with(
  179. {:bare => true, :shared => true},
  180. "#{absolute_project_path}/.git",
  181. "/foo/bar.git").returns(nil)
  182. Repo.expects(:new)
  183. @r.fork_bare("/foo/bar.git")
  184. end
  185. def test_fork_bare_with_options
  186. FileUtils.stubs(:mkdir_p)
  187. Git.any_instance.expects(:clone).with(
  188. {:bare => true, :shared => true, :template => '/awesome'},
  189. "#{absolute_project_path}/.git",
  190. "/foo/bar.git").returns(nil)
  191. Repo.expects(:new)
  192. @r.fork_bare("/foo/bar.git", :template => '/awesome')
  193. end
  194. # diff
  195. def test_diff
  196. Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--')
  197. @r.diff('master^', 'master')
  198. Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar')
  199. @r.diff('master^', 'master', 'foo/bar')
  200. Git.any_instance.expects(:diff).with({}, 'master^', 'master', '--', 'foo/bar', 'foo/baz')
  201. @r.diff('master^', 'master', 'foo/bar', 'foo/baz')
  202. end
  203. def test_diff2
  204. Git.any_instance.expects(:native).with('diff', {}, 'a', 'b', '--').returns(fixture('diff_p'))
  205. diffs = @r.diff('a', 'b')
  206. assert_equal 15, diffs.size
  207. end
  208. # commit_diff
  209. def test_diff
  210. Git.any_instance.expects(:diff).returns(fixture('diff_p'))
  211. diffs = @r.commit_diff('master')
  212. assert_equal 15, diffs.size
  213. end
  214. # init bare
  215. # archive
  216. def test_archive_tar
  217. #@r.archive_tar -- no assertion being done here
  218. end
  219. # archive_tar_gz
  220. def test_archive_tar_gz
  221. #@r.archive_tar_gz -- again, no assertion
  222. end
  223. # enable_daemon_serve
  224. def test_enable_daemon_serve
  225. f = stub
  226. f.expects("write").with('')
  227. File.expects(:open).with(File.join(@r.path, 'git-daemon-export-ok'), 'w').yields(f)
  228. @r.enable_daemon_serve
  229. end
  230. # disable_daemon_serve
  231. def test_disable_daemon_serve
  232. FileUtils.expects(:rm_rf).with(File.join(@r.path, 'git-daemon-export-ok'))
  233. @r.disable_daemon_serve
  234. end
  235. def test_gc_auto
  236. Git.any_instance.expects(:gc).with({:auto => true})
  237. @r.gc_auto
  238. end
  239. # alternates
  240. def test_alternates_with_two_alternates
  241. File.expects(:read).with("#{absolute_project_path}/.git/objects/info/alternates").returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")
  242. assert_equal ["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], @r.alternates
  243. end
  244. def test_alternates_no_file
  245. File.expects(:read).raises(Errno::ENOENT)
  246. assert_equal [], @r.alternates
  247. end
  248. # alternates=
  249. def test_alternates_setter_ok
  250. alts = %w{/path/to/repo.git/objects /path/to/repo2.git/objects}
  251. alts.each do |alt|
  252. File.expects(:exist?).with(alt).returns(true)
  253. end
  254. File.any_instance.expects(:write).with(alts.join("\n"))
  255. assert_nothing_raised do
  256. @r.alternates = alts
  257. end
  258. end
  259. def test_alternates_setter_bad
  260. alts = %w{/path/to/repo.git/objects}
  261. alts.each do |alt|
  262. File.expects(:exist?).with(alt).returns(false)
  263. end
  264. File.any_instance.expects(:write).never
  265. assert_raise RuntimeError do
  266. @r.alternates = alts
  267. end
  268. end
  269. def test_alternates_setter_empty
  270. File.any_instance.expects(:write)
  271. @r.alternates = []
  272. end
  273. # inspect
  274. def test_inspect
  275. assert_equal %Q{#<Grit::Repo "#{File.expand_path(GRIT_REPO)}/.git">}, @r.inspect
  276. end
  277. # log
  278. def test_log
  279. Git.any_instance.expects(:log).times(2).with({:pretty => 'raw'}, 'master').returns(fixture('rev_list'))
  280. assert_equal '4c8124ffcf4039d292442eeccabdeca5af5c5017', @r.log.first.id
  281. assert_equal 'ab25fd8483882c3bda8a458ad2965d2248654335', @r.log.last.id
  282. end
  283. def test_log_with_path_and_options
  284. Git.any_instance.expects(:log).with({:pretty => 'raw', :max_count => 1}, 'master', '--', 'file.rb').returns(fixture('rev_list'))
  285. @r.log('master', 'file.rb', :max_count => 1)
  286. end
  287. # commit_deltas_from
  288. def test_commit_deltas_from_nothing_new
  289. other_repo = Repo.new(GRIT_REPO)
  290. @r.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
  291. other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
  292. delta_blobs = @r.commit_deltas_from(other_repo)
  293. assert_equal 0, delta_blobs.size
  294. end
  295. def test_commit_deltas_from_when_other_has_new
  296. other_repo = Repo.new(GRIT_REPO)
  297. @r.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
  298. other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
  299. %w[
  300. 4c8124ffcf4039d292442eeccabdeca5af5c5017
  301. 634396b2f541a9f2d58b00be1a07f0c358b999b3
  302. ab25fd8483882c3bda8a458ad2965d2248654335
  303. ].each do |ref|
  304. Commit.expects(:find_all).with(other_repo, ref, :max_count => 1).returns([stub()])
  305. end
  306. delta_blobs = @r.commit_deltas_from(other_repo)
  307. assert_equal 3, delta_blobs.size
  308. end
  309. # object_exist
  310. def test_select_existing_objects
  311. before = ['634396b2f541a9f2d58b00be1a07f0c358b999b3', 'deadbeef']
  312. after = ['634396b2f541a9f2d58b00be1a07f0c358b999b3']
  313. assert_equal after, @r.git.select_existing_objects(before)
  314. end
  315. end