debian-mirror-gitlab/spec/helpers/diff_helper_spec.rb

306 lines
12 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
describe DiffHelper do
include RepoHelpers
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :repository) }
2016-01-29 22:53:50 +05:30
let(:repository) { project.repository }
2015-09-11 14:41:01 +05:30
let(:commit) { project.commit(sample_commit.id) }
2016-09-13 17:45:13 +05:30
let(:diffs) { commit.raw_diffs }
2015-09-11 14:41:01 +05:30
let(:diff) { diffs.first }
2017-09-10 17:25:29 +05:30
let(:diff_refs) { commit.diff_refs }
2016-08-24 12:49:21 +05:30
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
describe 'diff_view' do
2017-09-10 17:25:29 +05:30
it 'uses the view param over the cookie' do
controller.params[:view] = 'parallel'
helper.request.cookies[:diff_view] = 'inline'
expect(helper.diff_view).to eq :parallel
end
it 'returns the default value when the view param is invalid' do
controller.params[:view] = 'invalid'
expect(helper.diff_view).to eq :inline
end
2016-06-02 11:05:42 +05:30
it 'returns a valid value when cookie is set' do
helper.request.cookies[:diff_view] = 'parallel'
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
expect(helper.diff_view).to eq :parallel
2015-04-26 12:48:37 +05:30
end
2017-09-10 17:25:29 +05:30
it 'returns the default value when cookie is invalid' do
2016-06-02 11:05:42 +05:30
helper.request.cookies[:diff_view] = 'invalid'
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
expect(helper.diff_view).to eq :inline
2015-04-26 12:48:37 +05:30
end
2017-09-10 17:25:29 +05:30
it 'returns the default value when cookie is nil' do
2016-06-02 11:05:42 +05:30
expect(helper.request.cookies).to be_empty
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
expect(helper.diff_view).to eq :inline
2015-09-11 14:41:01 +05:30
end
end
2016-09-13 17:45:13 +05:30
describe 'diff_options' do
it 'returns no collapse false' do
2017-09-10 17:25:29 +05:30
expect(diff_options).to include(expanded: false)
2016-08-24 12:49:21 +05:30
end
2017-09-10 17:25:29 +05:30
it 'returns no collapse true if expanded' do
allow(controller).to receive(:params) { { expanded: true } }
expect(diff_options).to include(expanded: true)
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns no collapse true if action name diff_for_path' do
2016-08-24 12:49:21 +05:30
allow(controller).to receive(:action_name) { 'diff_for_path' }
2017-09-10 17:25:29 +05:30
expect(diff_options).to include(expanded: true)
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns paths if action name diff_for_path and param old path' do
allow(controller).to receive(:params) { { old_path: 'lib/wadus.rb' } }
allow(controller).to receive(:action_name) { 'diff_for_path' }
expect(diff_options[:paths]).to include('lib/wadus.rb')
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns paths if action name diff_for_path and param new path' do
allow(controller).to receive(:params) { { new_path: 'lib/wadus.rb' } }
allow(controller).to receive(:action_name) { 'diff_for_path' }
expect(diff_options[:paths]).to include('lib/wadus.rb')
2015-09-11 14:41:01 +05:30
end
end
2016-04-02 18:10:28 +05:30
describe '#diff_line_content' do
2017-08-17 22:00:37 +05:30
context 'when the line is empty' do
it 'returns a non breaking space' do
expect(diff_line_content(nil)).to eq(' ')
end
it 'returns an HTML-safe string' do
expect(diff_line_content(nil)).to be_html_safe
end
end
context 'when the line is not empty' do
context 'when the line starts with +, -, or a space' do
it 'strips the first character' do
expect(diff_line_content('+new line')).to eq('new line')
expect(diff_line_content('-new line')).to eq('new line')
expect(diff_line_content(' new line')).to eq('new line')
end
context 'when the line is HTML-safe' do
it 'returns an HTML-safe string' do
expect(diff_line_content('+new line'.html_safe)).to be_html_safe
expect(diff_line_content('-new line'.html_safe)).to be_html_safe
expect(diff_line_content(' new line'.html_safe)).to be_html_safe
end
end
context 'when the line is not HTML-safe' do
it 'returns a non-HTML-safe string' do
expect(diff_line_content('+new line')).not_to be_html_safe
expect(diff_line_content('-new line')).not_to be_html_safe
expect(diff_line_content(' new line')).not_to be_html_safe
end
end
end
context 'when the line does not start with a +, -, or a space' do
it 'returns the string' do
expect(diff_line_content('@@ -6,12 +6,18 @@ module Popen')).to eq('@@ -6,12 +6,18 @@ module Popen')
end
context 'when the line is HTML-safe' do
it 'returns an HTML-safe string' do
expect(diff_line_content('@@ -6,12 +6,18 @@ module Popen'.html_safe)).to be_html_safe
end
end
context 'when the line is not HTML-safe' do
it 'returns a non-HTML-safe string' do
expect(diff_line_content('@@ -6,12 +6,18 @@ module Popen')).not_to be_html_safe
end
end
end
2015-04-26 12:48:37 +05:30
end
2016-04-02 18:10:28 +05:30
end
describe "#mark_inline_diffs" do
let(:old_line) { %{abc 'def'} }
let(:new_line) { %{abc "def"} }
it "returns strings with marked inline diffs" do
marked_old_line, marked_new_line = mark_inline_diffs(old_line, new_line)
2015-04-26 12:48:37 +05:30
2018-04-05 14:03:07 +05:30
expect(marked_old_line).to eq(%q{abc <span class="idiff left right deletion">&#39;def&#39;</span>})
2016-04-02 18:10:28 +05:30
expect(marked_old_line).to be_html_safe
2018-04-05 14:03:07 +05:30
expect(marked_new_line).to eq(%q{abc <span class="idiff left right addition">&quot;def&quot;</span>})
2016-04-02 18:10:28 +05:30
expect(marked_new_line).to be_html_safe
2016-01-29 22:53:50 +05:30
end
2018-04-05 14:03:07 +05:30
context 'when given HTML' do
it 'sanitizes it' do
old_line = %{test.txt}
new_line = %{<img src=x onerror=alert(document.domain)>}
marked_old_line, marked_new_line = mark_inline_diffs(old_line, new_line)
expect(marked_old_line).to eq(%q{<span class="idiff left right deletion">test.txt</span>})
expect(marked_old_line).to be_html_safe
expect(marked_new_line).to eq(%q{<span class="idiff left right addition">&lt;img src=x onerror=alert(document.domain)&gt;</span>})
expect(marked_new_line).to be_html_safe
end
it 'sanitizes the entire line, not just the changes' do
old_line = %{<img src=x onerror=alert(document.domain)>}
new_line = %{<img src=y onerror=alert(document.domain)>}
marked_old_line, marked_new_line = mark_inline_diffs(old_line, new_line)
expect(marked_old_line).to eq(%q{&lt;img src=<span class="idiff left right deletion">x</span> onerror=alert(document.domain)&gt;})
expect(marked_old_line).to be_html_safe
expect(marked_new_line).to eq(%q{&lt;img src=<span class="idiff left right addition">y</span> onerror=alert(document.domain)&gt;})
expect(marked_new_line).to be_html_safe
end
end
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
describe '#parallel_diff_discussions' do
let(:discussion) { { 'abc_3_3' => 'comment' } }
let(:diff_file) { double(line_code: 'abc_3_3') }
before do
helper.instance_variable_set(:@grouped_diff_discussions, discussion)
end
it 'does not put comments on nonewline lines' do
left = Gitlab::Diff::Line.new('\\nonewline', 'old-nonewline', 3, 3, 3)
right = Gitlab::Diff::Line.new('\\nonewline', 'new-nonewline', 3, 3, 3)
result = helper.parallel_diff_discussions(left, right, diff_file)
expect(result).to eq([nil, nil])
end
it 'puts comments on added lines' do
left = Gitlab::Diff::Line.new('\\nonewline', 'old-nonewline', 3, 3, 3)
right = Gitlab::Diff::Line.new('new line', 'new', 3, 3, 3)
result = helper.parallel_diff_discussions(left, right, diff_file)
expect(result).to eq([nil, 'comment'])
end
it 'puts comments on unchanged lines' do
left = Gitlab::Diff::Line.new('unchanged line', nil, 3, 3, 3)
right = Gitlab::Diff::Line.new('unchanged line', nil, 3, 3, 3)
result = helper.parallel_diff_discussions(left, right, diff_file)
expect(result).to eq(['comment', nil])
end
end
2016-09-13 17:45:13 +05:30
describe "#diff_match_line" do
let(:old_pos) { 40 }
let(:new_pos) { 50 }
let(:text) { 'some_text' }
2017-08-17 22:00:37 +05:30
it "generates foldable top match line for inline view with empty text by default" do
2016-09-13 17:45:13 +05:30
output = diff_match_line old_pos, new_pos
expect(output).to be_html_safe
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
expect(output).to have_css "td:nth-child(2):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
expect(output).to have_css 'td:nth-child(3):not(.parallel).line_content.match', text: ''
end
2017-08-17 22:00:37 +05:30
it "allows to define text and bottom option" do
2016-09-13 17:45:13 +05:30
output = diff_match_line old_pos, new_pos, text: text, bottom: true
expect(output).to be_html_safe
expect(output).to have_css "td:nth-child(1).diff-line-num.unfold.js-unfold.js-unfold-bottom.old_line[data-linenumber='#{old_pos}']", text: '...'
expect(output).to have_css "td:nth-child(2).diff-line-num.unfold.js-unfold.js-unfold-bottom.new_line[data-linenumber='#{new_pos}']", text: '...'
expect(output).to have_css 'td:nth-child(3):not(.parallel).line_content.match', text: text
end
2017-08-17 22:00:37 +05:30
it "generates match line for parallel view" do
2016-09-13 17:45:13 +05:30
output = diff_match_line old_pos, new_pos, text: text, view: :parallel
expect(output).to be_html_safe
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
expect(output).to have_css "td:nth-child(3):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
expect(output).to have_css 'td:nth-child(4).line_content.match.parallel', text: text
end
2017-08-17 22:00:37 +05:30
it "allows to generate only left match line for parallel view" do
2016-09-13 17:45:13 +05:30
output = diff_match_line old_pos, nil, text: text, view: :parallel
expect(output).to be_html_safe
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
expect(output).not_to have_css 'td:nth-child(3)'
end
2017-08-17 22:00:37 +05:30
it "allows to generate only right match line for parallel view" do
2016-09-13 17:45:13 +05:30
output = diff_match_line nil, new_pos, text: text, view: :parallel
expect(output).to be_html_safe
expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
expect(output).not_to have_css 'td:nth-child(3)'
end
end
2017-09-10 17:25:29 +05:30
context 'viewer related' do
let(:viewer) { diff_file.simple_viewer }
before do
assign(:project, project)
end
describe '#diff_render_error_reason' do
context 'for error :too_large' do
before do
expect(viewer).to receive(:render_error).and_return(:too_large)
end
it 'returns an error message' do
expect(helper.diff_render_error_reason(viewer)).to eq('it is too large')
end
end
context 'for error :server_side_but_stored_externally' do
before do
expect(viewer).to receive(:render_error).and_return(:server_side_but_stored_externally)
expect(diff_file).to receive(:external_storage).and_return(:lfs)
end
it 'returns an error message' do
expect(helper.diff_render_error_reason(viewer)).to eq('it is stored in LFS')
end
end
end
describe '#diff_render_error_options' do
it 'includes a "view the blob" link' do
expect(helper.diff_render_error_options(viewer)).to include(/view the blob/)
end
end
end
2018-03-17 18:26:18 +05:30
context '#diff_file_path_text' do
it 'returns full path by default' do
expect(diff_file_path_text(diff_file)).to eq(diff_file.new_path)
end
it 'returns truncated path' do
expect(diff_file_path_text(diff_file, max: 10)).to eq("...open.rb")
end
end
2015-04-26 12:48:37 +05:30
end