From b034b5adf1d044bfc1e3b1e7b7bfc32033822d78 Mon Sep 17 00:00:00 2001 From: "matthew.cobbing" Date: Sun, 18 Feb 2024 17:31:46 +0000 Subject: [PATCH] refactor: diff to internal --- bromide.go | 36 +++++++++++++++++++------------- bromide_test.go | 6 ++++-- cmd/bromide/main.go | 34 +++++++++++++++++++++++------- go.mod | 1 + internal/diff.go | 13 ++++++++++++ snapshots/Test_Snapshot.accepted | 5 ++++- 6 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 internal/diff.go diff --git a/bromide.go b/bromide.go index 29a551b..f6d8433 100644 --- a/bromide.go +++ b/bromide.go @@ -7,8 +7,8 @@ import ( "strings" "testing" - "github.com/hexops/valast" - "github.com/sergi/go-diff/diffmatchpatch" + "github.com/cobbinma/bromide/internal" + "github.com/davecgh/go-spew/spew" ) const folder = "snapshots" @@ -18,12 +18,12 @@ func Snapshot[K comparable](t *testing.T, item K) { currentDir, err := os.Getwd() if err != nil { - t.Errorf("Error getting current directory: %v", err) + t.Errorf("error getting current directory: %v", err) } snapshotDir := fmt.Sprintf("%s/%s", currentDir, folder) - accepted := fmt.Sprintf("%s/%s.accepted", snapshotDir, t.Name()) - neww := fmt.Sprintf("%s/%s.new", snapshotDir, t.Name()) + acceptedPath := fmt.Sprintf("%s/%s%s", snapshotDir, t.Name(), internal.Accepted.Extension()) + pendingPath := fmt.Sprintf("%s/%s%s", snapshotDir, t.Name(), internal.Pending.Extension()) incoming := serialize(item) @@ -31,14 +31,14 @@ func Snapshot[K comparable](t *testing.T, item K) { panic("unable to create snapshot directory") } - file, err := os.Open(accepted) + file, err := os.Open(acceptedPath) if err != nil { if !os.IsNotExist(err) { panic("unable to open existing snapshot") } // test does not have accepted snapshot - file, err := os.OpenFile(neww, os.O_RDWR|os.O_CREATE, 0644) + file, err := os.OpenFile(pendingPath, os.O_RDWR|os.O_CREATE, 0644) if err != nil { panic("unable to create new snapshot" + err.Error()) } @@ -49,6 +49,7 @@ func Snapshot[K comparable](t *testing.T, item K) { } t.Errorf("new snapshot 📸") + t.Log("to update snapshots run `bromide review`") return } defer file.Close() @@ -58,14 +59,13 @@ func Snapshot[K comparable](t *testing.T, item K) { io.Copy(existing, file) if existing.String() != incoming { - dmp := diffmatchpatch.New() + diff := internal.Diff(existing.String(), incoming) - diffs := dmp.DiffMain(existing.String(), incoming, true) + t.Log("snapshot mismatch") + t.Log(diff) + t.Log("to update snapshots run `bromide review`") - t.Log("snapshot does not match") - t.Log(dmp.DiffPrettyText(diffs)) - - os.WriteFile(neww, []byte(incoming), 0644) + os.WriteFile(pendingPath, []byte(incoming), 0644) t.Fail() return @@ -73,5 +73,13 @@ func Snapshot[K comparable](t *testing.T, item K) { } func serialize[K any](item K) string { - return valast.StringWithOptions(item, &valast.Options{Unqualify: false}) + config := &spew.ConfigState{ + Indent: " ", + SortKeys: true, + DisablePointerAddresses: true, + DisableCapacities: true, + SpewKeys: true, + DisableMethods: false, + } + return config.Sdump(item) } diff --git a/bromide_test.go b/bromide_test.go index bf167a3..0ae5eb1 100644 --- a/bromide_test.go +++ b/bromide_test.go @@ -7,14 +7,16 @@ import ( ) type testStruct struct { - example string + example *string again int } func Test_Snapshot(t *testing.T) { + text := "hello" hello := testStruct{ - example: "test", + example: &text, again: 5, } + bromide.Snapshot(t, hello) } diff --git a/cmd/bromide/main.go b/cmd/bromide/main.go index c44c953..8a8433e 100644 --- a/cmd/bromide/main.go +++ b/cmd/bromide/main.go @@ -8,7 +8,6 @@ import ( "github.com/cobbinma/bromide/internal" "github.com/manifoldco/promptui" - "github.com/sergi/go-diff/diffmatchpatch" "github.com/urfave/cli/v2" ) @@ -30,10 +29,31 @@ type Review struct { new Snapshot } +func (r Review) TestName() string { + splits := strings.SplitAfter(r.path, "/") + if len(splits) == 0 { + return "" + } + + return splits[len(splits)-1] +} + func (r Review) Path(status internal.ReviewState) string { return r.path + status.Extension() } +func (r Review) Title() string { + title := "" + switch { + case r.old == nil: + title = "New Snapshot" + default: + title = "Mismatched Snapshot" + } + + return title +} + func main() { app := &cli.App{ Name: "bromide", @@ -92,14 +112,12 @@ func main() { existing = string(review.old.contents) } - dmp := diffmatchpatch.New() - diffs := dmp.DiffMain(existing, accepted, true) - - fmt.Printf("reviewing %v of %v\n", i+1, len(reviews)) - fmt.Println(dmp.DiffPrettyText(diffs)) + fmt.Printf("reviewing %v of %v\n\n", i+1, len(reviews)) + fmt.Printf("%s\n", review.TestName()) + fmt.Println(internal.Diff(existing, accepted)) prompt := promptui.Select{ - Label: "snapshot review", + Label: review.Title(), Items: []string{ string(accept), string(reject), string(skip), }, @@ -136,7 +154,7 @@ func main() { } } - fmt.Printf("reviewed %v snapshot(s) 📸\n", len(reviews)) + fmt.Printf("\nreviewed %v snapshot(s) 📸\n", len(reviews)) return nil }, diff --git a/go.mod b/go.mod index 15d87b7..a540101 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/go-cmp v0.5.9 // indirect golang.org/x/mod v0.7.0 // indirect golang.org/x/sys v0.3.0 // indirect diff --git a/internal/diff.go b/internal/diff.go new file mode 100644 index 0000000..e7154cd --- /dev/null +++ b/internal/diff.go @@ -0,0 +1,13 @@ +package internal + +import ( + "github.com/sergi/go-diff/diffmatchpatch" +) + +func Diff(old, new string) string { + dmp := diffmatchpatch.New() + + diffs := dmp.DiffMain(old, new, true) + + return dmp.DiffPrettyText(diffs) +} diff --git a/snapshots/Test_Snapshot.accepted b/snapshots/Test_Snapshot.accepted index 5feffe5..28eda58 100644 --- a/snapshots/Test_Snapshot.accepted +++ b/snapshots/Test_Snapshot.accepted @@ -1 +1,4 @@ -testStruct{example: "test", again: 5} \ No newline at end of file +(bromide_test.testStruct) { + example: (*string)((len=5) "hello"), + again: (int) 5 +}