diff options
Diffstat (limited to 'spec/services/merge_requests/build_service_spec.rb')
-rw-r--r-- | spec/services/merge_requests/build_service_spec.rb | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/spec/services/merge_requests/build_service_spec.rb b/spec/services/merge_requests/build_service_spec.rb index ab3d9880d29..3c9d2271ddc 100644 --- a/spec/services/merge_requests/build_service_spec.rb +++ b/spec/services/merge_requests/build_service_spec.rb @@ -79,6 +79,15 @@ RSpec.describe MergeRequests::BuildService do end end + shared_examples 'with a Default.md template' do + let(:files) { { '.gitlab/merge_request_templates/Default.md' => 'Default template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'the template description is preferred' do + expect(merge_request.description).to eq('Default template contents') + end + end + describe '#execute' do it 'calls the compare service with the correct arguments' do allow_any_instance_of(described_class).to receive(:projects_and_branches_valid?).and_return(true) @@ -221,6 +230,7 @@ RSpec.describe MergeRequests::BuildService do end it_behaves_like 'allows the merge request to be created' + it_behaves_like 'with a Default.md template' it 'uses the title of the commit as the title of the merge request' do expect(merge_request.title).to eq(commit_2.safe_message.split("\n").first) @@ -241,6 +251,8 @@ RSpec.describe MergeRequests::BuildService do context 'commit has no description' do let(:commits) { Commit.decorate([commit_3], project) } + it_behaves_like 'with a Default.md template' + it 'uses the title of the commit as the title of the merge request' do expect(merge_request.title).to eq(commit_3.safe_message) end @@ -279,6 +291,17 @@ RSpec.describe MergeRequests::BuildService do expect(merge_request.description).to eq(expected_description) end + + context 'a Default.md template is defined' do + let(:files) { { '.gitlab/merge_request_templates/Default.md' => 'Default template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'appends the closing description to a Default.md template' do + expected_description = ['Default template contents', closing_message].compact.join("\n\n") + + expect(merge_request.description).to eq(expected_description) + end + end end context 'when the source branch matches an internal issue' do @@ -332,6 +355,7 @@ RSpec.describe MergeRequests::BuildService do end it_behaves_like 'allows the merge request to be created' + it_behaves_like 'with a Default.md template' it 'uses the title of the branch as the merge request title' do expect(merge_request.title).to eq('Feature branch') @@ -347,6 +371,15 @@ RSpec.describe MergeRequests::BuildService do it 'keeps the description from the initial params' do expect(merge_request.description).to eq(description) end + + context 'a Default.md template is defined' do + let(:files) { { '.gitlab/merge_request_templates/Default.md' => 'Default template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'keeps the description from the initial params' do + expect(merge_request.description).to eq(description) + end + end end context 'when the source branch matches an issue' do @@ -377,6 +410,17 @@ RSpec.describe MergeRequests::BuildService do it 'sets the closing description' do expect(merge_request.description).to eq(closing_message) end + + context 'a Default.md template is defined' do + let(:files) { { '.gitlab/merge_request_templates/Default.md' => 'Default template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'appends the closing description to a Default.md template' do + expected_description = ['Default template contents', closing_message].compact.join("\n\n") + + expect(merge_request.description).to eq(expected_description) + end + end end end end @@ -389,6 +433,7 @@ RSpec.describe MergeRequests::BuildService do end it_behaves_like 'allows the merge request to be created' + it_behaves_like 'with a Default.md template' it 'uses the first line of the first multi-line commit message as the title' do expect(merge_request.title).to eq('Closes #1234 Second commit') @@ -426,6 +471,17 @@ RSpec.describe MergeRequests::BuildService do it 'sets the closing description' do expect(merge_request.description).to eq("Create the app#{closing_message ? "\n\n" + closing_message : ''}") end + + context 'a Default.md template is defined' do + let(:files) { { '.gitlab/merge_request_templates/Default.md' => 'Default template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'appends the closing description to a Default.md template' do + expected_description = ['Default template contents', closing_message].compact.join("\n\n") + + expect(merge_request.description).to eq(expected_description) + end + end end end @@ -626,4 +682,52 @@ RSpec.describe MergeRequests::BuildService do end end end + + describe '#assign_description_from_repository_template' do + subject { service.send(:assign_description_from_repository_template) } + + it 'performs no action if the merge request description is not blank' do + merge_request.description = 'foo' + subject + expect(merge_request.description).to eq 'foo' + end + + context 'when a Default template is not found' do + it 'does not modify the merge request description' do + merge_request.description = nil + subject + expect(merge_request.description).to be_nil + end + end + + context 'when a Default template is found' do + context 'when its contents cannot be retrieved' do + let(:files) { { '.gitlab/merge_request_templates/OtherTemplate.md' => 'Other template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'does not modify the merge request description' do + allow(TemplateFinder).to receive(:all_template_names).and_return({ + merge_requests: [ + { name: 'Default', id: 'default', key: 'default', project_id: project.id } + ] + }) + + merge_request.description = nil + subject + expect(merge_request.description).to be_nil + end + end + + context 'when its contents can be retrieved' do + let(:files) { { '.gitlab/merge_request_templates/Default.md' => 'Default template contents' } } + let(:project) { create(:project, :custom_repo, files: files ) } + + it 'modifies the merge request description' do + merge_request.description = nil + subject + expect(merge_request.description).to eq 'Default template contents' + end + end + end + end end |