2016-01-29 22:53:50 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Gitlab :: Diff :: 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-01-29 22:53:50 +05:30
let ( :commit ) { project . commit ( sample_commit . id ) }
2016-09-13 17:45:13 +05:30
let ( :diff ) { commit . raw_diffs . first }
2016-08-24 12:49:21 +05:30
let ( :diff_file ) { Gitlab :: Diff :: File . new ( diff , diff_refs : commit . diff_refs , repository : project . repository ) }
2016-01-29 22:53:50 +05:30
describe '#highlight' do
2016-04-02 18:10:28 +05:30
context " with a diff file " do
2017-09-10 17:25:29 +05:30
let ( :subject ) { described_class . new ( diff_file , repository : project . repository ) . highlight }
2016-01-29 22:53:50 +05:30
2017-08-17 22:00:37 +05:30
it 'returns Gitlab::Diff::Line elements' do
2016-04-02 18:10:28 +05:30
expect ( subject . first ) . to be_an_instance_of ( Gitlab :: Diff :: Line )
end
2016-01-29 22:53:50 +05:30
2017-08-17 22:00:37 +05:30
it 'does not modify "match" lines' do
2016-04-02 18:10:28 +05:30
expect ( subject [ 0 ] . text ) . to eq ( '@@ -6,12 +6,18 @@ module Popen' )
expect ( subject [ 22 ] . text ) . to eq ( '@@ -19,6 +25,7 @@ module Popen' )
end
2016-01-29 22:53:50 +05:30
2016-04-02 18:10:28 +05:30
it 'highlights and marks unchanged lines' do
2017-08-17 22:00:37 +05:30
code = %Q{ <span id="LC7" class="line" lang="ruby"> <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 }
2016-01-29 22:53:50 +05:30
2016-04-02 18:10:28 +05:30
expect ( subject [ 2 ] . text ) . to eq ( code )
end
2016-01-29 22:53:50 +05:30
2016-04-02 18:10:28 +05:30
it 'highlights and marks removed lines' do
2017-08-17 22:00:37 +05:30
code = %Q{ -<span id="LC9" class="line" lang="ruby"> <span class="k">raise</span> <span class="s2">"System commands must be given as an array of strings"</span></span> \n }
2016-01-29 22:53:50 +05:30
2016-04-02 18:10:28 +05:30
expect ( subject [ 4 ] . text ) . to eq ( code )
end
it 'highlights and marks added lines' do
2017-09-10 17:25:29 +05:30
code = %Q{ +<span id="LC9" class="line" lang="ruby"> <span class="k">raise</span> <span class="no"><span class="idiff left">RuntimeError</span></span><span class="p"><span class="idiff">,</span></span><span class="idiff right"> </span><span class="s2">"System commands must be given as an array of strings"</span></span> \n }
2016-04-02 18:10:28 +05:30
expect ( subject [ 5 ] . text ) . to eq ( code )
end
2016-01-29 22:53:50 +05:30
end
2016-04-02 18:10:28 +05:30
context " with diff lines " do
2017-09-10 17:25:29 +05:30
let ( :subject ) { described_class . new ( diff_file . diff_lines , repository : project . repository ) . highlight }
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
it 'returns Gitlab::Diff::Line elements' do
2016-04-02 18:10:28 +05:30
expect ( subject . first ) . to be_an_instance_of ( Gitlab :: Diff :: Line )
end
2017-08-17 22:00:37 +05:30
it 'does not modify "match" lines' do
2016-04-02 18:10:28 +05:30
expect ( subject [ 0 ] . text ) . to eq ( '@@ -6,12 +6,18 @@ module Popen' )
expect ( subject [ 22 ] . text ) . to eq ( '@@ -19,6 +25,7 @@ module Popen' )
end
it 'marks unchanged lines' do
2017-08-17 22:00:37 +05:30
code = %q{ def popen(cmd, path=nil) }
2016-04-02 18:10:28 +05:30
expect ( subject [ 2 ] . text ) . to eq ( code )
expect ( subject [ 2 ] . text ) . not_to be_html_safe
end
it 'marks removed lines' do
2017-08-17 22:00:37 +05:30
code = %q{ - raise "System commands must be given as an array of strings" }
2016-04-02 18:10:28 +05:30
expect ( subject [ 4 ] . text ) . to eq ( code )
expect ( subject [ 4 ] . text ) . not_to be_html_safe
end
it 'marks added lines' do
2017-09-10 17:25:29 +05:30
code = %q{ + raise <span class="idiff left right">RuntimeError, </span>"System commands must be given as an array of strings" }
2016-01-29 22:53:50 +05:30
2016-04-02 18:10:28 +05:30
expect ( subject [ 5 ] . text ) . to eq ( code )
expect ( subject [ 5 ] . text ) . to be_html_safe
end
2018-03-27 19:54:05 +05:30
context 'when the inline diff marker has an invalid range' do
before do
allow_any_instance_of ( Gitlab :: Diff :: InlineDiffMarker ) . to receive ( :mark ) . and_raise ( RangeError )
end
it 'keeps the original rich line' do
2018-10-15 14:42:47 +05:30
allow ( Gitlab :: Sentry ) . to receive ( :track_exception )
2018-03-27 19:54:05 +05:30
code = %q{ + raise RuntimeError, "System commands must be given as an array of strings" }
expect ( subject [ 5 ] . text ) . to eq ( code )
expect ( subject [ 5 ] . text ) . not_to be_html_safe
end
it 'reports to Sentry if configured' do
2018-10-15 14:42:47 +05:30
expect ( Gitlab :: Sentry ) . to receive ( :track_exception ) . and_call_original
2018-03-27 19:54:05 +05:30
2018-10-15 14:42:47 +05:30
expect { subject } . to raise_exception ( RangeError )
2018-03-27 19:54:05 +05:30
end
end
2016-01-29 22:53:50 +05:30
end
end
end