From f3eff05a45d691309d88ccb76b1fc00bd3819a25 Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Mon, 19 Sep 2022 13:39:50 -0400 Subject: [PATCH] engine: switched to multilinediff package (#47) Improve diff readability by switching to the multilinediff package which has a much more readable two-column diff setup. Eventually this package will be improved to have coloured output, which this tool can benefit from when it's ready. --- engine/engine.go | 6 +-- go.mod | 4 +- go.sum | 6 ++- internal/diff/diff.go | 34 ----------------- internal/diff/diff_test.go | 78 -------------------------------------- internal/diff/reporter.go | 58 ---------------------------- 6 files changed, 10 insertions(+), 176 deletions(-) delete mode 100644 internal/diff/diff.go delete mode 100644 internal/diff/diff_test.go delete mode 100644 internal/diff/reporter.go diff --git a/engine/engine.go b/engine/engine.go index 9c4d40e..d4a0722 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -18,8 +18,8 @@ import ( "fmt" "os" + "github.com/RageCage64/multilinediff" "github.com/google/yamlfmt" - "github.com/google/yamlfmt/internal/diff" "github.com/google/yamlfmt/internal/paths" ) @@ -91,7 +91,7 @@ func (e *Engine) LintFile(path string) error { if err != nil { return err } - diffContent := diff.MultilineStringDiff(string(yamlBytes), string(formatted)) + diffContent := multilinediff.MultilineDiff(string(yamlBytes), string(formatted), "\n") if diffContent != "" { return fmt.Errorf(diffContent) } @@ -130,6 +130,6 @@ func (e *Engine) DryRunFile(path string) (string, error) { if err != nil { return "", err } - diffContent := diff.MultilineStringDiff(string(yamlBytes), string(formatted)) + diffContent := multilinediff.MultilineDiff(string(yamlBytes), string(formatted), "\n") return diffContent, nil } diff --git a/go.mod b/go.mod index 7e3cb70..c65b65c 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,10 @@ go 1.19 require ( github.com/RageCage64/go-utf8-codepoint-converter v0.1.0 + github.com/RageCage64/multilinediff v0.1.0 github.com/bmatcuk/doublestar/v4 v4.2.0 - github.com/google/go-cmp v0.5.8 github.com/mitchellh/mapstructure v1.5.0 gopkg.in/yaml.v3 v3.0.1 ) + +require github.com/google/go-cmp v0.5.9 // indirect diff --git a/go.sum b/go.sum index eaaa5c6..a52aca2 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,11 @@ github.com/RageCage64/go-utf8-codepoint-converter v0.1.0 h1:6GreQRSQApXW1sgeFXMBLDdxSC6DCzu4lKBS/LEyrSA= github.com/RageCage64/go-utf8-codepoint-converter v0.1.0/go.mod h1:asNWDxR7n0QIQyZNYTlpNk6Dg7GkUnxtCXho987uen8= +github.com/RageCage64/multilinediff v0.1.0 h1:P9Iht5Vj4SHSmTJhQPJnySzTlX/cpL99kN3RJxejipQ= +github.com/RageCage64/multilinediff v0.1.0/go.mod h1:pKr+KLgP0gvRzA+yv0/IUaYQuBYN1ucWysvsL58aMP0= github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/internal/diff/diff.go b/internal/diff/diff.go deleted file mode 100644 index a6ac577..0000000 --- a/internal/diff/diff.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package diff - -import ( - "strings" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" -) - -func MultilineStringDiff(a, b string) string { - var reporter prettyReporter - cmp.Diff( - a, b, - cmpopts.AcyclicTransformer("multiline", func(s string) []string { - return strings.Split(s, "\n") - }), - cmp.Reporter(&reporter), - ) - return reporter.String() -} diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go deleted file mode 100644 index 5bf3516..0000000 --- a/internal/diff/diff_test.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package diff_test - -import ( - "fmt" - "testing" - - "github.com/google/yamlfmt/internal/diff" -) - -func TestMultilineDiff(t *testing.T) { - testCases := []struct { - name string - before string - after string - }{ - { - name: "multiple line diff", - before: `this -is -a -first input`, - after: `this -is -the -second input`, - }, - { - name: "add blank line", - before: `foo -bar`, - after: `foo - -bar`, - }, - { - name: "remove blank line", - before: `foo - -bar`, - after: `foo -bar`, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - diffStr := diff.MultilineStringDiff(tc.before, tc.after) - if diffStr == "" { - t.Log("there should have been a diff") - t.Fail() - } - fmt.Println(diffStr) - }) - } -} - -func TestMultilineDiffNoDiff(t *testing.T) { - before := "content" - - diffStr := diff.MultilineStringDiff(before, before) - if diffStr != "" { - t.Fatalf("diff output should be empty with no diff, output contained:\n%s", diffStr) - } -} diff --git a/internal/diff/reporter.go b/internal/diff/reporter.go deleted file mode 100644 index 95f1e73..0000000 --- a/internal/diff/reporter.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package diff - -import ( - "fmt" - "strings" - - "github.com/google/go-cmp/cmp" -) - -type prettyReporter struct { - path cmp.Path - lines []string - diffCount int -} - -func (r *prettyReporter) PushStep(ps cmp.PathStep) { - r.path = append(r.path, ps) -} - -func (r *prettyReporter) Report(rs cmp.Result) { - vx, vy := r.path.Last().Values() - if !rs.Equal() { - r.diffCount++ - if vx.IsValid() { - r.lines = append(r.lines, fmt.Sprintf("- %+v", vx)) - } - if vy.IsValid() { - r.lines = append(r.lines, fmt.Sprintf("+ %+v", vy)) - } - } else { - r.lines = append(r.lines, fmt.Sprintf(" %+v", vx)) - } -} - -func (r *prettyReporter) PopStep() { - r.path = r.path[:len(r.path)-1] -} - -func (r *prettyReporter) String() string { - if r.diffCount == 0 { - return "" - } - return strings.Join(r.lines, "\n") -}