diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2019-01-07 19:12:30 -0200 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2019-01-08 12:45:19 -0200 |
commit | 8303eb8a9e5f90b4b764ff3e094c743a12b0cfe4 (patch) | |
tree | e3f003c7bb61bba962324b6aaa674d107e24bf99 | |
parent | ada8ccdb579bb791a67f6d2dd865a91137e702c8 (diff) | |
download | gitlab-ce-8303eb8a9e5f90b4b764ff3e094c743a12b0cfe4.tar.gz |
Adjusts duplicated line when commenting on unfolded diff lines
-rw-r--r-- | changelogs/unreleased/osw-fix-bottom-expansion-diff-comment.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/diff/lines_unfolder.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/diff/lines_unfolder_spec.rb | 115 |
3 files changed, 118 insertions, 13 deletions
diff --git a/changelogs/unreleased/osw-fix-bottom-expansion-diff-comment.yml b/changelogs/unreleased/osw-fix-bottom-expansion-diff-comment.yml new file mode 100644 index 00000000000..b2ac53312ae --- /dev/null +++ b/changelogs/unreleased/osw-fix-bottom-expansion-diff-comment.yml @@ -0,0 +1,5 @@ +--- +title: Adjusts duplicated line when commenting on unfolded diff lines (in the bottom) +merge_request: 24201 +author: +type: fixed diff --git a/lib/gitlab/diff/lines_unfolder.rb b/lib/gitlab/diff/lines_unfolder.rb index 9306b7e16a2..6cf904b2b2a 100644 --- a/lib/gitlab/diff/lines_unfolder.rb +++ b/lib/gitlab/diff/lines_unfolder.rb @@ -158,9 +158,14 @@ module Gitlab from = comment_position - UNFOLD_CONTEXT_SIZE - # There's no line before the match if it's in the top-most - # position. - prev_line_number = line_before_unfold_position&.old_pos || 0 + prev_line_number = + if bottom? + last_line.old_pos + else + # There's no line before the match if it's in the top-most + # position. + line_before_unfold_position&.old_pos || 0 + end if from <= prev_line_number + 1 @generate_top_match_line = false diff --git a/spec/lib/gitlab/diff/lines_unfolder_spec.rb b/spec/lib/gitlab/diff/lines_unfolder_spec.rb index f22c2c90334..8a470e12d04 100644 --- a/spec/lib/gitlab/diff/lines_unfolder_spec.rb +++ b/spec/lib/gitlab/diff/lines_unfolder_spec.rb @@ -301,8 +301,7 @@ describe Gitlab::Diff::LinesUnfolder do expected_diff_lines.each_with_index do |expected_line, i| line = new_diff_lines[i] - expect([line.old_pos, line.new_pos, line.text]) - .to eq([expected_line[0], expected_line[1], expected_line[2]]) + expect([line.old_pos, line.new_pos, line.text]).to eq(expected_line) end end @@ -403,8 +402,7 @@ describe Gitlab::Diff::LinesUnfolder do expected_diff_lines.each_with_index do |expected_line, i| line = new_diff_lines[i] - expect([line.old_pos, line.new_pos, line.text]) - .to eq([expected_line[0], expected_line[1], expected_line[2]]) + expect([line.old_pos, line.new_pos, line.text]).to eq(expected_line) end end @@ -505,8 +503,7 @@ describe Gitlab::Diff::LinesUnfolder do expected_diff_lines.each_with_index do |expected_line, i| line = new_diff_lines[i] - expect([line.old_pos, line.new_pos, line.text]) - .to eq([expected_line[0], expected_line[1], expected_line[2]]) + expect([line.old_pos, line.new_pos, line.text]).to eq(expected_line) end end @@ -604,8 +601,7 @@ describe Gitlab::Diff::LinesUnfolder do expected_diff_lines.each_with_index do |expected_line, i| line = new_diff_lines[i] - expect([line.old_pos, line.new_pos, line.text]) - .to eq([expected_line[0], expected_line[1], expected_line[2]]) + expect([line.old_pos, line.new_pos, line.text]).to eq(expected_line) end end @@ -729,8 +725,7 @@ describe Gitlab::Diff::LinesUnfolder do expected_diff_lines.each_with_index do |expected_line, i| line = new_diff_lines[i] - expect([line.old_pos, line.new_pos, line.text]) - .to eq([expected_line[0], expected_line[1], expected_line[2]]) + expect([line.old_pos, line.new_pos, line.text]).to eq(expected_line) end end @@ -746,5 +741,105 @@ describe Gitlab::Diff::LinesUnfolder do end end end + + context 'position requires bottom expansion and no new match line' do + let(:position) do + Gitlab::Diff::Position.new(base_sha: "1c59dfa64afbea8c721bb09a06a9d326c952ea19", + start_sha: "1c59dfa64afbea8c721bb09a06a9d326c952ea19", + head_sha: "1487062132228de836236c522fe52fed4980a46c", + old_path: "build-aux/flatpak/org.gnome.Nautilus.json", + new_path: "build-aux/flatpak/org.gnome.Nautilus.json", + position_type: "text", + old_line: 95, + new_line: 87) + end + + context 'blob lines' do + let(:expected_blob_lines) do + [[94, 94, " \"url\": \"https://gitlab.gnome.org/GNOME/nautilus.git\""], + [95, 95, " }"], + [96, 96, " ]"], + [97, 97, " }"], + [98, 98, " ]"]] + end + + it 'returns the extracted blob lines correctly' do + extracted_lines = subject.blob_lines + + expect(extracted_lines.size).to eq(5) + + extracted_lines.each_with_index do |line, i| + expect([line.old_line, line.new_line, line.text]).to eq(expected_blob_lines[i]) + end + end + end + + context 'diff lines' do + let(:expected_diff_lines) do + [[7, 7, "@@ -7,9 +7,6 @@"], + [7, 7, " \"tags\": [\"devel\", \"development\", \"nightly\"],"], + [8, 8, " \"desktop-file-name-prefix\": \"(Development) \","], + [9, 9, " \"finish-args\": ["], + [10, 10, "- \"--share=ipc\", \"--socket=x11\","], + [11, 10, "- \"--socket=wayland\","], + [12, 10, "- \"--talk-name=org.gnome.OnlineAccounts\","], + [13, 10, " \"--talk-name=org.freedesktop.Tracker1\","], + [14, 11, " \"--filesystem=home\","], + [15, 12, " \"--talk-name=org.gtk.vfs\", \"--talk-name=org.gtk.vfs.*\","], + [62, 59, "@@ -62,7 +59,7 @@"], + [62, 59, " },"], + [63, 60, " {"], + [64, 61, " \"name\": \"gnome-desktop\","], + [65, 62, "- \"config-opts\": [\"--disable-debug-tools\", \"--disable-udev\"],"], + [66, 62, "+ \"config-opts\": [\"--disable-debug-tools\", \"--disable-\"],"], + [66, 63, " \"sources\": ["], + [67, 64, " {"], + [68, 65, " \"type\": \"git\","], + [83, 80, "@@ -83,11 +80,6 @@"], + [83, 80, " \"buildsystem\": \"meson\","], + [84, 81, " \"builddir\": true,"], + [85, 82, " \"name\": \"nautilus\","], + [86, 83, "- \"config-opts\": ["], + [87, 83, "- \"-Denable-desktop=false\","], + [88, 83, "- \"-Denable-selinux=false\","], + [89, 83, "- \"--libdir=/app/lib\""], + [90, 83, "- ],"], + [91, 83, " \"sources\": ["], + [92, 84, " {"], + [93, 85, " \"type\": \"git\","], + # No new match line + + # Injected blob lines + [94, 86, " \"url\": \"https://gitlab.gnome.org/GNOME/nautilus.git\""], + [95, 87, " }"], + [96, 88, " ]"], + [97, 89, " }"], + [98, 90, " ]"]] + # end + end + + it 'return merge of blob lines with diff lines correctly' do + new_diff_lines = subject.unfolded_diff_lines + + expected_diff_lines.each_with_index do |expected_line, i| + line = new_diff_lines[i] + + expect([line.old_pos, line.new_pos, line.text]).to eq(expected_line) + end + end + + it 'merged lines have correct line codes' do + new_diff_lines = subject.unfolded_diff_lines + + new_diff_lines.each_with_index do |line, i| + old_pos, new_pos = expected_diff_lines[i][0], expected_diff_lines[i][1] + + unless line.type == 'match' + expect(line.line_code).to eq(Gitlab::Git.diff_line_code(diff_file.file_path, new_pos, old_pos)) + end + end + end + end + end end end |