Skip to content

Commit

Permalink
refactor: diff to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
cobbinma committed Feb 18, 2024
1 parent 011fe9f commit b034b5a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 25 deletions.
36 changes: 22 additions & 14 deletions bromide.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,27 +18,27 @@ 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)

if err := os.MkdirAll(snapshotDir, 0755); err != nil {
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())
}
Expand All @@ -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()
Expand All @@ -58,20 +59,27 @@ 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
}
}

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)
}
6 changes: 4 additions & 2 deletions bromide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
34 changes: 26 additions & 8 deletions cmd/bromide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
Expand Down Expand Up @@ -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),
},
Expand Down Expand Up @@ -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
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions internal/diff.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 4 additions & 1 deletion snapshots/Test_Snapshot.accepted
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
testStruct{example: "test", again: 5}
(bromide_test.testStruct) {
example: (*string)((len=5) "hello"),
again: (int) 5
}

0 comments on commit b034b5a

Please sign in to comment.