Commit 7c128db3 authored by Jose Vargas's avatar Jose Vargas

Create shared_examples for tests

This adds a "template shared examples"
shared_example for both the metrics dashboard
templates and the gitlab-ci yml templates

Also added custom template support via
the project repository
parent 2f0912da
# frozen_string_literal: true
module Gitlab
module Template
class CustomMetricsDashboardYmlTemplate < CustomTemplate
class << self
def extension
'.yml'
end
def base_dir
'metrics-dashboards/'
end
end
end
end
end
......@@ -23,7 +23,11 @@ RSpec.describe "Custom file template classes" do
'Dockerfile/category/baz.txt' => 'CustomDockerfileTemplate category baz',
'gitignore/category/baz.txt' => 'CustomGitignoreTemplate category baz',
'gitlab-ci/category/baz.yml' => 'CustomGitlabCiYmlTemplate category baz',
'LICENSE/category/baz.txt' => 'CustomLicenseTemplate category baz'
'LICENSE/category/baz.txt' => 'CustomLicenseTemplate category baz',
'metrics-dashboards/foo.yml' => 'CustomMetricsDashboardYmlTemplate Foo',
'metrics-dashboards/bar.yml' => 'CustomMetricsDashboardYmlTemplate Bar',
'metrics-dashboards/bad.xyz' => 'CustomMetricsDashboardYmlTemplate Bad'
}
let(:project) { create(:project, :custom_repo, files: files) }
......
......@@ -12,6 +12,7 @@ RSpec.describe TemplateFinder do
:dockerfiles | described_class
:gitignores | described_class
:gitlab_ci_ymls | described_class
:metrics_dashboard_ymls | described_class
:licenses | ::LicenseTemplateFinder
end
......@@ -28,6 +29,7 @@ RSpec.describe TemplateFinder do
:dockerfiles | 'Binary'
:gitignores | 'Actionscript'
:gitlab_ci_ymls | 'Android'
:metrics_dashboard_ymls | 'Default'
end
with_them do
......
......@@ -6,10 +6,6 @@ RSpec.describe Gitlab::Template::GitlabCiYmlTemplate do
subject { described_class }
describe '.all' do
it 'strips the gitlab-ci suffix' do
expect(subject.all.first.name).not_to end_with('.gitlab-ci.yml')
end
it 'combines the globals and rest' do
all = subject.all.map(&:name)
......@@ -17,34 +13,6 @@ RSpec.describe Gitlab::Template::GitlabCiYmlTemplate do
expect(all).to include('Docker')
expect(all).to include('Ruby')
end
it 'ensure that the template name is used exactly once' do
all = subject.all.group_by(&:name)
duplicates = all.select { |_, templates| templates.length > 1 }
expect(duplicates).to be_empty
end
end
describe '.find' do
it 'returns nil if the file does not exist' do
expect(subject.find('mepmep-yadida')).to be nil
end
it 'returns the GitlabCiYml object of a valid file' do
ruby = subject.find('Ruby')
expect(ruby).to be_a described_class
expect(ruby.name).to eq('Ruby')
end
end
describe '.by_category' do
it 'returns sorted results' do
result = described_class.by_category('General')
expect(result).to eq(result.sort)
end
end
describe '#content' do
......@@ -56,13 +24,5 @@ RSpec.describe Gitlab::Template::GitlabCiYmlTemplate do
end
end
describe '#<=>' do
it 'sorts lexicographically' do
one = described_class.new('a.gitlab-ci.yml')
other = described_class.new('z.gitlab-ci.yml')
expect(one.<=>(other)).to be(-1)
expect([other, one].sort).to eq([one, other])
end
end
include_examples 'template shared examples', 'Ruby', '.gitlab-ci.yml'
end
......@@ -6,43 +6,11 @@ RSpec.describe Gitlab::Template::MetricsDashboardTemplate do
subject { described_class }
describe '.all' do
it 'strips the metrics-dashboard suffix' do
expect(subject.all.first.name).not_to end_with('metrics-dashboard.yml')
end
it 'combines the globals and rest' do
all = subject.all.map(&:name)
expect(all).to include('Default')
end
it 'ensures that the template name is used exactly once' do
all = subject.all.group_by(&:name)
duplicates = all.select { |_, templates| templates.length > 1 }
expect(duplicates).to be_empty
end
end
describe '.find' do
it 'returns nil if the file does not exist' do
expect(subject.find('nonexistent-file')).to be nil
end
it 'returns the MetricsDashboardYml object of a valid file' do
default_dashboard = subject.find('Default')
expect(default_dashboard).to be_a described_class
expect(default_dashboard.name).to eq('Default')
end
end
describe '.by_category' do
it 'returns sorted results' do
result = described_class.by_category('General')
expect(result).to eq(result.sort)
end
end
describe '#content' do
......@@ -54,13 +22,5 @@ RSpec.describe Gitlab::Template::MetricsDashboardTemplate do
end
end
describe '#<=>' do
it 'sorts lexicographically' do
one = described_class.new('a.metrics-dashboard.yml')
other = described_class.new('z.metrics-dashboard.yml')
expect(one.<=>(other)).to be(-1)
expect([other, one].sort).to eq([one, other])
end
end
include_examples 'template shared examples', 'Default', '.metrics-dashboard.yml'
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.shared_examples 'template shared examples' do |filename, file_extension|
describe '.all' do
it "strips the #{file_extension} suffix" do
expect(subject.all.first.name).not_to end_with(file_extension)
end
it 'ensures that the template name is used exactly once' do
all = subject.all.group_by(&:name)
duplicates = all.select { |_, templates| templates.length > 1 }
expect(duplicates).to be_empty
end
end
describe '.by_category' do
it 'returns sorted results' do
result = described_class.by_category('General')
expect(result).to eq(result.sort)
end
end
describe '.find' do
it 'returns nil if the file does not exist' do
expect(subject.find('nonexistent-file')).to be nil
end
it 'returns the corresponding object of a valid file' do
template = subject.find(filename)
expect(template).to be_a described_class
expect(template.name).to eq(filename)
end
end
describe '#<=>' do
it 'sorts lexicographically' do
one = described_class.new("a.#{file_extension}")
other = described_class.new("z.#{file_extension}")
expect(one.<=>(other)).to be(-1)
expect([other, one].sort).to eq([one, other])
end
end
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