Refactor Gitlab::Highlight and fix tests
This commit is contained in:
parent
0a8039eb77
commit
512bebe21d
7 changed files with 132 additions and 136 deletions
|
@ -1,6 +1,10 @@
|
|||
module BlobHelper
|
||||
def highlight(blob_name, blob_content, nowrap: false, continue: false)
|
||||
Gitlab::Highlight.highlight(blob_name, blob_content, nowrap: nowrap, continue: continue)
|
||||
def highlighter(blob_name, blob_content, nowrap: false)
|
||||
Gitlab::Highlight.new(blob_name, blob_content, nowrap: nowrap)
|
||||
end
|
||||
|
||||
def highlight(blob_name, blob_content, nowrap: false)
|
||||
Gitlab::Highlight.highlight(blob_name, blob_content, nowrap: nowrap)
|
||||
end
|
||||
|
||||
def no_highlight_files
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
.file-content.blame.highlight
|
||||
%table
|
||||
- current_line = 1
|
||||
- blame_highlighter = highlighter(@blob.name, @blob.data, nowrap: true)
|
||||
- @blame.each do |blame_group|
|
||||
%tr
|
||||
%td.blame-commit
|
||||
|
@ -41,5 +42,5 @@
|
|||
%pre{class: 'code highlight white'}
|
||||
%code
|
||||
- blame_group[:lines].each do |line|
|
||||
:erb
|
||||
<%= highlight(@blob.name, line, nowrap: true, continue: true).html_safe %>
|
||||
:preserve
|
||||
#{blame_highlighter.highlight(line)}
|
||||
|
|
|
@ -9,5 +9,4 @@
|
|||
%i.fa.fa-link
|
||||
= i
|
||||
.blob-content{data: {blob_id: blob.id}}
|
||||
:preserve
|
||||
#{highlight(blob.name, blob.data)}
|
||||
= highlight(blob.name, blob.data)
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
module Gitlab
|
||||
class Highlight
|
||||
def self.highlight(blob_name, blob_content, nowrap: true, continue: false)
|
||||
formatter = rouge_formatter(nowrap: nowrap)
|
||||
|
||||
lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText
|
||||
formatter.format(lexer.lex(blob_content, continue: continue)).html_safe
|
||||
def self.highlight(blob_name, blob_content, nowrap: true)
|
||||
new(blob_name, blob_content, nowrap: nowrap).highlight(blob_content, continue: false)
|
||||
end
|
||||
|
||||
def self.highlight_lines(repository, ref, file_name)
|
||||
|
@ -14,9 +11,26 @@ module Gitlab
|
|||
highlight(file_name, blob.data).lines.map!(&:html_safe)
|
||||
end
|
||||
|
||||
def initialize(blob_name, blob_content, nowrap: true)
|
||||
@formatter = rouge_formatter(nowrap: nowrap)
|
||||
@lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText
|
||||
end
|
||||
|
||||
def highlight(text, continue: true)
|
||||
@formatter.format(lex(text, continue: continue)).html_safe
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.rouge_formatter(options = {})
|
||||
def lex(text, continue: true)
|
||||
if @lexer == Rouge::Lexers::PlainText
|
||||
@lexer.lex(text)
|
||||
else
|
||||
@lexer.lex(text, continue: continue)
|
||||
end
|
||||
end
|
||||
|
||||
def rouge_formatter(options = {})
|
||||
options = options.reverse_merge(
|
||||
nowrap: true,
|
||||
cssclass: 'code highlight',
|
||||
|
|
98
spec/fixtures/parallel_diff_result.yml
vendored
98
spec/fixtures/parallel_diff_result.yml
vendored
|
@ -10,37 +10,37 @@
|
|||
:text: "@@ -6,12 +6,18 @@ module Popen"
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_6_6
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 6
|
||||
:text: |2
|
||||
<span id="LC6" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_6_6
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 6
|
||||
:text: |2
|
||||
<span id="LC6" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_6_6
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 7
|
||||
:text: |2
|
||||
<span id="LC7" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_7_7
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 7
|
||||
:text: |2
|
||||
<span id="LC7" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_7_7
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 8
|
||||
:text: |2
|
||||
<span id="LC8" class="line"> <span class="k">unless</span> <span class="n">cmd</span><span class="p">.</span><span class="nf">is_a?</span><span class="p">(</span><span class="no">Array</span><span class="p">)</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_8_8
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 8
|
||||
:text: |2
|
||||
<span id="LC8" class="line"> <span class="k">unless</span> <span class="n">cmd</span><span class="p">.</span><span class="nf">is_a?</span><span class="p">(</span><span class="no">Array</span><span class="p">)</span></span>
|
||||
|
@ -55,40 +55,40 @@
|
|||
:type: new
|
||||
:number: 9
|
||||
:text: |
|
||||
+<span id="LC9" class="line"> <span class="k">raise</span> <span class="no">RuntimeError</span><span class="p">,</span> <span class="s2">"System commands must be given as an array of strings"</span></span>
|
||||
+<span id="LC9" class="line"> <span class="k">raise</span> <span class="no"><span class='idiff'>RuntimeError</span></span><span class="p"><span class='idiff'>,</span></span> <span class="s2">"System commands must be given as an array of strings"</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 10
|
||||
:text: |2
|
||||
<span id="LC10" class="line"> <span class="k">end</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_10
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 10
|
||||
:text: |2
|
||||
<span id="LC10" class="line"> <span class="k">end</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_10
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 11
|
||||
:text: |2
|
||||
<span id="LC11" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_11_11
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 11
|
||||
:text: |2
|
||||
<span id="LC11" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_11_11
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 12
|
||||
:text: |2
|
||||
<span id="LC12" class="line"> <span class="n">path</span> <span class="o">||=</span> <span class="no">Dir</span><span class="p">.</span><span class="nf">pwd</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_12_12
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 12
|
||||
:text: |2
|
||||
<span id="LC12" class="line"> <span class="n">path</span> <span class="o">||=</span> <span class="no">Dir</span><span class="p">.</span><span class="nf">pwd</span></span>
|
||||
|
@ -101,9 +101,9 @@
|
|||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_13_13
|
||||
:right:
|
||||
:type: old
|
||||
:number:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code:
|
||||
:line_code:
|
||||
- :left:
|
||||
:type: old
|
||||
:number: 14
|
||||
|
@ -117,8 +117,8 @@
|
|||
+<span id="LC13" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_13
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_14
|
||||
:right:
|
||||
|
@ -128,8 +128,8 @@
|
|||
+<span id="LC14" class="line"> <span class="n">vars</span> <span class="o">=</span> <span class="p">{</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_14
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_15
|
||||
:right:
|
||||
|
@ -139,8 +139,8 @@
|
|||
+<span id="LC15" class="line"> <span class="s2">"PWD"</span> <span class="o">=></span> <span class="n">path</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_15
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_16
|
||||
:right:
|
||||
|
@ -150,8 +150,8 @@
|
|||
+<span id="LC16" class="line"> <span class="p">}</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_16
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_17
|
||||
:right:
|
||||
|
@ -161,8 +161,8 @@
|
|||
+<span id="LC17" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_17
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_18
|
||||
:right:
|
||||
|
@ -172,8 +172,8 @@
|
|||
+<span id="LC18" class="line"> <span class="n">options</span> <span class="o">=</span> <span class="p">{</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_18
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_19
|
||||
:right:
|
||||
|
@ -183,8 +183,8 @@
|
|||
+<span id="LC19" class="line"> <span class="ss">chdir: </span><span class="n">path</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_19
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_20
|
||||
:right:
|
||||
|
@ -194,37 +194,37 @@
|
|||
+<span id="LC20" class="line"> <span class="p">}</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_20
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 15
|
||||
:text: |2
|
||||
<span id="LC21" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_21
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 21
|
||||
:text: |2
|
||||
<span id="LC21" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_15_21
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 16
|
||||
:text: |2
|
||||
<span id="LC22" class="line"> <span class="k">unless</span> <span class="no">File</span><span class="p">.</span><span class="nf">directory?</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_16_22
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 22
|
||||
:text: |2
|
||||
<span id="LC22" class="line"> <span class="k">unless</span> <span class="no">File</span><span class="p">.</span><span class="nf">directory?</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_16_22
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 17
|
||||
:text: |2
|
||||
<span id="LC23" class="line"> <span class="no">FileUtils</span><span class="p">.</span><span class="nf">mkdir_p</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_17_23
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 23
|
||||
:text: |2
|
||||
<span id="LC23" class="line"> <span class="no">FileUtils</span><span class="p">.</span><span class="nf">mkdir_p</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>
|
||||
|
@ -240,44 +240,44 @@
|
|||
:text: "@@ -19,6 +25,7 @@ module Popen"
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_19_25
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 19
|
||||
:text: |2
|
||||
<span id="LC25" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_19_25
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 25
|
||||
:text: |2
|
||||
<span id="LC25" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_19_25
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 20
|
||||
:text: |2
|
||||
<span id="LC26" class="line"> <span class="vi">@cmd_output</span> <span class="o">=</span> <span class="s2">""</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_20_26
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 26
|
||||
:text: |2
|
||||
<span id="LC26" class="line"> <span class="vi">@cmd_output</span> <span class="o">=</span> <span class="s2">""</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_20_26
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 21
|
||||
:text: |2
|
||||
<span id="LC27" class="line"> <span class="vi">@cmd_status</span> <span class="o">=</span> <span class="mi">0</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_21_27
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 27
|
||||
:text: |2
|
||||
<span id="LC27" class="line"> <span class="vi">@cmd_status</span> <span class="o">=</span> <span class="mi">0</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_21_27
|
||||
- :left:
|
||||
:type:
|
||||
:number:
|
||||
:type:
|
||||
:number:
|
||||
:text: ''
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_22_28
|
||||
:right:
|
||||
|
@ -287,37 +287,37 @@
|
|||
+<span id="LC28" class="line"></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_22_28
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 22
|
||||
:text: |2
|
||||
<span id="LC29" class="line"> <span class="no">Open3</span><span class="p">.</span><span class="nf">popen3</span><span class="p">(</span><span class="n">vars</span><span class="p">,</span> <span class="o">*</span><span class="n">cmd</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span><span class="p">,</span> <span class="n">wait_thr</span><span class="o">|</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_22_29
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 29
|
||||
:text: |2
|
||||
<span id="LC29" class="line"> <span class="no">Open3</span><span class="p">.</span><span class="nf">popen3</span><span class="p">(</span><span class="n">vars</span><span class="p">,</span> <span class="o">*</span><span class="n">cmd</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span><span class="p">,</span> <span class="n">wait_thr</span><span class="o">|</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_22_29
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 23
|
||||
:text: |2
|
||||
<span id="LC30" class="line"> <span class="vi">@cmd_output</span> <span class="o"><<</span> <span class="n">stdout</span><span class="p">.</span><span class="nf">read</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_23_30
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 30
|
||||
:text: |2
|
||||
<span id="LC30" class="line"> <span class="vi">@cmd_output</span> <span class="o"><<</span> <span class="n">stdout</span><span class="p">.</span><span class="nf">read</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_23_30
|
||||
- :left:
|
||||
:type:
|
||||
:type:
|
||||
:number: 24
|
||||
:text: |2
|
||||
<span id="LC31" class="line"> <span class="vi">@cmd_output</span> <span class="o"><<</span> <span class="n">stderr</span><span class="p">.</span><span class="nf">read</span></span>
|
||||
:line_code: 2f6fcd96b88b36ce98c38da085c795a27d92a3dd_24_31
|
||||
:right:
|
||||
:type:
|
||||
:type:
|
||||
:number: 31
|
||||
:text: |2
|
||||
<span id="LC31" class="line"> <span class="vi">@cmd_output</span> <span class="o"><<</span> <span class="n">stderr</span><span class="p">.</span><span class="nf">read</span></span>
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe BlobHelper do
|
||||
describe 'highlight' do
|
||||
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
|
||||
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
|
||||
|
||||
describe '#highlight' do
|
||||
it 'should return plaintext for unknown lexer context' do
|
||||
result = highlight(blob_name, no_context_content, nowrap: true, continue: false)
|
||||
result = helper.highlight(blob_name, no_context_content, nowrap: true)
|
||||
expect(result).to eq('<span id="LC1" class="line">:type "assem"))</span>')
|
||||
end
|
||||
|
||||
|
@ -24,28 +24,17 @@ describe BlobHelper do
|
|||
expected = %Q[<span id="LC1" class="line"><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"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span>]
|
||||
|
||||
expect(highlight(blob_name, blob_content, nowrap: true, continue: false)).to eq(expected)
|
||||
end
|
||||
|
||||
it 'should highlight continued blocks' do
|
||||
# Both lines have LC1 as ID since formatter doesn't support continue at the moment
|
||||
expected = [
|
||||
'<span id="LC1" class="line"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>',
|
||||
'<span id="LC1" class="line"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span>'
|
||||
]
|
||||
|
||||
result = split_content.map{ |content| highlight(blob_name, content, nowrap: true, continue: true) }
|
||||
expect(result).to eq(expected)
|
||||
expect(helper.highlight(blob_name, blob_content, nowrap: true)).to eq(expected)
|
||||
end
|
||||
|
||||
it 'should highlight multi-line comments' do
|
||||
result = highlight(blob_name, multiline_content, nowrap: true, continue: false)
|
||||
result = helper.highlight(blob_name, multiline_content, nowrap: true)
|
||||
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(' """')
|
||||
expect(lines[1].text).to eq(' This is line 2.')
|
||||
expect(lines[2].text).to eq(' """')
|
||||
end
|
||||
|
||||
context 'diff highlighting' do
|
||||
|
@ -59,9 +48,23 @@ describe BlobHelper do
|
|||
end
|
||||
|
||||
it 'should highlight each line properly' do
|
||||
result = highlight(blob_name, blob_content, nowrap: true, continue: false)
|
||||
result = helper.highlight(blob_name, blob_content, nowrap: true)
|
||||
expect(result).to eq(expected)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#highlighter" do
|
||||
it 'should highlight continued blocks' do
|
||||
# Both lines have LC1 as ID since formatter doesn't support continue at the moment
|
||||
expected = [
|
||||
'<span id="LC1" class="line"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>',
|
||||
'<span id="LC1" class="line"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span>'
|
||||
]
|
||||
|
||||
highlighter = helper.highlighter(blob_name, blob_content, nowrap: true)
|
||||
result = split_content.map{ |content| highlighter.highlight(content) }
|
||||
expect(result).to eq(expected)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,59 +8,34 @@ describe Gitlab::Diff::Highlight, lib: true do
|
|||
let(:diff) { commit.diffs.first }
|
||||
let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent, commit]) }
|
||||
|
||||
describe '.process_diff_lines' do
|
||||
context 'when processing Gitlab::Diff::Line objects' do
|
||||
let(:diff_lines) { Gitlab::Diff::Highlight.process_diff_lines(diff_file) }
|
||||
describe '#highlight' do
|
||||
let(:diff_lines) { Gitlab::Diff::Highlight.new(diff_file).highlight }
|
||||
|
||||
it 'should return Gitlab::Diff::Line elements' do
|
||||
expect(diff_lines.first).to be_an_instance_of(Gitlab::Diff::Line)
|
||||
end
|
||||
it 'should return Gitlab::Diff::Line elements' do
|
||||
expect(diff_lines.first).to be_an_instance_of(Gitlab::Diff::Line)
|
||||
end
|
||||
|
||||
it 'should highlight the code' do
|
||||
code = %Q{ <span id="LC7" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>\n}
|
||||
it 'should not modify "match" lines' do
|
||||
expect(diff_lines[0].text).to eq('@@ -6,12 +6,18 @@ module Popen')
|
||||
expect(diff_lines[22].text).to eq('@@ -19,6 +25,7 @@ module Popen')
|
||||
end
|
||||
|
||||
expect(diff_lines[2].text).to eq(code)
|
||||
end
|
||||
it 'should highlight unchanged lines' do
|
||||
code = %Q{ <span id="LC7" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>\n}
|
||||
|
||||
it 'should not generate the inline diff markup' do
|
||||
expect(diff_lines[5].text).not_to match(Regexp.new(Regexp.escape('<span class="idiff">')))
|
||||
end
|
||||
expect(diff_lines[2].text).to eq(code)
|
||||
end
|
||||
|
||||
it 'should not modify "match" lines' do
|
||||
expect(diff_lines[0].text).to eq('@@ -6,12 +6,18 @@ module Popen')
|
||||
expect(diff_lines[22].text).to eq('@@ -19,6 +25,7 @@ module Popen')
|
||||
end
|
||||
it 'should highlight removed lines' do
|
||||
code = %Q{-<span id="LC9" class="line"> <span class="k">raise</span> <span class="s2">"System commands must be given as an array of strings"</span></span>\n}
|
||||
|
||||
it 'should highlight unchanged lines' do
|
||||
code = %Q{ <span id="LC7" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>\n}
|
||||
expect(diff_lines[4].text).to eq(code)
|
||||
end
|
||||
|
||||
expect(diff_lines[2].text).to eq(code)
|
||||
end
|
||||
it 'should highlight added lines' do
|
||||
code = %Q{+<span id="LC9" class="line"> <span class="k">raise</span> <span class="no"><span class='idiff'>RuntimeError</span></span><span class="p"><span class='idiff'>,</span></span> <span class="s2">"System commands must be given as an array of strings"</span></span>\n}
|
||||
|
||||
it 'should highlight added lines' do
|
||||
code = %Q{+<span id="LC9" class="line"> <span class="k">raise</span> <span class="no">RuntimeError</span><span class="p">,</span> <span class="s2">"System commands must be given as an array of strings"</span></span>\n}
|
||||
|
||||
expect(diff_lines[5].text).to eq(code)
|
||||
end
|
||||
|
||||
it 'should highlight removed lines' do
|
||||
code = %Q{-<span id="LC9" class="line"> <span class="k">raise</span> <span class="s2">"System commands must be given as an array of strings"</span></span>\n}
|
||||
|
||||
expect(diff_lines[4].text).to eq(code)
|
||||
end
|
||||
expect(diff_lines[5].text).to eq(code)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.highlight_lines' do
|
||||
let(:lines) do
|
||||
Gitlab::Diff::Highlight.highlight_lines(project.repository, commit.id, 'files/ruby/popen.rb')
|
||||
end
|
||||
|
||||
it 'should properly highlight all the lines' do
|
||||
expect(lines[4]).to eq(%Q{<span id="LC5" class="line"> <span class="kp">extend</span> <span class="nb">self</span></span>\n})
|
||||
expect(lines[21]).to eq(%Q{<span id="LC22" class="line"> <span class="k">unless</span> <span class="no">File</span><span class="p">.</span><span class="nf">directory?</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>\n})
|
||||
expect(lines[26]).to eq(%Q{<span id="LC27" class="line"> <span class="vi">@cmd_status</span> <span class="o">=</span> <span class="mi">0</span></span>\n})
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue