Use head tree (cached) for file search. Also add some tests

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2016-03-24 15:35:49 +01:00
parent 3b1543cd15
commit b105b84e3a
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
2 changed files with 28 additions and 1 deletions

View file

@ -468,7 +468,11 @@ class Repository
end
def gitlab_ci_yml
@gitlab_ci_yml ||= blob_at(head_commit.sha, '.gitlab-ci.yml') unless empty?
return nil if empty?
@gitlab_ci_yml ||= tree(:head).blobs.find do |file|
file.name == '.gitlab-ci.yml'
end
end
def head_commit

View file

@ -148,6 +148,29 @@ describe Repository, models: true do
end
end
describe "#gitlab_ci_yml" do
before do
TestBlob = Struct.new(:name)
end
it 'returns valid file' do
files = [TestBlob.new('file'), TestBlob.new('.gitlab-ci.yml'), TestBlob.new('copying')]
expect(repository.tree).to receive(:blobs).and_return(files)
expect(repository.gitlab_ci_yml.name).to eq('.gitlab-ci.yml')
end
it 'returns nil if not exists' do
expect(repository.tree).to receive(:blobs).and_return([])
expect(repository.gitlab_ci_yml).to be_nil
end
it 'returns nil for empty repository' do
expect(repository).to receive(:empty?).and_return(true)
expect(repository.gitlab_ci_yml).to be_nil
end
end
describe :add_branch do
context 'when pre hooks were successful' do
it 'should run without errors' do