Commit 3403eee0 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch...

Merge branch '205401-cannot-view-content-of-moved-file-without-changes-in-merge-request-backend' into 'master'

Add alternate_viewer information for renamed viewers

See merge request gitlab-org/gitlab!29108
parents 4b975914 236aa0fd
......@@ -88,6 +88,7 @@ class DiffFileBaseEntity < Grape::Entity
expose :b_mode
expose :viewer, using: DiffViewerEntity
expose :alternate_viewer, using: DiffViewerEntity
expose :old_size do |diff_file|
diff_file.old_blob&.raw_size
......
......@@ -314,6 +314,10 @@ module Gitlab
@rich_viewer = rich_viewer_class&.new(self)
end
def alternate_viewer
alternate_viewer_class&.new(self)
end
def rendered_as_text?(ignore_errors: true)
simple_viewer.is_a?(DiffViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?)
end
......@@ -419,6 +423,17 @@ module Gitlab
return if collapsed?
return unless diffable?
return unless modified_file?
find_renderable_viewer_class(classes)
end
def alternate_viewer_class
return unless viewer.class == DiffViewer::Renamed
find_renderable_viewer_class(RICH_VIEWERS) || (DiffViewer::Text if text?)
end
def find_renderable_viewer_class(classes)
return if different_type? || external_storage_error?
verify_binary = !stored_externally?
......
......@@ -567,6 +567,61 @@ describe Gitlab::Diff::File do
end
end
describe '#alternate_viewer' do
subject { diff_file.alternate_viewer }
where(:viewer_class) do
[
DiffViewer::Image,
DiffViewer::Collapsed,
DiffViewer::NotDiffable,
DiffViewer::Text,
DiffViewer::NoPreview,
DiffViewer::Added,
DiffViewer::Deleted,
DiffViewer::ModeChanged,
DiffViewer::ModeChanged,
DiffViewer::NoPreview
]
end
with_them do
let(:viewer) { viewer_class.new(diff_file) }
before do
allow(diff_file).to receive(:viewer).and_return(viewer)
end
it { is_expected.to be_nil }
end
context 'when viewer is DiffViewer::Renamed' do
let(:viewer) { DiffViewer::Renamed.new(diff_file) }
before do
allow(diff_file).to receive(:viewer).and_return(viewer)
end
context 'when it can be rendered as text' do
it { is_expected.to be_a(DiffViewer::Text) }
end
context 'when it can be rendered as image' do
let(:commit) { project.commit('2f63565e7aac07bcdadb654e253078b727143ec4') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('files/images/6049019_460s.jpg') }
it { is_expected.to be_a(DiffViewer::Image) }
end
context 'when it is something else' do
let(:commit) { project.commit('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
let(:diff_file) { commit.diffs.diff_file_with_new_path('Gemfile.zip') }
it { is_expected.to be_nil }
end
end
end
describe '#rendered_as_text?' do
context 'when the simple viewer is text-based' do
let(:commit) { project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d') }
......
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