From d4d9ed150388d8b09d03ec2efecff9730c9601d9 Mon Sep 17 00:00:00 2001 From: Lars Dehler Date: Thu, 28 Dec 2023 19:08:31 +0100 Subject: [PATCH] fix incorrect newline handling --- assets/multiline/expected-dyff-spruce.human | 17 +++++++++++++---- assets/multiline/from.yml | 18 +++++++++++------- assets/multiline/to.yml | 18 +++++++++++------- pkg/dyff/output_human.go | 8 ++++++-- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/assets/multiline/expected-dyff-spruce.human b/assets/multiline/expected-dyff-spruce.human index e8a2c42..413a13c 100644 --- a/assets/multiline/expected-dyff-spruce.human +++ b/assets/multiline/expected-dyff-spruce.human @@ -13,18 +13,27 @@ files.newline.content - ± value change in multiline text (three inserts, three deletions) + ± value change in multiline text (four inserts, four deletions)   +  -   - This line will change 1  + This line changed 1  UnChanged line +    - This line will change 2  + This line changed 2 +  UnChanged line +   +   +  -  +  Moved line +  +       UnChanged line  - This line will change 3  + This line changed 3 +  +      @@ -81,8 +90,8 @@  [22 lines unchanged)]    End line 1 -  End line 1 -  End line 1 -  End line 1 +  End line 2 +  End line 3 +  End line 4 diff --git a/assets/multiline/from.yml b/assets/multiline/from.yml index 9f06105..4881d47 100644 --- a/assets/multiline/from.yml +++ b/assets/multiline/from.yml @@ -15,14 +15,18 @@ files: This line will change 1 UnChanged line - + This line will change 2 - - UnChanged line - This line will change 3 + + Moved line + + + UnChanged line + This line will change 3 + - name: complex content: | @@ -91,6 +95,6 @@ files: Truncated line Truncated line End line 1 - End line 1 - End line 1 - End line 1 + End line 2 + End line 3 + End line 4 diff --git a/assets/multiline/to.yml b/assets/multiline/to.yml index 94966e9..f3ee4c6 100644 --- a/assets/multiline/to.yml +++ b/assets/multiline/to.yml @@ -12,13 +12,17 @@ files: # + to keep trailing newlines content: |+ - This line changed 1 UnChanged line - + This line changed 2 - - + UnChanged line + + + Moved line + + + UnChanged line This line changed 3 @@ -90,6 +94,6 @@ files: Truncated line Truncated line End line 1 - End line 1 - End line 1 - End line 1 + End line 2 + End line 3 + End line 4 diff --git a/pkg/dyff/output_human.go b/pkg/dyff/output_human.go index f352683..0fbb76c 100644 --- a/pkg/dyff/output_human.go +++ b/pkg/dyff/output_human.go @@ -371,14 +371,18 @@ func (report *HumanReport) writeStringDiff(output stringWriter, from string, to continue } // add amount of unchanged lines as configured - lines := strings.Split(strings.TrimSuffix(d.Text, "\n"), "\n") + lines := strings.Split(d.Text, "\n") lower := int(math.Min(float64(len(lines)), float64(multilineContextLines))) upper := len(lines) - multilineContextLines + // if string ends with \n we need to display one more line on the upper limit + if strings.HasSuffix(d.Text, "\n") { + upper-- + } var val string if upper <= lower { val = strings.Join(lines, "\n") } else { - val = fmt.Sprintf("%s\n\n[%s unchanged)]\n\n%s\n", + val = fmt.Sprintf("%s\n\n[%s unchanged)]\n\n%s", strings.Join(lines[:lower], "\n"), text.Plural((upper-lower), "line"), strings.Join(lines[upper:], "\n"))