Remove nil values from snippet blobs

Snippets Vue code, doesn't expect to receive any nil blob,
and it doesn't make sense to have it. In this commit, we
remove nil values from the list.
parent 458553d0
......@@ -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