added branches, more log stuff, better tests, changed the log api a bit
[rubygit.git] / tests / units / test_lib.rb
blob03a4411701ae3583b9204cdd124558783dedfe97
1 #!/usr/bin/env ruby
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
12   def setup
13     set_file_paths
14     @lib = Git.open(@wdir).lib
15   end
17   # takes parameters, returns array of appropriate commit objects
18   # :count
19   # :since
20   # :between
21   # :object
22   def test_log_commits
23     a = @lib.log_commits :count => 10
24     assert(a.first.is_a?(String))
25     assert_equal(10, a.size)
26     
27     a = @lib.log_commits :count => 20, :since => '3 years ago'
28     assert(a.first.is_a?(String))
29     assert_equal(20, a.size)
30     
31     a = @lib.log_commits :count => 20, :since => '1 second ago'
32     assert_equal(0, a.size)
33     
34     a = @lib.log_commits :count => 20, :between => ['v2.5', 'v2.6']
35     assert_equal(2, a.size)
36     
37     a = @lib.log_commits :count => 20, :object => 'example.txt'
38     assert_equal(20, a.size)
39     
40     a = @lib.log_commits :count => 20, :object => 'ex_dir/ex.txt'
41     assert_equal(1, a.size)
42   end
43   
44   def test_revparse
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
48   end
49   
50   def test_object_type
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'))
55   end
56   
57   def test_object_size
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'))
62   end
63   
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"
69     commit += "\ntest"
70     assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit
71     
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
75     
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
78     
79   end
81   # returns Git::Branch object array
82   def test_branches_all
83     branches = @lib.branches_all
84     assert(branches.size > 0)
85     assert(branches.select { |b| b.current }.size > 0)  # has a current branch
86     assert(branches.select { |b| b.remote }.size > 0)   # has a remote branch
87     assert(branches.select { |b| !b.remote }.size > 0)  # has a local branch
88     assert(branches.select { |b| b.name == 'master' }.size > 0)  # has a master branch
89   end
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'])
95   end
96   
97   # options this will accept
98   #  :treeish
99   #  :path_limiter
100   #  :ignore_case (bool)
101   #  :invert_match (bool)
102   def test_grep
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)
107     
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)
114         
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)
118     
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)
122   end
123