require 'spec_helper' describe Gitlab::Highlight do include RepoHelpers let(:project) { create(:project, :repository) } let(:repository) { project.repository } describe 'language provided' do let(:highlighter) do described_class.new('foo.erb', 'bar', language: 'erb?parent=json') end it 'sets correct lexer' do expect(highlighter.lexer.tag).to eq 'erb' expect(highlighter.lexer.parent.tag).to eq 'json' end end describe '#highlight' do let(:file_name) { 'test.lisp' } let(:no_context_content) { ":type \"assem\"))" } let(:content) { "(make-pathname :defaults name\n#{no_context_content}" } 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[(make-pathname :defaults name :type "assem"))] expect(described_class.highlight(file_name, content)).to eq(expected) end it 'returns plain version for unknown lexer context' do result = described_class.highlight(file_name, no_context_content) expect(result).to eq(%[:type "assem"))]) 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) expect(result).to eq(%[(make-pathname :defaults name\n:type "assem"))]) 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(+aaa +bbb - ccc ddd) end it 'highlights each line properly' do result = described_class.highlight(file_name, content) expect(result).to eq(expected) end end describe 'with CRLF' do let(:branch) { 'crlf-diff' } let(:path) { 'files/whitespace' } let(:blob) { repository.blob_at_branch(branch, path) } let(:lines) do described_class.highlight(blob.path, blob.data).lines end it 'strips extra LFs' do expect(lines[0]).to eq("test ") 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 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 allow(Sidekiq).to receive(:server?).and_return(true) expect(Timeout).to receive(:timeout).with(described_class::TIMEOUT_BACKGROUND).and_call_original subject.highlight("Content") end end end end