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
17 # takes parameters, returns array of appropriate commit objects
23 a = @lib.log_commits :count => 10
24 assert(a.first.is_a?(String))
25 assert_equal(10, a.size)
27 a = @lib.log_commits :count => 20, :since => '3 years ago'
28 assert(a.first.is_a?(String))
29 assert_equal(20, a.size)
31 a = @lib.log_commits :count => 20, :since => '1 second ago'
32 assert_equal(0, a.size)
34 a = @lib.log_commits :count => 20, :between => ['v2.5', 'v2.6']
35 assert_equal(2, a.size)
37 a = @lib.log_commits :count => 20, :object => 'example.txt'
38 assert_equal(20, a.size)
40 a = @lib.log_commits :count => 20, :object => 'ex_dir/ex.txt'
41 assert_equal(1, a.size)
45 assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.revparse('1cc8667014381')) # commit
46 assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree
47 assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob
51 assert_equal('commit', @lib.object_type('1cc8667014381')) # commit
52 assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree
53 assert_equal('blob', @lib.object_type('v2.5:example.txt')) #blob
54 assert_equal('commit', @lib.object_type('v2.5'))
58 assert_equal(265, @lib.object_size('1cc8667014381')) # commit
59 assert_equal(72, @lib.object_size('1cc8667014381^{tree}')) #tree
60 assert_equal(128, @lib.object_size('v2.5:example.txt')) #blob
61 assert_equal(265, @lib.object_size('v2.5'))
64 def test_object_contents
65 commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n"
66 commit += "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n"
67 commit += "author scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
68 commit += "committer scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
70 assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit
72 tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n"
73 tree += "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt"
74 assert_equal(tree, @lib.object_contents('1cc8667014381^{tree}')) #tree
76 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"
77 assert_equal(blob, @lib.object_contents('v2.5:example.txt')) #blob
81 # returns Git::Branch object array
83 branches = @lib.branches_all
84 assert(branches.size > 0)
85 assert(branches.select { |b| b[1] }.size > 0) # has a current branch
86 assert(branches.select { |b| /\//.match(b[0]) }.size > 0) # has a remote branch
87 assert(branches.select { |b| !/\//.match(b[0]) }.size > 0) # has a local branch
88 assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch
91 def test_config_remote
92 config = @lib.config_remote('working')
93 assert_equal('../working.git', config['url'])
94 assert_equal('+refs/heads/*:refs/remotes/working/*', config['fetch'])
97 # options this will accept
100 # :ignore_case (bool)
101 # :invert_match (bool)
103 match = @lib.grep('search', :object => 'gitsearch1')
104 assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1])
105 assert_equal(2, match['gitsearch1:scott/text.txt'].size)
106 assert_equal(2, match.size)
108 match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => 'scott/new*')
109 assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
110 assert_equal(1, match.size)
112 match = @lib.grep('SEARCH', :object => 'gitsearch1')
113 assert_equal(0, match.size)
115 match = @lib.grep('SEARCH', :object => 'gitsearch1', :ignore_case => true)
116 assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
117 assert_equal(2, match.size)
119 match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true)
120 assert_equal(6, match['gitsearch1:scott/text.txt'].size)
121 assert_equal(2, match.size)