Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |