diff options
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | app/models/commit.rb | 7 | ||||
-rw-r--r-- | spec/models/commit_spec.rb | 23 |
3 files changed, 29 insertions, 2 deletions
@@ -4,6 +4,7 @@ v7.13.0 - Improved Lint stability - Disable colors in rake tasks automatically (if IO is not a TTY) - Implemented "rake env:info". Rake task to receive system information + - Fix coverage calculation on commit page v7.12.1 - Runner without tag should pick builds without tag only diff --git a/app/models/commit.rb b/app/models/commit.rb index 215afdd..7cf2810 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -205,8 +205,11 @@ class Commit < ActiveRecord::Base end def coverage - if project.coverage_enabled? && builds.size > 0 - builds.last.coverage + if project.coverage_enabled? && builds.count(:all) > 0 + coverage_array = builds.map(&:coverage).compact + if coverage_array.size > 1 + coverage_array.reduce(:+) / coverage_array.size + end end end diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index 8c0073e..e1969c4 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -159,4 +159,27 @@ describe Commit do commit.finished_at.should be_nil end end + + describe "coverage" do + let(:project) { FactoryGirl.create :project, coverage_regex: "/.*/" } + let(:commit) { FactoryGirl.create :commit, project: project } + + it "calculates average when there are two builds with coverage" do + FactoryGirl.create :build, coverage: 30, commit: commit + FactoryGirl.create :build, coverage: 40, commit: commit + commit.coverage.should == 35.0 + end + + it "calculates average when there are two builds with coverage and one with nil" do + FactoryGirl.create :build, coverage: 30, commit: commit + FactoryGirl.create :build, coverage: 40, commit: commit + FactoryGirl.create :build, commit: commit + commit.coverage.should == 35.0 + end + + it "calculates average when there is one build without coverage" do + FactoryGirl.create :build, commit: commit + commit.coverage.should be_nil + end + end end |