2019-11-07 22:06:48 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-19 10:26:44 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Highlight do
|
2016-01-19 10:26:44 -05:00
|
|
|
include RepoHelpers
|
|
|
|
|
2017-01-24 18:42:12 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-06-16 14:07:22 -04:00
|
|
|
let(:repository) { project.repository }
|
|
|
|
|
2018-09-06 00:34:25 -04:00
|
|
|
describe 'language provided' do
|
2016-06-16 14:52:40 -04:00
|
|
|
let(:highlighter) do
|
2018-09-06 00:34:25 -04:00
|
|
|
described_class.new('foo.erb', 'bar', language: 'erb?parent=json')
|
2016-06-22 15:53:58 -04:00
|
|
|
end
|
|
|
|
|
2018-09-06 00:34:25 -04:00
|
|
|
it 'sets correct lexer' do
|
|
|
|
expect(highlighter.lexer.tag).to eq 'erb'
|
|
|
|
expect(highlighter.lexer.parent.tag).to eq 'json'
|
2016-06-10 18:42:43 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-10 17:13:06 -04:00
|
|
|
|
|
|
|
describe '#highlight' do
|
2019-06-21 09:20:41 -04:00
|
|
|
let(:plain_text_file_name) { "test.txt" }
|
|
|
|
let(:plain_text_content) { "plain text contents" }
|
2018-09-11 00:37:44 -04:00
|
|
|
let(:file_name) { 'test.lisp' }
|
2019-06-21 09:20:41 -04:00
|
|
|
let(:content) { "(make-pathname :defaults name\n:type \"assem\")" }
|
2018-09-11 00:37:44 -04:00
|
|
|
let(:multiline_content) do
|
|
|
|
%q(
|
|
|
|
def test(input):
|
|
|
|
"""This is line 1 of a multi-line comment.
|
|
|
|
This is line 2.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'highlights' do
|
|
|
|
expected = %Q[<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>
|
2019-06-21 09:20:41 -04:00
|
|
|
<span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">)</span></span>]
|
2018-09-11 00:37:44 -04:00
|
|
|
|
|
|
|
expect(described_class.highlight(file_name, content)).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns plain version for unknown lexer context' do
|
2019-06-21 09:20:41 -04:00
|
|
|
result = described_class.highlight(plain_text_file_name, plain_text_content)
|
2018-09-11 00:37:44 -04:00
|
|
|
|
2019-06-21 09:20:41 -04:00
|
|
|
expect(result).to eq(%[<span id="LC1" class="line" lang="plaintext">plain text contents</span>])
|
2018-09-11 00:37:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns plain version for long content' do
|
|
|
|
stub_const('Gitlab::Highlight::MAXIMUM_TEXT_HIGHLIGHT_SIZE', 1)
|
|
|
|
result = described_class.highlight(file_name, content)
|
|
|
|
|
2019-06-21 09:20:41 -04:00
|
|
|
expect(result).to eq(%[<span id="LC1" class="line" lang="">(make-pathname :defaults name</span>\n<span id="LC2" class="line" lang="">:type "assem")</span>])
|
2018-09-11 00:37:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'highlights multi-line comments' do
|
|
|
|
result = described_class.highlight(file_name, multiline_content)
|
|
|
|
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.')
|
|
|
|
expect(lines[1].text).to eq(' This is line 2.')
|
|
|
|
expect(lines[2].text).to eq(' """')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'diff highlighting' do
|
|
|
|
let(:file_name) { 'test.diff' }
|
|
|
|
let(:content) { "+aaa\n+bbb\n- ccc\n ddd\n"}
|
|
|
|
let(:expected) do
|
|
|
|
%q(<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>)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'highlights each line properly' do
|
|
|
|
result = described_class.highlight(file_name, content)
|
|
|
|
|
|
|
|
expect(result).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 17:21:29 -04:00
|
|
|
describe 'with CRLF' do
|
|
|
|
let(:branch) { 'crlf-diff' }
|
|
|
|
let(:path) { 'files/whitespace' }
|
|
|
|
let(:blob) { repository.blob_at_branch(branch, path) }
|
|
|
|
let(:lines) do
|
2018-09-06 00:34:25 -04:00
|
|
|
described_class.highlight(blob.path, blob.data).lines
|
2017-06-06 17:21:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'strips extra LFs' do
|
|
|
|
expect(lines[0]).to eq("<span id=\"LC1\" class=\"line\" lang=\"plaintext\">test </span>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
it 'links dependencies via DependencyLinker' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Gitlab::DependencyLinker).to receive(:link)
|
|
|
|
.with('file.name', 'Contents', anything).and_call_original
|
2016-07-10 17:13:06 -04:00
|
|
|
|
|
|
|
described_class.highlight('file.name', 'Contents')
|
|
|
|
end
|
2018-08-16 11:27:20 -04:00
|
|
|
|
|
|
|
context 'timeout' do
|
|
|
|
subject { described_class.new('file.name', 'Contents') }
|
|
|
|
|
|
|
|
it 'utilizes timeout for web' do
|
|
|
|
expect(Timeout).to receive(:timeout).with(described_class::TIMEOUT_FOREGROUND).and_call_original
|
|
|
|
|
|
|
|
subject.highlight("Content")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'utilizes longer timeout for sidekiq' do
|
2019-12-22 04:07:51 -05:00
|
|
|
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
|
2018-08-16 11:27:20 -04:00
|
|
|
expect(Timeout).to receive(:timeout).with(described_class::TIMEOUT_BACKGROUND).and_call_original
|
|
|
|
|
|
|
|
subject.highlight("Content")
|
|
|
|
end
|
|
|
|
end
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
2016-01-19 10:26:44 -05:00
|
|
|
end
|