Fix bugs in Gitlab::Template::Finders preventing instances from BaseTemplate.all from loading content

This commit is contained in:
Nick Thomas 2018-08-14 22:28:46 +01:00
parent 6343964b9c
commit ffd164d27f
No known key found for this signature in database
GPG key ID: 2A313A47AFADACE9
3 changed files with 43 additions and 6 deletions

View file

@ -21,7 +21,7 @@ module Gitlab
def category_directory(category)
return @base_dir unless category.present?
@base_dir + @categories[category]
File.join(@base_dir, @categories[category])
end
class << self

View file

@ -27,7 +27,7 @@ module Gitlab
directory = select_directory(file_name)
raise FileNotFoundError if directory.nil?
category_directory(directory) + file_name
File.join(category_directory(directory), file_name)
end
def list_files_for(dir)
@ -37,8 +37,8 @@ module Gitlab
entries = @repository.tree(:head, dir).entries
names = entries.map(&:name)
names.select { |f| f =~ self.class.filter_regex(@extension) }
paths = entries.map(&:path)
paths.select { |f| f =~ self.class.filter_regex(@extension) }
end
private
@ -47,10 +47,10 @@ module Gitlab
return [] unless @commit
# Insert root as directory
directories = ["", @categories.keys]
directories = ["", *@categories.keys]
directories.find do |category|
path = category_directory(category) + file_name
path = File.join(category_directory(category), file_name)
@repository.blob_at(@commit.id, path)
end
end

View file

@ -0,0 +1,37 @@
require 'spec_helper'
describe Gitlab::Template::Finders::RepoTemplateFinder do
set(:project) { create(:project, :repository) }
let(:categories) { { 'HTML' => 'html' } }
subject(:finder) { described_class.new(project, 'files/', '.html', categories) }
describe '#read' do
it 'returns the content of the given path' do
result = finder.read('files/html/500.html')
expect(result).to be_present
end
it 'raises an error if the path does not exist' do
expect { finder.read('does/not/exist') }.to raise_error(described_class::FileNotFoundError)
end
end
describe '#find' do
it 'returns the full path of the found template' do
result = finder.find('500')
expect(result).to eq('files/html/500.html')
end
end
describe '#list_files_for' do
it 'returns the full path of the found files' do
result = finder.list_files_for('files/html')
expect(result).to contain_exactly('files/html/500.html')
end
end
end