3 require File.dirname(__FILE__) + '/../test_helper'
5 # tests all the low level git communication
7 # this will be helpful if we ever figure out how
8 # to either build these in pure ruby or get git bindings working
9 # because right now it forks for every call
11 class TestLib < Test::Unit::TestCase
14 @lib = Git.open(@wdir).lib
18 data = @lib.commit_data('1cc8667014381')
19 assert_equal('scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800', data['author'])
20 assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', data['tree'])
21 assert_equal("test\n", data['message'])
22 assert_equal(["546bec6f8872efa41d5d97a369f669165ecda0de"], data['parent'])
25 # takes parameters, returns array of appropriate commit objects
31 a = @lib.log_commits :count => 10
32 assert(a.first.is_a?(String))
33 assert_equal(10, a.size)
35 a = @lib.log_commits :count => 20, :since => '3 years ago'
36 assert(a.first.is_a?(String))
37 assert_equal(20, a.size)
39 a = @lib.log_commits :count => 20, :since => '1 second ago'
40 assert_equal(0, a.size)
42 a = @lib.log_commits :count => 20, :between => ['v2.5', 'v2.6']
43 assert_equal(2, a.size)
45 a = @lib.log_commits :count => 20, :path_limiter => 'ex_dir/'
46 assert_equal(1, a.size)
48 a = @lib.full_log_commits :count => 20
49 assert_equal(20, a.size)
53 assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.revparse('1cc8667014381')) # commit
54 assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree
55 assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob
59 assert_equal('commit', @lib.object_type('1cc8667014381')) # commit
60 assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree
61 assert_equal('blob', @lib.object_type('v2.5:example.txt')) #blob
62 assert_equal('commit', @lib.object_type('v2.5'))
66 assert_equal(265, @lib.object_size('1cc8667014381')) # commit
67 assert_equal(72, @lib.object_size('1cc8667014381^{tree}')) #tree
68 assert_equal(128, @lib.object_size('v2.5:example.txt')) #blob
69 assert_equal(265, @lib.object_size('v2.5'))
72 def test_object_contents
73 commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n"
74 commit += "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n"
75 commit += "author scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
76 commit += "committer scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
78 assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit
80 tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n"
81 tree += "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt"
82 assert_equal(tree, @lib.object_contents('1cc8667014381^{tree}')) #tree
84 blob = "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2"
85 assert_equal(blob, @lib.object_contents('v2.5:example.txt')) #blob
89 # returns Git::Branch object array
91 branches = @lib.branches_all
92 assert(branches.size > 0)
93 assert(branches.select { |b| b[1] }.size > 0) # has a current branch
94 assert(branches.select { |b| /\//.match(b[0]) }.size > 0) # has a remote branch
95 assert(branches.select { |b| !/\//.match(b[0]) }.size > 0) # has a local branch
96 assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch
99 def test_config_remote
100 config = @lib.config_remote('working')
101 assert_equal('../working.git', config['url'])
102 assert_equal('+refs/heads/*:refs/remotes/working/*', config['fetch'])
107 tree = @lib.ls_tree('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7')
108 assert_equal("3aac4b445017a8fc07502670ec2dbf744213dd48", tree['blob']['example.txt'][:sha])
109 assert_equal("100644", tree['blob']['example.txt'][:mode])
114 # options this will accept
117 # :ignore_case (bool)
118 # :invert_match (bool)
120 match = @lib.grep('search', :object => 'gitsearch1')
121 assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1])
122 assert_equal(2, match['gitsearch1:scott/text.txt'].size)
123 assert_equal(2, match.size)
125 match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => 'scott/new*')
126 assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
127 assert_equal(1, match.size)
129 match = @lib.grep('SEARCH', :object => 'gitsearch1')
130 assert_equal(0, match.size)
132 match = @lib.grep('SEARCH', :object => 'gitsearch1', :ignore_case => true)
133 assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
134 assert_equal(2, match.size)
136 match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true)
137 assert_equal(6, match['gitsearch1:scott/text.txt'].size)
138 assert_equal(2, match.size)