2015-05-23 23:34:03 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe BlobHelper do
|
2016-07-30 00:04:04 -04:00
|
|
|
include TreeHelper
|
|
|
|
|
2016-01-19 08:52:41 -05:00
|
|
|
let(:blob_name) { 'test.lisp' }
|
|
|
|
let(:no_context_content) { ":type \"assem\"))" }
|
|
|
|
let(:blob_content) { "(make-pathname :defaults name\n#{no_context_content}" }
|
|
|
|
let(:split_content) { blob_content.split("\n") }
|
|
|
|
let(:multiline_content) do
|
|
|
|
%q(
|
|
|
|
def test(input):
|
|
|
|
"""This is line 1 of a multi-line comment.
|
|
|
|
This is line 2.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
end
|
2015-05-23 23:34:03 -04:00
|
|
|
|
2016-01-19 08:52:41 -05:00
|
|
|
describe '#highlight' do
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'returns plaintext for unknown lexer context' do
|
2016-06-21 14:24:12 -04:00
|
|
|
result = helper.highlight(blob_name, no_context_content)
|
2017-03-13 18:39:22 -04:00
|
|
|
expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">:type "assem"))</span></code></pre>])
|
2015-05-23 23:34:03 -04:00
|
|
|
end
|
|
|
|
|
2018-01-15 10:00:58 -05:00
|
|
|
it 'returns plaintext for long blobs' do
|
|
|
|
stub_const('Blob::MAXIMUM_TEXT_HIGHLIGHT_SIZE', 1)
|
|
|
|
result = helper.highlight(blob_name, blob_content)
|
|
|
|
|
|
|
|
expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">(make-pathname :defaults name</span>\n<span id="LC2" class="line" lang="">:type "assem"))</span></code></pre>])
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'highlights single block' do
|
2017-03-13 18:39:22 -04:00
|
|
|
expected = %Q[<pre class="code highlight"><code><span id="LC1" class="line" lang="common_lisp"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>
|
|
|
|
<span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span></code></pre>]
|
2015-05-23 23:34:03 -04:00
|
|
|
|
2016-06-21 14:24:12 -04:00
|
|
|
expect(helper.highlight(blob_name, blob_content)).to eq(expected)
|
2015-05-23 23:34:03 -04:00
|
|
|
end
|
2015-07-31 01:31:53 -04:00
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'highlights multi-line comments' do
|
2016-06-21 14:24:12 -04:00
|
|
|
result = helper.highlight(blob_name, multiline_content)
|
2015-07-31 01:31:53 -04:00
|
|
|
html = Nokogiri::HTML(result)
|
|
|
|
lines = html.search('.s')
|
|
|
|
expect(lines.count).to eq(3)
|
|
|
|
expect(lines[0].text).to eq('"""This is line 1 of a multi-line comment.')
|
2016-01-19 08:52:41 -05:00
|
|
|
expect(lines[1].text).to eq(' This is line 2.')
|
|
|
|
expect(lines[2].text).to eq(' """')
|
2015-07-31 01:31:53 -04:00
|
|
|
end
|
2015-08-10 02:08:28 -04:00
|
|
|
|
|
|
|
context 'diff highlighting' do
|
|
|
|
let(:blob_name) { 'test.diff' }
|
|
|
|
let(:blob_content) { "+aaa\n+bbb\n- ccc\n ddd\n"}
|
|
|
|
let(:expected) do
|
2017-03-13 18:39:22 -04:00
|
|
|
%q(<pre class="code highlight"><code><span id="LC1" class="line" lang="diff"><span class="gi">+aaa</span></span>
|
|
|
|
<span id="LC2" class="line" lang="diff"><span class="gi">+bbb</span></span>
|
|
|
|
<span id="LC3" class="line" lang="diff"><span class="gd">- ccc</span></span>
|
|
|
|
<span id="LC4" class="line" lang="diff"> ddd</span></code></pre>)
|
2015-08-10 02:08:28 -04:00
|
|
|
end
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'highlights each line properly' do
|
2016-06-21 14:24:12 -04:00
|
|
|
result = helper.highlight(blob_name, blob_content)
|
2015-08-10 02:08:28 -04:00
|
|
|
expect(result).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
2015-05-23 23:34:03 -04:00
|
|
|
end
|
2016-01-19 08:52:41 -05:00
|
|
|
|
2017-04-20 19:01:47 -04:00
|
|
|
describe "#sanitize_svg_data" do
|
2016-03-25 01:39:58 -04:00
|
|
|
let(:input_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'unsanitized.svg') }
|
|
|
|
let(:data) { open(input_svg_path).read }
|
|
|
|
let(:expected_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'sanitized.svg') }
|
|
|
|
let(:expected) { open(expected_svg_path).read }
|
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'retains essential elements' do
|
2017-04-20 19:01:47 -04:00
|
|
|
expect(sanitize_svg_data(data)).to eq(expected)
|
2016-03-25 01:39:58 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-30 00:04:04 -04:00
|
|
|
|
|
|
|
describe "#edit_blob_link" do
|
2016-08-17 11:05:00 -04:00
|
|
|
let(:namespace) { create(:namespace, name: 'gitlab' )}
|
2017-01-25 17:10:32 -05:00
|
|
|
let(:project) { create(:project, :repository, namespace: namespace) }
|
2016-07-30 00:04:04 -04:00
|
|
|
|
|
|
|
before do
|
2017-04-06 12:36:38 -04:00
|
|
|
allow(self).to receive(:current_user).and_return(nil)
|
2016-08-17 11:05:00 -04:00
|
|
|
allow(self).to receive(:can_collaborate_with_project?).and_return(true)
|
2016-07-30 00:04:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'verifies blob is text' do
|
2016-08-17 11:05:00 -04:00
|
|
|
expect(helper).not_to receive(:blob_text_viewable?)
|
2016-07-30 00:04:04 -04:00
|
|
|
|
|
|
|
button = edit_blob_link(project, 'refs/heads/master', 'README.md')
|
|
|
|
|
|
|
|
expect(button).to start_with('<button')
|
|
|
|
end
|
2016-08-17 11:05:00 -04:00
|
|
|
|
|
|
|
it 'uses the passed blob instead retrieve from repository' do
|
|
|
|
blob = project.repository.blob_at('refs/heads/master', 'README.md')
|
|
|
|
|
|
|
|
expect(project.repository).not_to receive(:blob_at)
|
|
|
|
|
|
|
|
edit_blob_link(project, 'refs/heads/master', 'README.md', blob: blob)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a link with the proper route' do
|
|
|
|
link = edit_blob_link(project, 'master', 'README.md')
|
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/edit/master/README.md")
|
2016-08-17 11:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a link with the passed link_opts on the expected route' do
|
|
|
|
link = edit_blob_link(project, 'master', 'README.md', link_opts: { mr_id: 10 })
|
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/edit/master/README.md?mr_id=10")
|
2016-08-17 11:05:00 -04:00
|
|
|
end
|
2016-07-30 00:04:04 -04:00
|
|
|
end
|
2017-04-25 16:22:56 -04:00
|
|
|
|
|
|
|
context 'viewer related' do
|
|
|
|
include FakeBlobHelpers
|
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { build(:project, lfs_enabled: true) }
|
2017-04-26 16:48:49 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
|
|
|
|
end
|
2017-04-25 16:22:56 -04:00
|
|
|
|
|
|
|
let(:viewer_class) do
|
|
|
|
Class.new(BlobViewer::Base) do
|
2017-05-12 15:43:06 -04:00
|
|
|
include BlobViewer::ServerSide
|
|
|
|
|
2017-05-26 19:27:30 -04:00
|
|
|
self.collapse_limit = 1.megabyte
|
|
|
|
self.size_limit = 5.megabytes
|
2017-04-25 16:22:56 -04:00
|
|
|
self.type = :rich
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:viewer) { viewer_class.new(blob) }
|
|
|
|
let(:blob) { fake_blob }
|
|
|
|
|
|
|
|
describe '#blob_render_error_reason' do
|
|
|
|
context 'for error :too_large' do
|
2017-05-26 19:27:30 -04:00
|
|
|
context 'when the blob size is larger than the absolute size limit' do
|
2017-04-25 16:22:56 -04:00
|
|
|
let(:blob) { fake_blob(size: 10.megabytes) }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
2017-04-26 16:48:49 -04:00
|
|
|
expect(helper.blob_render_error_reason(viewer)).to eq('it is larger than 5 MB')
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-26 19:27:30 -04:00
|
|
|
context 'when the blob size is larger than the size limit' do
|
2017-04-25 16:22:56 -04:00
|
|
|
let(:blob) { fake_blob(size: 2.megabytes) }
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
2017-04-26 16:48:49 -04:00
|
|
|
expect(helper.blob_render_error_reason(viewer)).to eq('it is larger than 1 MB')
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
context 'for error :server_side_but_stored_externally' do
|
2017-04-26 16:48:49 -04:00
|
|
|
let(:blob) { fake_blob(lfs: true) }
|
|
|
|
|
2017-04-25 16:22:56 -04:00
|
|
|
it 'returns an error message' do
|
2017-04-26 16:48:49 -04:00
|
|
|
expect(helper.blob_render_error_reason(viewer)).to eq('it is stored in LFS')
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#blob_render_error_options' do
|
2017-04-26 16:48:49 -04:00
|
|
|
before do
|
|
|
|
assign(:project, project)
|
2017-04-13 12:47:28 -04:00
|
|
|
assign(:blob, blob)
|
2017-04-26 16:48:49 -04:00
|
|
|
assign(:id, File.join('master', blob.path))
|
|
|
|
|
|
|
|
controller.params[:controller] = 'projects/blob'
|
|
|
|
controller.params[:action] = 'show'
|
|
|
|
controller.params[:namespace_id] = project.namespace.to_param
|
|
|
|
controller.params[:project_id] = project.to_param
|
|
|
|
controller.params[:id] = File.join('master', blob.path)
|
|
|
|
end
|
|
|
|
|
2017-05-30 22:48:30 -04:00
|
|
|
context 'for error :collapsed' do
|
|
|
|
let(:blob) { fake_blob(size: 2.megabytes) }
|
2017-04-25 16:22:56 -04:00
|
|
|
|
2017-05-30 22:48:30 -04:00
|
|
|
it 'includes a "load it anyway" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/load it anyway/)
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
2017-05-30 22:48:30 -04:00
|
|
|
end
|
2017-04-25 16:22:56 -04:00
|
|
|
|
2017-05-30 22:48:30 -04:00
|
|
|
context 'for error :too_large' do
|
|
|
|
let(:blob) { fake_blob(size: 10.megabytes) }
|
2017-04-25 16:22:56 -04:00
|
|
|
|
2017-05-30 22:48:30 -04:00
|
|
|
it 'does not include a "load it anyway" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/load it anyway/)
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
context 'when the viewer is rich' do
|
|
|
|
context 'the blob is rendered as text' do
|
|
|
|
let(:blob) { fake_blob(path: 'file.md', size: 2.megabytes) }
|
|
|
|
|
|
|
|
it 'includes a "view the source" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/view the source/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the blob is not rendered as text' do
|
|
|
|
let(:blob) { fake_blob(path: 'file.pdf', binary: true, size: 2.megabytes) }
|
2017-04-25 16:22:56 -04:00
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
it 'does not include a "view the source" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
|
|
|
|
end
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
context 'when the viewer is not rich' do
|
|
|
|
before do
|
|
|
|
viewer_class.type = :simple
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:blob) { fake_blob(path: 'file.md', size: 2.megabytes) }
|
2017-04-25 16:22:56 -04:00
|
|
|
|
|
|
|
it 'does not include a "view the source" link' do
|
2017-04-26 16:48:49 -04:00
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
it 'includes a "download it" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/download it/)
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
2017-05-02 18:45:50 -04:00
|
|
|
end
|
2017-04-25 16:22:56 -04:00
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
context 'for error :server_side_but_stored_externally' do
|
2017-04-26 16:48:49 -04:00
|
|
|
let(:blob) { fake_blob(path: 'file.md', lfs: true) }
|
2017-04-25 16:22:56 -04:00
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
it 'does not include a "load it anyway" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/load it anyway/)
|
|
|
|
end
|
|
|
|
|
2017-04-25 16:22:56 -04:00
|
|
|
it 'does not include a "view the source" link' do
|
2017-04-26 16:48:49 -04:00
|
|
|
expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
it 'includes a "download it" link' do
|
|
|
|
expect(helper.blob_render_error_options(viewer)).to include(/download it/)
|
|
|
|
end
|
2017-04-25 16:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-23 23:34:03 -04:00
|
|
|
end
|