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_branch.rb | 49 ++++++++++++++++++ tests/units/test_diff.rb | 29 +++++++++++ tests/units/test_lib.rb | 124 +++++++++++++++++++++++++++++++++++++++++++++ tests/units/test_log.rb | 8 +-- tests/units/test_object.rb | 17 +++++++ 5 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 tests/units/test_branch.rb create mode 100644 tests/units/test_diff.rb create mode 100644 tests/units/test_lib.rb (limited to 'tests/units') diff --git a/tests/units/test_branch.rb b/tests/units/test_branch.rb new file mode 100644 index 0000000..ea242fc --- /dev/null +++ b/tests/units/test_branch.rb @@ -0,0 +1,49 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestBranch < Test::Unit::TestCase + def setup + set_file_paths + @git = Git.open(@wdir) + + @commit = @git.object('1cc8667014381') + @tree = @git.object('1cc8667014381^{tree}') + @blob = @git.object('v2.5:example.txt') + + @branches = @git.branches + end + + + def test_branches_all + assert(@git.branches[:master].is_a?(Git::Branch)) + assert(@git.branches.size > 5) + end + + def test_branches_local + bs = @git.branches.local + assert(bs.size > 4) + end + + def test_branches_remote + bs = @git.branches.remote + assert_equal(1, bs.size) + end + + def test_branches_single + b = @git.branches[:test_object] + assert_equal('test_object', b.name) + + b = @git.branches['working/master'] + assert_equal('master', b.name) + assert_equal('working/master', b.full) + assert_equal('working', b.remote.name) + assert_equal('+refs/heads/*:refs/remotes/working/*', b.remote.fetch) + assert_equal('../working.git', b.remote.url) + end + + def test_branch_commit + assert_equal(270, @git.branches[:test_branches].commit.size) + end + +end \ No newline at end of file diff --git a/tests/units/test_diff.rb b/tests/units/test_diff.rb new file mode 100644 index 0000000..b8ed6b8 --- /dev/null +++ b/tests/units/test_diff.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestDiff < Test::Unit::TestCase + def setup + set_file_paths + @git = Git.open(@wdir) + end + + def test_diff + end + + def test_diff_summary + end + + def test_diff_stat + end + + def test_diff_shortstat + end + + def test_patch + end + + def test_unified + end + +end \ No newline at end of file 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 diff --git a/tests/units/test_log.rb b/tests/units/test_log.rb index 770c245..b62c544 100644 --- a/tests/units/test_log.rb +++ b/tests/units/test_log.rb @@ -32,19 +32,19 @@ class TestLog < Test::Unit::TestCase end def test_get_log_since_file - l = @git.log.file('example.txt') + l = @git.log.object('example.txt') assert_equal(30, l.size) - l = @git.log.between('v2.5').file('example.txt') + l = @git.log.between('v2.5').object('example.txt') assert_equal(2, l.size) - l = @git.log.between('v2.5', 'test').file('example.txt') + l = @git.log.between('v2.5', 'test').object('example.txt') assert_equal(1, l.size) end def test_log_file_noexist assert_raise Git::GitExecuteError do - @git.log.file('no-exist.txt').size + @git.log.object('no-exist.txt').size end end diff --git a/tests/units/test_object.rb b/tests/units/test_object.rb index 44bfc57..df565d5 100644 --- a/tests/units/test_object.rb +++ b/tests/units/test_object.rb @@ -76,4 +76,21 @@ class TestObject < Test::Unit::TestCase assert_equal('1f09f2edb9c0d9275d15960771b363ca6940fbe3', sha) end + def test_grep + g = @git.tree('a3db7143944dcfa0').grep('search') # there + assert_equal(3, g.to_a.flatten.size) + assert_equal(1, g.size) + + assert_equal({}, @git.tree('a3db7143944dcfa0').grep('34a566d193')) # not there + + g = @git.commit('gitsearch1').grep('search') # there + assert_equal(8, g.to_a.flatten.size) + assert_equal(2, g.size) + + g = @git.commit('gitsearch1').grep('search', 'scott/new*') # there + assert_equal(3, g.to_a.flatten.size) + assert_equal(1, g.size) + end + + end \ No newline at end of file -- cgit