Commit d0e101e9 authored by Timothy Andrew's avatar Timothy Andrew

Fix all cycle analytics specs.

A number of failures were introduced due to performance
improvements (like pre-calculating metrics).
parent cebfe053
......@@ -9,7 +9,11 @@ describe 'CycleAnalytics#code', feature: true do
generate_cycle_analytics_spec(phase: :code,
data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } },
start_time_conditions: [["issue mentioned in a commit", -> (context, data) { context.create_commit_referencing_issue(data[:issue]) }]],
end_time_conditions: [["merge request that closes issue is created", -> (context, data) { context.create_merge_request_closing_issue(data[:issue]) }]])
end_time_conditions: [["merge request that closes issue is created", -> (context, data) { context.create_merge_request_closing_issue(data[:issue]) }]],
post_fn: -> (context, data) do
context.merge_merge_requests_closing_issue(data[:issue])
context.deploy_master
end)
context "when a regular merge request (that doesn't close the issue) is created" do
it "returns nil" do
......@@ -18,6 +22,9 @@ describe 'CycleAnalytics#code', feature: true do
create_commit_referencing_issue(issue)
create_merge_request_closing_issue(issue, message: "Closes nothing")
merge_merge_requests_closing_issue(issue)
deploy_master
end
expect(subject.code).to be_nil
......
......@@ -3,13 +3,21 @@ require 'spec_helper'
describe 'CycleAnalytics#issue', models: true do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { CycleAnalytics.new(project, from: from_date) }
generate_cycle_analytics_spec(phase: :issue,
data_fn: -> (context) { { issue: context.build(:issue, project: context.project) } },
start_time_conditions: [["issue created", -> (context, data) { data[:issue].save }]],
end_time_conditions: [["issue associated with a milestone", -> (context, data) { data[:issue].update(milestone: context.create(:milestone, project: context.project)) if data[:issue].persisted? }],
["list label added to issue", -> (context, data) { data[:issue].update(label_ids: [context.create(:label, lists: [context.create(:list)]).id]) if data[:issue].persisted? }]])
["list label added to issue", -> (context, data) { data[:issue].update(label_ids: [context.create(:label, lists: [context.create(:list)]).id]) if data[:issue].persisted? }]],
post_fn: -> (context, data) do
if data[:issue].persisted?
context.create_merge_request_closing_issue(data[:issue].reload)
context.merge_merge_requests_closing_issue(data[:issue])
context.deploy_master
end
end)
context "when a regular label (instead of a list label) is added to the issue" do
it "returns nil" do
......@@ -17,6 +25,10 @@ describe 'CycleAnalytics#issue', models: true do
regular_label = create(:label)
issue = create(:issue, project: project)
issue.update(label_ids: [regular_label.id])
create_merge_request_closing_issue(issue)
merge_merge_requests_closing_issue(issue)
deploy_master
end
expect(subject.issue).to be_nil
......
......@@ -7,17 +7,28 @@ describe 'CycleAnalytics#plan', feature: true do
subject { CycleAnalytics.new(project, from: from_date) }
generate_cycle_analytics_spec(phase: :plan,
data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } },
data_fn: -> (context) { { issue: context.create(:issue, project: context.project),
branch_name: context.random_git_name } },
start_time_conditions: [["issue associated with a milestone", -> (context, data) { data[:issue].update(milestone: context.create(:milestone, project: context.project)) }],
["list label added to issue", -> (context, data) { data[:issue].update(label_ids: [context.create(:label, lists: [context.create(:list)]).id]) }]],
end_time_conditions: [["issue mentioned in a commit", -> (context, data) { context.create_commit_referencing_issue(data[:issue]) }]])
end_time_conditions: [["issue mentioned in a commit", -> (context, data) { context.create_commit_referencing_issue(data[:issue], branch_name: data[:branch_name]) }]],
post_fn: -> (context, data) do
context.create_merge_request_closing_issue(data[:issue], source_branch: data[:branch_name])
context.merge_merge_requests_closing_issue(data[:issue])
context.deploy_master
end)
context "when a regular label (instead of a list label) is added to the issue" do
it "returns nil" do
branch_name = random_git_name
label = create(:label)
issue = create(:issue, project: project)
issue.update(label_ids: [label.id])
create_commit_referencing_issue(issue)
create_commit_referencing_issue(issue, branch_name: branch_name)
create_merge_request_closing_issue(issue, source_branch: branch_name)
merge_merge_requests_closing_issue(issue)
deploy_master
expect(subject.issue).to be_nil
end
......
......@@ -9,12 +9,15 @@ describe 'CycleAnalytics#review', feature: true do
generate_cycle_analytics_spec(phase: :review,
data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } },
start_time_conditions: [["merge request that closes issue is created", -> (context, data) { context.create_merge_request_closing_issue(data[:issue]) }]],
end_time_conditions: [["merge request that closes issue is merged", -> (context, data) { context.merge_merge_requests_closing_issue(data[:issue]) }]])
end_time_conditions: [["merge request that closes issue is merged", -> (context, data) { context.merge_merge_requests_closing_issue(data[:issue]) }]],
post_fn: -> (context, data) { context.deploy_master })
context "when a regular merge request (that doesn't close the issue) is created and merged" do
it "returns nil" do
5.times do
MergeRequests::MergeService.new(project, user).execute(create(:merge_request))
deploy_master
end
expect(subject.review).to be_nil
......
......@@ -10,19 +10,30 @@ describe 'CycleAnalytics#test', feature: true do
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) }
{
pipeline: context.create(:ci_pipeline, ref: merge_request.source_branch, sha: merge_request.diff_head_sha, project: context.project),
issue: issue
}
end,
start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]],
end_time_conditions: [["pipeline is finished", -> (context, data) { data[:pipeline].succeed! }]])
end_time_conditions: [["pipeline is finished", -> (context, data) { data[:pipeline].succeed! }]],
post_fn: -> (context, data) do
context.merge_merge_requests_closing_issue(data[:issue])
context.deploy_master
end)
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)
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.succeed!
merge_merge_requests_closing_issue(issue)
deploy_master
end
expect(subject.test).to be_nil
......@@ -36,6 +47,8 @@ describe 'CycleAnalytics#test', feature: true do
pipeline.run!
pipeline.succeed!
deploy_master
end
expect(subject.test).to be_nil
......@@ -51,6 +64,9 @@ describe 'CycleAnalytics#test', feature: true do
pipeline.run!
pipeline.drop!
merge_merge_requests_closing_issue(issue)
deploy_master
end
expect(subject.test).to be_nil
......@@ -66,6 +82,9 @@ describe 'CycleAnalytics#test', feature: true do
pipeline.run!
pipeline.cancel!
merge_merge_requests_closing_issue(issue)
deploy_master
end
expect(subject.test).to be_nil
......
module CycleAnalyticsHelpers
def create_commit_referencing_issue(issue)
branch_name = random_git_name
def create_commit_referencing_issue(issue, branch_name: random_git_name)
project.repository.add_branch(user, branch_name, 'master')
create_commit("Commit for ##{issue.iid}", issue.project, user, branch_name)
end
def create_commit(message, project, user, branch_name)
filename = random_git_name
oldrev = project.repository.commit(branch_name).sha
options = {
committer: project.repository.user_to_committer(user),
......@@ -20,14 +20,17 @@ module CycleAnalyticsHelpers
GitPushService.new(project,
user,
oldrev: project.repository.commit(branch_name).sha,
oldrev: oldrev,
newrev: commit_sha,
ref: 'refs/heads/master').execute
end
def create_merge_request_closing_issue(issue, message: nil)
def create_merge_request_closing_issue(issue, message: nil, source_branch: nil)
if !source_branch || project.repository.commit(source_branch).blank?
source_branch = random_git_name
project.repository.add_branch(user, source_branch, 'master')
end
sha = project.repository.commit_file(user, random_git_name, "content", "commit message", source_branch, false)
project.repository.commit(sha)
......
......@@ -18,8 +18,9 @@ module CycleAnalyticsHelpers
# `context` (no lexical scope, so need to do `context.create` for factories, for example) and `data` (from the `data_fn`).
# Each `condition_fn` is expected to implement a case which consitutes the end of the given cycle analytics phase.
# before_end_fn: This function is run before calling the end time conditions. Used for setup that needs to be run between the start and end conditions.
# post_fn: Code that needs to be run after running the end time conditions.
def generate_cycle_analytics_spec(phase:, data_fn:, start_time_conditions:, end_time_conditions:, before_end_fn: nil)
def generate_cycle_analytics_spec(phase:, data_fn:, start_time_conditions:, end_time_conditions:, before_end_fn: nil, post_fn: nil)
combinations_of_start_time_conditions = (1..start_time_conditions.size).flat_map { |size| start_time_conditions.combination(size).to_a }
combinations_of_end_time_conditions = (1..end_time_conditions.size).flat_map { |size| end_time_conditions.combination(size).to_a }
......@@ -44,6 +45,8 @@ module CycleAnalyticsHelpers
Timecop.freeze(end_time) { condition_fn[self, data] }
end
Timecop.freeze(end_time + 1.day) { post_fn[self, data] } if post_fn
end_time - start_time
end
......@@ -73,6 +76,8 @@ module CycleAnalyticsHelpers
end_time_conditions.each do |condition_name, condition_fn|
Timecop.freeze(end_time) { condition_fn[self, data] }
end
Timecop.freeze(end_time + 1.day) { post_fn[self, data] } if post_fn
end
# Turn off the stub before checking assertions
......@@ -99,6 +104,8 @@ module CycleAnalyticsHelpers
Timecop.freeze(end_time) { condition_fn[self, data] }
end
Timecop.freeze(end_time + 1.day) { post_fn[self, data] } if post_fn
expect(subject.send(phase)).to be_nil
end
end
......@@ -115,6 +122,8 @@ module CycleAnalyticsHelpers
end_time_conditions.each_with_index do |(condition_name, condition_fn), index|
Timecop.freeze(end_time + index.days) { condition_fn[self, data] }
end
Timecop.freeze(end_time + 1.day) { post_fn[self, data] } if post_fn
end
expect(subject.send(phase)).to be_nil
......@@ -132,6 +141,8 @@ module CycleAnalyticsHelpers
start_time_conditions.each do |condition_name, condition_fn|
Timecop.freeze(start_time) { condition_fn[self, data] }
end
post_fn[self, data] if post_fn
end
expect(subject.send(phase)).to be_nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment