From 9d59d2965184964ab6662282ef5f9ceac2c58552 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Fri, 9 Nov 2007 13:11:22 -0800 Subject: added branches, more log stuff, better tests, changed the log api a bit added tests for Git::Lib, started Git::Diff development --- tests/units/test_lib.rb | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 tests/units/test_lib.rb (limited to 'tests/units/test_lib.rb') diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb new file mode 100644 index 0000000..03a4411 --- /dev/null +++ b/tests/units/test_lib.rb @@ -0,0 +1,124 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +# tests all the low level git communication +# +# this will be helpful if we ever figure out how +# to either build these in pure ruby or get git bindings working +# because right now it forks for every call + +class TestLib < Test::Unit::TestCase + def setup + set_file_paths + @lib = Git.open(@wdir).lib + end + + # takes parameters, returns array of appropriate commit objects + # :count + # :since + # :between + # :object + def test_log_commits + a = @lib.log_commits :count => 10 + assert(a.first.is_a?(String)) + assert_equal(10, a.size) + + a = @lib.log_commits :count => 20, :since => '3 years ago' + assert(a.first.is_a?(String)) + assert_equal(20, a.size) + + a = @lib.log_commits :count => 20, :since => '1 second ago' + assert_equal(0, a.size) + + a = @lib.log_commits :count => 20, :between => ['v2.5', 'v2.6'] + assert_equal(2, a.size) + + a = @lib.log_commits :count => 20, :object => 'example.txt' + assert_equal(20, a.size) + + a = @lib.log_commits :count => 20, :object => 'ex_dir/ex.txt' + assert_equal(1, a.size) + end + + def test_revparse + assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.revparse('1cc8667014381')) # commit + assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree + assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob + end + + def test_object_type + assert_equal('commit', @lib.object_type('1cc8667014381')) # commit + assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree + assert_equal('blob', @lib.object_type('v2.5:example.txt')) #blob + assert_equal('commit', @lib.object_type('v2.5')) + end + + def test_object_size + assert_equal(265, @lib.object_size('1cc8667014381')) # commit + assert_equal(72, @lib.object_size('1cc8667014381^{tree}')) #tree + assert_equal(128, @lib.object_size('v2.5:example.txt')) #blob + assert_equal(265, @lib.object_size('v2.5')) + end + + def test_object_contents + commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n" + commit += "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n" + commit += "author scott Chacon 1194561188 -0800\n" + commit += "committer scott Chacon 1194561188 -0800\n" + commit += "\ntest" + assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit + + tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n" + tree += "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt" + assert_equal(tree, @lib.object_contents('1cc8667014381^{tree}')) #tree + + 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" + assert_equal(blob, @lib.object_contents('v2.5:example.txt')) #blob + + end + + # returns Git::Branch object array + def test_branches_all + branches = @lib.branches_all + assert(branches.size > 0) + assert(branches.select { |b| b.current }.size > 0) # has a current branch + assert(branches.select { |b| b.remote }.size > 0) # has a remote branch + assert(branches.select { |b| !b.remote }.size > 0) # has a local branch + assert(branches.select { |b| b.name == 'master' }.size > 0) # has a master branch + end + + def test_config_remote + config = @lib.config_remote('working') + assert_equal('../working.git', config['url']) + assert_equal('+refs/heads/*:refs/remotes/working/*', config['fetch']) + end + + # options this will accept + # :treeish + # :path_limiter + # :ignore_case (bool) + # :invert_match (bool) + def test_grep + match = @lib.grep('search', :object => 'gitsearch1') + assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1]) + assert_equal(2, match['gitsearch1:scott/text.txt'].size) + assert_equal(2, match.size) + + match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => 'scott/new*') + assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1]) + assert_equal(1, match.size) + + match = @lib.grep('SEARCH', :object => 'gitsearch1') + assert_equal(0, match.size) + + match = @lib.grep('SEARCH', :object => 'gitsearch1', :ignore_case => true) + assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1]) + assert_equal(2, match.size) + + match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true) + assert_equal(6, match['gitsearch1:scott/text.txt'].size) + assert_equal(2, match.size) + end + +end \ No newline at end of file -- cgit