Extract #read_index for tests

This commit is contained in:
Brandon Keepers
2014-09-29 15:04:48 -04:00
parent 1c6483a499
commit 2b411aad90
2 changed files with 19 additions and 29 deletions

View File

@@ -110,22 +110,30 @@ module Linguist
if @old_commit_oid == @commit_oid if @old_commit_oid == @commit_oid
@old_stats @old_stats
else else
compute_stats(@old_commit_oid, @commit_oid, @old_stats) compute_stats(@old_commit_oid, @old_stats)
end end
end end
end end
def read_index
attr_index = Rugged::Index.new
attr_index.read_tree(current_tree)
repository.index = attr_index
end
def current_tree
@tree ||= Rugged::Commit.lookup(repository, @commit_oid).tree
end
protected protected
def compute_stats(old_commit_oid, commit_oid, cache = nil)
def compute_stats(old_commit_oid, cache = nil)
file_map = cache ? cache.dup : {} file_map = cache ? cache.dup : {}
old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree
new_tree = Rugged::Commit.lookup(repository, commit_oid).tree
diff = Rugged::Tree.diff(repository, old_tree, new_tree) read_index
attr_index = Rugged::Index.new diff = Rugged::Tree.diff(repository, old_tree, current_tree)
attr_index.read_tree(new_tree)
repository.index = attr_index
diff.each_delta do |delta| diff.each_delta do |delta|
old = delta.old_file[:path] old = delta.old_file[:path]

View File

@@ -68,51 +68,33 @@ class TestRepository < Test::Unit::TestCase
assert !repo.breakdown_by_file["Ruby"].empty? assert !repo.breakdown_by_file["Ruby"].empty?
end end
def test_linguist_override_generated? def test_linguist_override_generated?
attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33'
linguist_repo(attr_commit).read_index
file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Rakefile') file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Rakefile')
git_attrs = { "linguist-language" => nil,
"linguist-vendored" => nil,
"linguist-generated"=> true }
# TODO: get rid of this (would like this to come from git data)
file.stubs(:git_attributes).returns(git_attrs)
# overridden in .gitattributes # overridden in .gitattributes
assert file.generated? assert file.generated?
end end
def test_linguist_override_vendored? def test_linguist_override_vendored?
attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33'
repo = linguist_repo(attr_commit).read_index
override_vendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Gemfile') override_vendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Gemfile')
git_attrs = { "linguist-language" => nil,
"linguist-vendored" => true,
"linguist-generated"=> nil }
# TODO: get rid of this (would like this to come from git data)
override_vendored.stubs(:git_attributes).returns(git_attrs)
# overridden .gitattributes # overridden .gitattributes
assert override_vendored.vendored? assert override_vendored.vendored?
end end
def test_linguist_override_unvendored? def test_linguist_override_unvendored?
attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33'
repo = linguist_repo(attr_commit).read_index
# lib/linguist/vendor.yml defines this as vendored. # lib/linguist/vendor.yml defines this as vendored.
override_unvendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'test/fixtures/foo.rb') override_unvendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'test/fixtures/foo.rb')
git_attrs = { "linguist-language" => nil,
"linguist-vendored" => "false",
"linguist-generated"=> nil }
# TODO: get rid of this (would like this to come from git data)
override_unvendored.stubs(:git_attributes).returns(git_attrs)
# overridden .gitattributes # overridden .gitattributes
assert !override_unvendored.vendored? assert !override_unvendored.vendored?
end end