debian-mirror-gitlab/spec/lib/gitlab/highlight_spec.rb

138 lines
4.8 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-01-29 22:53:50 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Highlight do
2016-01-29 22:53:50 +05:30
include RepoHelpers
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2016-08-24 12:49:21 +05:30
let(:repository) { project.repository }
2018-12-13 13:39:08 +05:30
describe 'language provided' do
2016-08-24 12:49:21 +05:30
let(:highlighter) do
2018-12-13 13:39:08 +05:30
described_class.new('foo.erb', 'bar', language: 'erb?parent=json')
2016-08-24 12:49:21 +05:30
end
2018-12-13 13:39:08 +05:30
it 'sets correct lexer' do
expect(highlighter.lexer.tag).to eq 'erb'
expect(highlighter.lexer.parent.tag).to eq 'json'
2017-09-10 17:25:29 +05:30
end
2018-12-13 13:39:08 +05:30
end
2016-08-24 12:49:21 +05:30
2018-12-13 13:39:08 +05:30
describe '#highlight' do
2019-09-30 21:07:59 +05:30
let(:plain_text_file_name) { "test.txt" }
let(:plain_text_content) { "plain text contents" }
2018-12-13 13:39:08 +05:30
let(:file_name) { 'test.lisp' }
2019-09-30 21:07:59 +05:30
let(:content) { "(make-pathname :defaults name\n:type \"assem\")" }
2018-12-13 13:39:08 +05:30
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-09-30 21:07:59 +05:30
<span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">)</span></span>]
2018-12-13 13:39:08 +05:30
expect(described_class.highlight(file_name, content)).to eq(expected)
end
it 'returns plain version for unknown lexer context' do
2019-09-30 21:07:59 +05:30
result = described_class.highlight(plain_text_file_name, plain_text_content)
2018-12-13 13:39:08 +05:30
2019-09-30 21:07:59 +05:30
expect(result).to eq(%[<span id="LC1" class="line" lang="plaintext">plain text contents</span>])
2016-08-24 12:49:21 +05:30
end
2021-06-08 01:23:25 +05:30
context 'when content is too long to be highlighted' do
let(:result) { described_class.highlight(file_name, content) } # content is 44 bytes
2016-08-24 12:49:21 +05:30
2021-06-08 01:23:25 +05:30
before do
stub_config(extra: { 'maximum_text_highlight_size_kilobytes' => 0.0001 } ) # 1.024 bytes
end
it 'returns plain version for long content' do
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>])
end
2018-12-13 13:39:08 +05:30
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' }
2022-08-27 11:52:29 +05:30
let(:content) { "+aaa\n+bbb\n- ccc\n ddd\n" }
2018-12-13 13:39:08 +05:30
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)
2016-08-24 12:49:21 +05:30
end
2021-04-29 21:17:54 +05:30
context 'when start line number is set' do
let(:expected) do
%q(<span id="LC10" class="line" lang="diff"><span class="gi">+aaa</span></span>
<span id="LC11" class="line" lang="diff"><span class="gi">+bbb</span></span>
<span id="LC12" class="line" lang="diff"><span class="gd">- ccc</span></span>
<span id="LC13" class="line" lang="diff"> ddd</span>)
end
it 'highlights each line properly' do
result = described_class.new(file_name, content).highlight(content, context: { line_number: 10 })
expect(result).to eq(expected)
end
end
2016-08-24 12:49:21 +05:30
end
2017-09-10 17:25:29 +05:30
describe 'with CRLF' do
let(:branch) { 'crlf-diff' }
let(:path) { 'files/whitespace' }
let(:blob) { repository.blob_at_branch(branch, path) }
let(:lines) do
2018-12-13 13:39:08 +05:30
described_class.highlight(blob.path, blob.data).lines
2017-09-10 17:25:29 +05:30
end
it 'strips extra LFs' do
expect(lines[0]).to eq("<span id=\"LC1\" class=\"line\" lang=\"plaintext\">test </span>")
end
end
it 'links dependencies via DependencyLinker' do
expect(Gitlab::DependencyLinker).to receive(:link)
.with('file.name', 'Contents', anything).and_call_original
described_class.highlight('file.name', 'Contents')
end
2018-11-08 19:23:39 +05:30
context 'timeout' do
2022-05-07 20:08:51 +05:30
subject(:highlight) { described_class.new('file.rb', 'begin', language: 'ruby').highlight('Content') }
2018-11-08 19:23:39 +05:30
2022-05-07 20:08:51 +05:30
it 'falls back to plaintext on timeout' do
allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
2022-07-23 23:45:48 +05:30
expect(Gitlab::RenderTimeout).to receive(:timeout).and_raise(Timeout::Error)
2018-11-08 19:23:39 +05:30
2022-05-07 20:08:51 +05:30
expect(Rouge::Lexers::PlainText).to receive(:lex).and_call_original
2021-06-08 01:23:25 +05:30
2022-05-07 20:08:51 +05:30
highlight
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
end
end
2016-01-29 22:53:50 +05:30
end