command mocking refactored

This commit is contained in:
Tomasz Gieniusz 2012-10-16 21:31:25 +02:00
parent d26c261a18
commit 0cb7b52591
3 changed files with 17 additions and 22 deletions

View File

@ -9,12 +9,12 @@ module GitStats
end
def run
result = in_repo { %x[#@command] }
result = run_in_repo { %x[#@command] }
repo.git_command_observer.try(:call, @command, result)
result
end
def in_repo
def run_in_repo
Dir.chdir(@repo.path) { yield }
end

View File

@ -2,30 +2,27 @@ require 'spec_helper'
describe GitStats::GitData::Repo do
let(:repo) { build(:repo) }
let(:expected_authors) { {
"john.doe@gmail.com" => build(:author, repo: repo, name: "John Doe", email: "john.doe@gmail.com"),
"joe.doe@gmail.com" => build(:author, repo: repo, name: "Joe Doe", email: "joe.doe@gmail.com")
} }
describe 'git output parsing' do
before {
GitStats::GitData::Command.should_receive(:new).with(
repo, 'git shortlog -se HEAD').and_return(
double(:run => " 156 John Doe <john.doe@gmail.com>
53 Joe Doe <joe.doe@gmail.com>
"))
}
it 'should parse git shortlog output to authors hash' do
repo.authors.should == {
"john.doe@gmail.com" => GitStats::GitData::Author.new(repo: repo, name: "John Doe", email: "john.doe@gmail.com"),
"joe.doe@gmail.com" => GitStats::GitData::Author.new(repo: repo, name: "Joe Doe", email: "joe.doe@gmail.com")
}
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return(" 156 John Doe <john.doe@gmail.com>
53 Joe Doe <joe.doe@gmail.com>
")
repo.authors.should == expected_authors
end
it 'should parse git revlist output to date sorted commits array' do
GitStats::GitData::Command.should_receive(:new).with(
repo, 'git rev-list --pretty=format:"%h|%at|%ai|%aE" HEAD | grep -v commit').and_return(
double(:run => "e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("e4412c3|1348603824|2012-09-25 22:10:24 +0200|john.doe@gmail.com
ce34874|1347482927|2012-09-12 22:48:47 +0200|joe.doe@gmail.com
5eab339|1345835073|2012-08-24 21:04:33 +0200|john.doe@gmail.com
"))
")
repo.stub(authors: expected_authors)
repo.commits.should == [
GitStats::GitData::Commit.new(

View File

@ -13,11 +13,9 @@ describe GitStats::GitData::ShortStat do
{content: '', expect: [0, 0, 0]},
].each do |test|
it "#{test[:content]} parsing" do
GitStats::GitData::Command.should_receive(:new).with(
commit.repo, 'git show --shortstat --oneline abc').and_return(
double(:run => "abc some commit
GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("abc some commit
#{test[:content]}
"))
")
commit.short_stat.should be_a(GitStats::GitData::ShortStat)