Skip to content

Commit

Permalink
internal/diff: first import
Browse files Browse the repository at this point in the history
This CL exports internal/diff.{Format,Files} to streamline the display
of text and file diffs.
Built on top of github.com/pkg/diff.

Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed Nov 4, 2022
1 parent 063750f commit e79cf64
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -18,6 +18,7 @@ require (
github.com/peterh/liner v1.2.2
github.com/pierrec/lz4/v4 v4.1.15
github.com/pierrec/xxHash v0.1.5
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/sbinet/npyio v0.7.0
github.com/ulikunitz/xz v0.5.10
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -93,6 +93,8 @@ github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo=
github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -133,8 +135,6 @@ golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3/go.mod h1:NOZ3BPKG0ec/BKJQgnvsSFpcKLM5xXVWnvZS97DWHgE=
golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY=
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 h1:tnebWN09GYg9OLPss1KXj8txwZc6X6uMr6VFdcGNbHw=
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08 h1:LtBIgSqNhkuC9gA3BFjGy5obHQT1lnmNsMDFSqWzQ5w=
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp/shiny v0.0.0-20220722155223-a9213eeb770e h1:pkl1Ko5DrhA4ezwKwdnmO7H1sKmMy9qLuYKRjS7SlmE=
Expand Down
48 changes: 48 additions & 0 deletions internal/diff/diff.go
@@ -0,0 +1,48 @@
// Copyright ©2022 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package diff provides basic text comparison (like Unix's diff(1)).
package diff // import "go-hep.org/x/hep/internal/diff"

import (
"fmt"
"os"
"strings"

"github.com/pkg/diff"
)

// Format returns a formatted diff of the two texts,
// showing the entire text and the minimum line-level
// additions and removals to turn got into want.
// (That is, lines only in got appear with a leading -,
// and lines only in want appear with a leading +.)
func Format(got, want string) string {
o := new(strings.Builder)
err := diff.Text("a/got", "b/want", got, want, o)
if err != nil {
panic(err)
}
return o.String()
}

// Files returns a formatted diff of the two texts from the provided
// two file names.
// Files returns nil if they compare equal.
func Files(got, want string) error {
g, err := os.ReadFile(got)
if err != nil {
return fmt.Errorf("diff: could not read chk file %q: %w", got, err)
}
w, err := os.ReadFile(want)
if err != nil {
return fmt.Errorf("diff: could not read ref file %q: %w", want, err)
}

if got, want := string(g), string(w); got != want {
return fmt.Errorf("diff: files differ:\n%s", Format(got, want))
}

return nil
}

0 comments on commit e79cf64

Please sign in to comment.