Commit afde97a1 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch...

Merge branch '32079-misleading-suggestion-is-created-when-made-at-the-last-line-of-a-file-with-no-newline-at-end' into 'master'

Resolve "Misleading suggestion is created when made at the last line of a file with "\ No newline at end of file""

Closes #32079

See merge request gitlab-org/gitlab!22732
parents 048eaa02 e67589a5
---
title: Fixes a new line issue with suggestions in the last line of a file
merge_request: 22732
author:
type: fixed
...@@ -18,7 +18,7 @@ module Gitlab ...@@ -18,7 +18,7 @@ module Gitlab
private private
def raw_diff def raw_diff
"#{diff_header}\n#{from_content_as_diff}#{to_content_as_diff}" "#{diff_header}\n#{from_content_as_diff}\n#{to_content_as_diff}"
end end
def diff_header def diff_header
...@@ -26,7 +26,7 @@ module Gitlab ...@@ -26,7 +26,7 @@ module Gitlab
end end
def from_content_as_diff def from_content_as_diff
from_content.lines.map { |line| line.prepend('-') }.join from_content.lines.map { |line| line.prepend('-') }.join.delete_suffix("\n")
end end
def to_content_as_diff def to_content_as_diff
......
...@@ -51,5 +51,20 @@ describe Gitlab::Diff::SuggestionDiff do ...@@ -51,5 +51,20 @@ describe Gitlab::Diff::SuggestionDiff do
expect(diff_lines[index].to_hash).to include(expected_line) expect(diff_lines[index].to_hash).to include(expected_line)
end end
end end
describe 'when the suggestion is for the last line of a file' do
it 'returns a correct value if there is no newline at the end of the file' do
from_content = "One line test"
to_content = "Successful test!"
suggestion = instance_double(Suggestion, from_line: 1,
from_content: from_content,
to_content: to_content)
diff_lines = described_class.new(suggestion).diff_lines
expect(diff_lines.first.text).to eq("-One line test")
expect(diff_lines.last.text).to eq("+Successful test!")
end
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment