Commit bc0c52f2 authored by Christian Couder's avatar Christian Couder

Refactor shared examples

parent 92cb13a9
...@@ -21,147 +21,23 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do ...@@ -21,147 +21,23 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do
stub_licensed_features(multiple_merge_request_assignees: true) stub_licensed_features(multiple_merge_request_assignees: true)
end end
shared_examples_for 'a service that can create a merge request' do
subject(:last_mr) { MergeRequest.last }
it 'creates a merge request with the correct target branch' do
branch = push_options[:target] || project.default_branch
expect { service.execute }.to change { MergeRequest.count }.by(1)
expect(last_mr.target_branch).to eq(branch)
end
context 'when project has been forked', :sidekiq_might_not_need_inline do
let(:forked_project) { fork_project(project, user1, repository: true) }
let(:service) { described_class.new(forked_project, user1, changes, push_options) }
before do
allow(forked_project).to receive(:empty_repo?).and_return(false)
end
it 'sets the correct source and target project' do
service.execute
expect(last_mr.source_project).to eq(forked_project)
expect(last_mr.target_project).to eq(project)
end
end
end
shared_examples_for 'a service that does not create a merge request' do
it do
expect { service.execute }.not_to change { MergeRequest.count }
end
end
# In the non-foss version of GitLab, there can be many assignees
shared_examples_for 'a service that can change assignees of a merge request' do |count|
subject(:last_mr) { MergeRequest.last }
it 'changes assignee count' do
service.execute
expect(last_mr.assignees.count).to eq(count)
end
end
describe '`assign` push option' do describe '`assign` push option' do
let(:push_options) { { assign: { user2.id => 1, user3.id => 1 } } } let(:assigned) { { user2.id => 1, user3.id => 1 } }
let(:unassigned) { nil }
context 'with a new branch' do let(:push_options) { { assign: assigned, unassign: unassigned } }
let(:changes) { new_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required) it_behaves_like 'with a new branch', 2
end it_behaves_like 'with an existing branch but no open MR', 2
it_behaves_like 'with an existing branch that has a merge request open', 2
context 'when coupled with the `create` push option in ee' do
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 } } }
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 2
end
end
context 'with an existing branch but no open MR' do
let(:changes) { existing_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
context 'when coupled with the `create` push option in ee' do
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 } } }
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 2
end
end
context 'with an existing branch that has a merge request open in ee' do
let(:changes) { existing_branch_changes }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
it_behaves_like 'a service that does not create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 2
end
end end
describe '`unassign` push option' do describe '`unassign` push option' do
let(:push_options) { { assign: { user2.id => 1, user3.id => 1 }, unassign: { user1.id => 1, user3.id => 1 } } } let(:assigned) { { user2.id => 1, user3.id => 1 } }
let(:unassigned) { { user1.id => 1, user3.id => 1 } }
context 'with a new branch' do let(:push_options) { { assign: assigned, unassign: unassigned } }
let(:changes) { new_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
context 'when coupled with the `create` push option' do it_behaves_like 'with a new branch', 1
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 }, unassign: { user1.id => 1, user3.id => 1 } } } it_behaves_like 'with an existing branch but no open MR', 1
it_behaves_like 'with an existing branch that has a merge request open', 1
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 1
end
end
context 'with an existing branch but no open MR' do
let(:changes) { existing_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
context 'when coupled with the `create` push option' do
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 }, unassign: { user1.id => 1, user3.id => 1 } } }
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 1
end
end
context 'with an existing branch that has a merge request open' do
let(:changes) { existing_branch_changes }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
it_behaves_like 'a service that does not create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 1
end
end end
end end
...@@ -29,33 +29,6 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do ...@@ -29,33 +29,6 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do
stub_licensed_features(multiple_merge_request_assignees: false) stub_licensed_features(multiple_merge_request_assignees: false)
end end
shared_examples_for 'a service that can create a merge request' do
subject(:last_mr) { MergeRequest.last }
it 'creates a merge request with the correct target branch' do
branch = push_options[:target] || project.default_branch
expect { service.execute }.to change { MergeRequest.count }.by(1)
expect(last_mr.target_branch).to eq(branch)
end
context 'when project has been forked', :sidekiq_might_not_need_inline do
let(:forked_project) { fork_project(project, user1, repository: true) }
let(:service) { described_class.new(forked_project, user1, changes, push_options) }
before do
allow(forked_project).to receive(:empty_repo?).and_return(false)
end
it 'sets the correct source and target project' do
service.execute
expect(last_mr.source_project).to eq(forked_project)
expect(last_mr.target_project).to eq(project)
end
end
end
shared_examples_for 'a service that can set the target of a merge request' do shared_examples_for 'a service that can set the target of a merge request' do
subject(:last_mr) { MergeRequest.last } subject(:last_mr) { MergeRequest.last }
...@@ -121,23 +94,6 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do ...@@ -121,23 +94,6 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do
end end
end end
# In the foss version of GitLab, there can be only one assignee
shared_examples_for 'a service that can change one assignee of a merge request' do
subject(:last_mr) { MergeRequest.last }
it 'changes assignee count' do
service.execute
expect(last_mr.assignees.count).to eq(1)
end
end
shared_examples_for 'a service that does not create a merge request' do
it do
expect { service.execute }.not_to change { MergeRequest.count }
end
end
shared_examples_for 'a service that does not update a merge request' do shared_examples_for 'a service that does not update a merge request' do
it do it do
expect { service.execute }.not_to change { MergeRequest.maximum(:updated_at) } expect { service.execute }.not_to change { MergeRequest.maximum(:updated_at) }
...@@ -618,54 +574,22 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do ...@@ -618,54 +574,22 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do
end end
end end
describe '`assign` push option' do shared_examples 'with an existing branch that has a merge request open in foss' do
let(:push_options) { { assign: { user2.id => 1, user3.id => 1 } } }
context 'with a new branch' do
let(:changes) { new_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
context 'when coupled with the `create` push option in foss' do
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 } } }
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change one assignee of a merge request'
end
end
context 'with an existing branch but no open MR' do
let(:changes) { existing_branch_changes } let(:changes) { existing_branch_changes }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
it_behaves_like 'a service that does not create a merge request' it_behaves_like 'a service that does not create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', 1
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end end
context 'when coupled with the `create` push option in foss' do describe '`assign` push option' do
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 } } } let(:assigned) { { user2.id => 1, user3.id => 1 } }
let(:unassigned) { nil }
it_behaves_like 'a service that can create a merge request' let(:push_options) { { assign: assigned, unassign: unassigned } }
it_behaves_like 'a service that can change one assignee of a merge request'
end
end
context 'with an existing branch that has a merge request open in foss' do
let(:changes) { existing_branch_changes }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
it_behaves_like 'a service that does not create a merge request' it_behaves_like 'with a new branch', 1
it_behaves_like 'a service that can change one assignee of a merge request' it_behaves_like 'with an existing branch but no open MR', 1
end it_behaves_like 'with an existing branch that has a merge request open in foss'
context 'with a deleted branch' do context 'with a deleted branch' do
let(:changes) { deleted_branch_changes } let(:changes) { deleted_branch_changes }
...@@ -681,53 +605,13 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do ...@@ -681,53 +605,13 @@ RSpec.describe MergeRequests::PushOptionsHandlerService do
end end
describe '`unassign` push option' do describe '`unassign` push option' do
let(:push_options) { { assign: { user2.id => 1, user3.id => 1 }, unassign: { user1.id => 1, user3.id => 1 } } } let(:assigned) { { user2.id => 1, user3.id => 1 } }
let(:unassigned) { { user1.id => 1, user3.id => 1 } }
context 'with a new branch' do let(:push_options) { { assign: assigned, unassign: unassigned } }
let(:changes) { new_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
context 'when coupled with the `create` push option' do
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 }, unassign: { user1.id => 1, user3.id => 1 } } }
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change one assignee of a merge request'
end
end
context 'with an existing branch but no open MR' do
let(:changes) { existing_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
context 'when coupled with the `create` push option' do it_behaves_like 'with a new branch', 1
let(:push_options) { { create: true, assign: { user2.id => 1, user3.id => 1 }, unassign: { user1.id => 1, user3.id => 1 } } } it_behaves_like 'with an existing branch but no open MR', 1
it_behaves_like 'with an existing branch that has a merge request open in foss'
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change one assignee of a merge request'
end
end
context 'with an existing branch that has a merge request open' do
let(:changes) { existing_branch_changes }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
it_behaves_like 'a service that does not create a merge request'
it_behaves_like 'a service that can change one assignee of a merge request'
end
context 'with a deleted branch' do context 'with a deleted branch' do
let(:changes) { deleted_branch_changes } let(:changes) { deleted_branch_changes }
......
...@@ -73,3 +73,93 @@ RSpec.shared_examples 'merge request reviewers cache counters invalidator' do ...@@ -73,3 +73,93 @@ RSpec.shared_examples 'merge request reviewers cache counters invalidator' do
described_class.new(project, user, {}).execute(merge_request) described_class.new(project, user, {}).execute(merge_request)
end end
end end
RSpec.shared_examples_for 'a service that can create a merge request' do
subject(:last_mr) { MergeRequest.last }
it 'creates a merge request with the correct target branch' do
branch = push_options[:target] || project.default_branch
expect { service.execute }.to change { MergeRequest.count }.by(1)
expect(last_mr.target_branch).to eq(branch)
end
context 'when project has been forked', :sidekiq_might_not_need_inline do
let(:forked_project) { fork_project(project, user1, repository: true) }
let(:service) { described_class.new(forked_project, user1, changes, push_options) }
before do
allow(forked_project).to receive(:empty_repo?).and_return(false)
end
it 'sets the correct source and target project' do
service.execute
expect(last_mr.source_project).to eq(forked_project)
expect(last_mr.target_project).to eq(project)
end
end
end
RSpec.shared_examples_for 'a service that does not create a merge request' do
it do
expect { service.execute }.not_to change { MergeRequest.count }
end
end
# In the non-foss version of GitLab, there can be many assignees, so
# there 'count' can be something other than 0 or 1. In the foss
# version of GitLab, there can be only one assignee though, so 'count'
# can only be 0 or 1.
RSpec.shared_examples_for 'a service that can change assignees of a merge request' do |count|
subject(:last_mr) { MergeRequest.last }
it 'changes assignee count' do
service.execute
expect(last_mr.assignees.count).to eq(count)
end
end
RSpec.shared_examples 'with an existing branch that has a merge request open' do |count|
let(:changes) { existing_branch_changes }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}
it_behaves_like 'a service that does not create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', count
end
RSpec.shared_examples 'when coupled with the `create` push option' do |count|
let(:push_options) { { create: true, assign: assigned, unassign: unassigned } }
it_behaves_like 'a service that can create a merge request'
it_behaves_like 'a service that can change assignees of a merge request', count
end
RSpec.shared_examples 'with a new branch' do |count|
let(:changes) { new_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
it_behaves_like 'when coupled with the `create` push option', count
end
RSpec.shared_examples 'with an existing branch but no open MR' do |count|
let(:changes) { existing_branch_changes }
it_behaves_like 'a service that does not create a merge request'
it 'adds an error to the service' do
service.execute
expect(service.errors).to include(error_mr_required)
end
it_behaves_like 'when coupled with the `create` push option', count
end
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