Test the `test` cycle analytics phase.
This commit is contained in:
parent
de483c6834
commit
9cff3f8f52
|
@ -54,7 +54,7 @@ class CycleAnalytics
|
|||
tip = merge_request.commits.first
|
||||
return unless tip
|
||||
|
||||
pipeline = Ci::Pipeline.find_by_sha(tip.sha)
|
||||
pipeline = Ci::Pipeline.success.find_by_sha(tip.sha)
|
||||
pipeline.started_at if pipeline
|
||||
end
|
||||
end
|
||||
|
@ -65,7 +65,7 @@ class CycleAnalytics
|
|||
tip = merge_request.commits.first
|
||||
return unless tip
|
||||
|
||||
pipeline = Ci::Pipeline.find_by_sha(tip.sha)
|
||||
pipeline = Ci::Pipeline.success.find_by_sha(tip.sha)
|
||||
pipeline.finished_at if pipeline
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'CycleAnalytics#test', feature: true do
|
||||
let(:project) { create(:project) }
|
||||
let(:from_date) { 10.days.ago }
|
||||
let(:user) { create(:user, :admin) }
|
||||
subject { CycleAnalytics.new(project, from: from_date) }
|
||||
|
||||
def create_merge_request_closing_issue(issue, message: nil)
|
||||
source_branch = random_git_name
|
||||
project.repository.add_branch(user, source_branch, 'master')
|
||||
sha = project.repository.commit_file(user, random_git_name, "content", "commit message", source_branch, false)
|
||||
project.repository.commit(sha)
|
||||
|
||||
opts = {
|
||||
title: 'Awesome merge_request',
|
||||
description: message || "Fixes #{issue.to_reference}",
|
||||
source_branch: source_branch,
|
||||
target_branch: 'master'
|
||||
}
|
||||
|
||||
MergeRequests::CreateService.new(project, user, opts).execute
|
||||
end
|
||||
|
||||
generate_cycle_analytics_spec(phase: :test,
|
||||
data_fn: lambda do |context|
|
||||
issue = context.create(:issue, project: context.project)
|
||||
merge_request = context.create_merge_request_closing_issue(issue)
|
||||
{ pipeline: context.create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) }
|
||||
end,
|
||||
start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]],
|
||||
end_time_conditions: [["pipeline is finished", -> (context, data) { data[:pipeline].succeed! }]])
|
||||
|
||||
context "when the pipeline is for a regular merge request (that doesn't close an issue)" do
|
||||
it "returns nil" do
|
||||
5.times do
|
||||
merge_request = create(:merge_request)
|
||||
pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha)
|
||||
|
||||
pipeline.run!
|
||||
pipeline.succeed!
|
||||
end
|
||||
|
||||
expect(subject.test).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when the pipeline is not for a merge request" do
|
||||
it "returns nil" do
|
||||
5.times do
|
||||
pipeline = create(:ci_pipeline, ref: "refs/heads/master", sha: project.repository.commit('master').sha)
|
||||
|
||||
pipeline.run!
|
||||
pipeline.succeed!
|
||||
end
|
||||
|
||||
expect(subject.test).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when the pipeline is dropped (failed)" do
|
||||
it "returns nil" do
|
||||
5.times do
|
||||
issue = create(:issue, project: project)
|
||||
merge_request = create_merge_request_closing_issue(issue)
|
||||
pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha)
|
||||
|
||||
pipeline.run!
|
||||
pipeline.drop!
|
||||
end
|
||||
|
||||
expect(subject.test).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when the pipeline is cancelled" do
|
||||
it "returns nil" do
|
||||
5.times do
|
||||
issue = create(:issue, project: project)
|
||||
merge_request = create_merge_request_closing_issue(issue)
|
||||
pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha)
|
||||
|
||||
pipeline.run!
|
||||
pipeline.cancel!
|
||||
end
|
||||
|
||||
expect(subject.test).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue