diff --git a/README.md b/README.md index cc83149..4283f30 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A [birdie](https://github.com/giacomocavalieri/birdie) and [insta](https://githu ![New snapshot screen](./assets/screenshot-new.png "New snapshot view") -![Snapshot review CLI](./assets/screenshots-diff-cli.png "Snapshot diff view (CLI)") +![Snapshot review CLI](./assets/screenshot-diff-cli.png "Snapshot diff view (CLI)") ## Installation @@ -15,14 +15,11 @@ go get github.com/ptdewey/freeze ## Usage ```go -package yourpackage_test +package package_test func TestSomething(t *testing.T) { result := SomeFunction("foo") freeze.Snap(t, result) - - // To capture the calling function name use SnapFunc - freeze.SnapFunc(t, SomeFunction("bar")) } ``` @@ -32,9 +29,28 @@ To review a set of snapshots, run: go run github.com/ptdewey/freeze/cmd/freeze review ``` - +Freeze can also be used programmatically: + +```go +// Example: tools/freeze/main.go +package main + +import "github.com/ptdewey/freeze" + +func main() { + // This will start the CLI review tool + freeze.Review() +} +``` + +Which can then be run with: + +```sh +go run tools/freeze/main.go +``` -Freeze also includes (in a separate Go module) a [Bubbletea](https://github.com/charmbracelet/bubbletea) TUI in [cmd/tui/main.go](./cmd/tui/main.go). (The TUI is shipped in a separate module to make the added dependencies optional) +Freeze also includes (in a separate Go module) a [Bubbletea](https://github.com/charmbracelet/bubbletea) TUI in [cmd/tui/main.go](./cmd/tui/main.go). +(The TUI is shipped in a separate module to make the added dependencies optional) ### TUI Usage diff --git a/__snapshots__/test_snap_func.snap b/__snapshots__/test_snap_func.snap deleted file mode 100644 index 5daa53b..0000000 --- a/__snapshots__/test_snap_func.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -title: TestSnapFunc -test_name: TestSnapFunc -file_path: -func_name: -version: 0.1.0 ---- -"helper result" diff --git a/__snapshots__/test_snap_func_another_helper.snap b/__snapshots__/test_snap_func_another_helper.snap deleted file mode 100644 index 9d17838..0000000 --- a/__snapshots__/test_snap_func_another_helper.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -title: TestSnapFuncAnotherHelper -test_name: TestSnapFuncAnotherHelper -file_path: -func_name: -version: 0.1.0 ---- -10 diff --git a/freeze.go b/freeze.go index 6fe6140..1e54453 100644 --- a/freeze.go +++ b/freeze.go @@ -2,7 +2,6 @@ package freeze import ( "fmt" - "runtime" "github.com/kortschak/utter" "github.com/ptdewey/freeze/internal/diff" @@ -29,27 +28,6 @@ func Snap(t testingT, title string, values ...any) { snap(t, title, content) } -func SnapFunc(t testingT, values ...any) { - t.Helper() - pc, _, _, _ := runtime.Caller(1) - fn := runtime.FuncForPC(pc) - funcName := "unknown" - if fn != nil { - fullName := fn.Name() - parts := len(fullName) - 1 - for i := len(fullName) - 1; i >= 0; i-- { - if fullName[i] == '.' { - parts = i + 1 - break - } - } - funcName = fullName[parts:] - } - testName := t.Name() - content := formatValues(values...) - snapWithTitle(t, funcName, testName, content) -} - func snap(t testingT, title string, content string) { t.Helper() testName := t.Name() diff --git a/freeze_test.go b/freeze_test.go index 5f5701d..647ade4 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -42,22 +42,6 @@ func TestMap(t *testing.T) { }) } -func testHelperFunction() string { - return "helper result" -} - -func TestSnapFunc(t *testing.T) { - freeze.SnapFunc(t, testHelperFunction()) -} - -func TestSnapFuncAnotherHelper(t *testing.T) { - freeze.SnapFunc(t, calculateSomething(5)) -} - -func calculateSomething(n int) int { - return n * 2 -} - func TestSerializeDeserialize(t *testing.T) { snap := &freeze.Snapshot{ Title: "My Test Title", diff --git a/internal/pretty/boxes.go b/internal/pretty/boxes.go index a85d207..5050f1c 100644 --- a/internal/pretty/boxes.go +++ b/internal/pretty/boxes.go @@ -28,14 +28,16 @@ func newSnapshotBoxInternal(snap *files.Snapshot, isFuncSnapshot bool) string { snapshotFileName := files.SnapshotFileName(snap.Name) + ".snap.new" var sb strings.Builder - sb.WriteString("─── " + "New Snapshot " + strings.Repeat("─", width-15) + "\n") - sb.WriteString(fmt.Sprintf(" file: %s\n\n", Gray(snapshotFileName))) + sb.WriteString("─── " + "New Snapshot " + strings.Repeat("─", width-15) + "\n\n") if snap.Title != "" { - sb.WriteString(fmt.Sprintf(" title: %s\n", Blue("\""+snap.Title+"\""))) + sb.WriteString(fmt.Sprintf(" title: %s\n", Blue(snap.Title))) } if snap.Name != "" { - sb.WriteString(fmt.Sprintf(" test: %s\n", Blue("\""+snap.Name+"\""))) + sb.WriteString(fmt.Sprintf(" test: %s\n", Blue(snap.Name))) + } + if snapshotFileName != "" { + sb.WriteString(fmt.Sprintf(" file: %s\n", Gray(snapshotFileName))) } sb.WriteString("\n")