Commit b4ad8f3d authored by Luke Duncalfe's avatar Luke Duncalfe

Merge branch '321834-fj-remove-nil-values-from-snippet-blobs' into 'master'

Resolve "Remove nil values from snippets blobs"

See merge request gitlab-org/gitlab!54552
parents 8f0f6137 b5629168
......@@ -216,8 +216,10 @@ class Snippet < ApplicationRecord
def blobs
return [] unless repository_exists?
branch = default_branch
list_files(branch).map { |file| Blob.lazy(repository, branch, file) }
files = list_files(default_branch)
items = files.map { |file| [default_branch, file] }
repository.blobs_at(items).compact
end
def hook_attrs
......
---
title: Fix bug when snippet blobs array contain a nil value
merge_request: 54552
author:
type: fixed
......@@ -207,14 +207,14 @@ RSpec.describe Projects::SnippetsController do
subject
expect(assigns(:snippet)).to eq(project_snippet)
expect(assigns(:blobs)).to eq(project_snippet.blobs)
expect(assigns(:blobs).map(&:name)).to eq(project_snippet.blobs.map(&:name))
expect(response).to have_gitlab_http_status(:ok)
end
it 'does not show the blobs expanded by default' do
subject
expect(project_snippet.blobs.map(&:expanded?)).to be_all(false)
expect(assigns(:blobs).map(&:expanded?)).to be_all(false)
end
context 'when param expanded is set' do
......@@ -223,7 +223,7 @@ RSpec.describe Projects::SnippetsController do
it 'shows all blobs expanded' do
subject
expect(project_snippet.blobs.map(&:expanded?)).to be_all(true)
expect(assigns(:blobs).map(&:expanded?)).to be_all(true)
end
end
end
......
......@@ -496,6 +496,16 @@ RSpec.describe Snippet do
it 'returns array of blobs' do
expect(snippet.blobs).to all(be_a(Blob))
end
context 'when file does not exist' do
it 'removes nil values from the blobs array' do
allow(snippet).to receive(:list_files).and_return(%w(LICENSE non_existent_snippet_file))
blobs = snippet.blobs
expect(blobs.count).to eq 1
expect(blobs.first.name).to eq 'LICENSE'
end
end
end
end
......
......@@ -159,7 +159,7 @@ RSpec.describe SnippetPresenter do
let(:snippet) { create(:snippet, :repository, author: user) }
it 'returns repository first blob' do
expect(subject).to eq snippet.blobs.first
expect(subject.name).to eq snippet.blobs.first.name
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