diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-08-15 21:32:36 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-08-15 21:32:36 +0800 |
commit | ce641335167230d288fbb2ec277548acbaff9dd7 (patch) | |
tree | 558c3a2c7bdded443da9229365a68c4308841c11 | |
parent | 0ea81ae50a702c7341b1bd6fd15002ee78ac4964 (diff) | |
download | gitlab-ce-ce641335167230d288fbb2ec277548acbaff9dd7.tar.gz |
Introduce Gitlab::Utils.now so that it's easier to stub
-rw-r--r-- | app/models/ci/pipeline.rb | 4 | ||||
-rw-r--r-- | app/models/concerns/statuseable.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/utils.rb | 7 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 9 |
4 files changed, 15 insertions, 7 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 5b9754b5c37..72c2dc469a9 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -48,11 +48,11 @@ module Ci end before_transition [:created, :pending] => :running do |pipeline| - pipeline.started_at = Time.now + pipeline.started_at = Gitlab::Utils.now end before_transition any => [:success, :failed, :canceled] do |pipeline| - pipeline.finished_at = Time.now + pipeline.finished_at = Gitlab::Utils.now end before_transition do |pipeline| diff --git a/app/models/concerns/statuseable.rb b/app/models/concerns/statuseable.rb index 750f937b724..9f73c5c9467 100644 --- a/app/models/concerns/statuseable.rb +++ b/app/models/concerns/statuseable.rb @@ -87,7 +87,7 @@ module Statuseable if started_at && finished_at finished_at - started_at elsif started_at - Time.now - started_at + Gitlab::Utils.now - started_at end end end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb index d13fe0ef8a9..4d1bd16eb95 100644 --- a/lib/gitlab/utils.rb +++ b/lib/gitlab/utils.rb @@ -7,11 +7,16 @@ module Gitlab # @param cmd [Array<String>] # @return [Boolean] def system_silent(cmd) - Popen::popen(cmd).last.zero? + Popen.popen(cmd).last.zero? end def force_utf8(str) str.force_encoding(Encoding::UTF_8) end + + # The same as Time.now but using this would make it easier to test + def now + Time.now + end end end diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 8137e9f8f71..67bd23a1ccb 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -129,12 +129,15 @@ describe Ci::Pipeline, models: true do describe '#duration' do before do - build.skip - build2.skip + allow(Gitlab::Utils).to receive(:now). + and_return(current - 120, current) + + pipeline.run + pipeline.succeed end it 'matches sum of builds duration' do - expect(pipeline.reload.duration).to eq(build.duration + build2.duration) + expect(pipeline.reload.duration).to eq(120) end end |