2019-10-16 05:07:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-28 09:14:39 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe TemplateFinder do
|
2018-08-28 09:14:39 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
let_it_be(:template_files) do
|
|
|
|
{
|
|
|
|
"Dockerfile/project_dockerfiles_template.dockerfile" => "project_dockerfiles_template content",
|
|
|
|
"gitignore/project_gitignores_template.gitignore" => "project_gitignores_template content",
|
|
|
|
"gitlab-ci/project_gitlab_ci_ymls_template.yml" => "project_gitlab_ci_ymls_template content",
|
|
|
|
".gitlab/issue_templates/project_issues_template.md" => "project_issues_template content",
|
|
|
|
".gitlab/merge_request_templates/project_merge_requests_template.md" => "project_merge_requests_template content"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.shared_examples 'fetches predefined vendor templates' do
|
|
|
|
where(:type, :vendored_name) do
|
|
|
|
:dockerfiles | 'Binary'
|
|
|
|
:gitignores | 'Actionscript'
|
|
|
|
:gitlab_ci_ymls | 'Android'
|
|
|
|
:metrics_dashboard_ymls | 'Default'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to include(have_attributes(name: vendored_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with name param' do
|
|
|
|
let(:params) { { name: vendored_name } }
|
|
|
|
|
|
|
|
it 'returns only the specified vendored template when a name is specified' do
|
|
|
|
expect(result).to have_attributes(name: vendored_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with mistaken name param' do
|
|
|
|
let(:params) { { name: 'unknown' } }
|
|
|
|
|
|
|
|
it 'returns nil when an unknown name is specified' do
|
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.shared_examples 'no issues and merge requests templates available' do
|
|
|
|
context 'with issue and merge request templates' do
|
|
|
|
where(:type, :vendored_name) do
|
|
|
|
:issues | nil
|
|
|
|
:merge_requests | nil
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
context 'when fetching all templates' do
|
|
|
|
it 'returns empty array' do
|
|
|
|
expect(result).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when looking for specific template by name' do
|
|
|
|
let(:params) { { name: 'anything' } }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { result }.to raise_exception(Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.shared_examples 'fetches issues and merge requests templates' do
|
|
|
|
where(:type, :template_name) do
|
|
|
|
:issues | 'project_issues_template'
|
|
|
|
:merge_requests | 'project_merge_requests_template'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns all repository template files for issues and merge requests' do
|
|
|
|
expect(result).to include(have_attributes(name: template_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with name param' do
|
|
|
|
let(:params) { { name: template_name } }
|
|
|
|
|
|
|
|
it 'returns only the specified vendored template when a name is specified' do
|
|
|
|
expect(result).to have_attributes(name: template_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with mistaken name param' do
|
|
|
|
let(:params) { { name: 'unknown' } }
|
|
|
|
|
|
|
|
it 'raises an error when an unknown name is specified' do
|
|
|
|
expect { result }.to raise_exception(Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-05 09:45:02 -04:00
|
|
|
describe '#build' do
|
2018-10-02 19:00:38 -04:00
|
|
|
let(:project) { build_stubbed(:project) }
|
|
|
|
|
2018-09-05 09:45:02 -04:00
|
|
|
where(:type, :expected_class) do
|
|
|
|
:dockerfiles | described_class
|
|
|
|
:gitignores | described_class
|
|
|
|
:gitlab_ci_ymls | described_class
|
2020-08-03 05:09:42 -04:00
|
|
|
:licenses | ::LicenseTemplateFinder
|
|
|
|
:metrics_dashboard_ymls | described_class
|
2021-02-08 13:09:49 -05:00
|
|
|
:issues | described_class
|
|
|
|
:merge_requests | described_class
|
2018-09-05 09:45:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
2018-10-02 19:00:38 -04:00
|
|
|
subject(:finder) { described_class.build(type, project) }
|
2018-09-05 09:45:02 -04:00
|
|
|
|
|
|
|
it { is_expected.to be_a(expected_class) }
|
2018-10-02 19:00:38 -04:00
|
|
|
it { expect(finder.project).to eq(project) }
|
2018-09-05 09:45:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-28 09:14:39 -04:00
|
|
|
describe '#execute' do
|
2021-02-08 13:09:49 -05:00
|
|
|
let_it_be(:project) { nil }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
subject(:result) { described_class.new(type, project, params).execute }
|
|
|
|
|
|
|
|
context 'when no project is passed in' do
|
|
|
|
it_behaves_like 'fetches predefined vendor templates'
|
|
|
|
it_behaves_like 'no issues and merge requests templates available'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has no repository' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
|
|
|
it_behaves_like 'fetches predefined vendor templates'
|
|
|
|
it_behaves_like 'no issues and merge requests templates available'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has a repository' do
|
|
|
|
let_it_be(:project) { create(:project, :custom_repo, files: template_files) }
|
|
|
|
|
|
|
|
it_behaves_like 'fetches predefined vendor templates'
|
|
|
|
it_behaves_like 'fetches issues and merge requests templates'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#template_names' do
|
|
|
|
let_it_be(:project) { nil }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
subject(:result) { described_class.new(type, project, params).template_names.values.flatten.map { |el| OpenStruct.new(el) } }
|
|
|
|
|
2018-08-28 09:14:39 -04:00
|
|
|
where(:type, :vendored_name) do
|
|
|
|
:dockerfiles | 'Binary'
|
|
|
|
:gitignores | 'Actionscript'
|
|
|
|
:gitlab_ci_ymls | 'Android'
|
2020-08-03 05:09:42 -04:00
|
|
|
:metrics_dashboard_ymls | 'Default'
|
2018-08-28 09:14:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
2021-02-08 13:09:49 -05:00
|
|
|
context 'when no project is passed in' do
|
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to include(have_attributes(name: vendored_name))
|
|
|
|
end
|
|
|
|
end
|
2018-08-28 09:14:39 -04:00
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
context 'when project has no repository' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to include(have_attributes(name: vendored_name))
|
|
|
|
end
|
2018-08-28 09:14:39 -04:00
|
|
|
end
|
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
context 'when project has a repository' do
|
|
|
|
let_it_be(:project) { create(:project, :custom_repo, files: template_files) }
|
2018-08-28 09:14:39 -04:00
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to include(have_attributes(name: vendored_name))
|
|
|
|
end
|
2018-08-28 09:14:39 -04:00
|
|
|
end
|
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
context 'template names hash keys' do
|
|
|
|
it 'has all the expected keys' do
|
|
|
|
expect(result.first.to_h.keys).to match_array(%i(id key name project_id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:type, :template_name) do
|
|
|
|
:issues | 'project_issues_template'
|
|
|
|
:merge_requests | 'project_merge_requests_template'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
context 'when no project is passed in' do
|
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has no repository' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has a repository' do
|
|
|
|
let_it_be(:project) { create(:project, :custom_repo, files: template_files) }
|
|
|
|
|
|
|
|
it 'returns all vendored templates when no name is specified' do
|
|
|
|
expect(result).to include(have_attributes(name: template_name))
|
|
|
|
end
|
2018-08-28 09:14:39 -04:00
|
|
|
|
2021-02-08 13:09:49 -05:00
|
|
|
context 'template names hash keys' do
|
|
|
|
it 'has all the expected keys' do
|
|
|
|
expect(result.first.to_h.keys).to match_array(%i(id key name project_id))
|
|
|
|
end
|
|
|
|
end
|
2018-08-28 09:14:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|