From fc23614c182668e7608ff6eb7d41b6440388bb29 Mon Sep 17 00:00:00 2001 From: Patrick Dewey <57921252+ptdewey@users.noreply.github.com> Date: Mon, 17 Nov 2025 23:35:06 +0000 Subject: [PATCH] refactor: misc cleanup --- freeze.go | 6 +++--- freeze_test.go | 34 +++++++++++++++++----------------- review.go | 33 ++++++++++++++++++++++++++++++--- __snapshots__/test_map.accepted | 5 ----- __snapshots__/test_map.snap | 5 +++++ __snapshots__/test_snap_custom_type.accepted | 5 ----- __snapshots__/test_snap_custom_type.snap | 5 +++++ __snapshots__/test_snap_multiple.accepted | 13 ------------- __snapshots__/test_snap_multiple.new | 13 ------------- __snapshots__/test_snap_multiple.snap | 13 +++++++++++++ __snapshots__/test_snap_string.accepted | 5 ----- __snapshots__/test_snap_string.snap | 5 +++++ internal/files/files.go | 42 +++++++++++++++++++++++++++++------------- internal/pretty/boxes.go | 2 +- 14 file(s) changed, 108 insertion(s)(+), 78 deletion(s)(-) diff --git a/freeze.go b/freeze.go --- a/freeze.go +++ b/freeze.go @@ -38,9 +38,9 @@ t.Helper() snapshot := &files.Snapshot{ - Version: version, - TestName: title, - Content: content, + Version: version, + Name: title, + Content: content, } accepted, err := files.ReadAccepted(title) diff --git a/freeze_test.go b/freeze_test.go --- a/freeze_test.go +++ b/freeze_test.go @@ -42,9 +42,9 @@ func TestSerializeDeserialize(t *testing.T) { snap := &freeze.Snapshot{ - Version: "1.0.0", - TestName: "TestExample", - Content: "test content\nmultiline", + Version: "1.0.0", + Name: "TestExample", + Content: "test content\nmultiline", } serialized := snap.Serialize() @@ -61,8 +61,8 @@ if deserialized.Version != snap.Version { t.Errorf("version mismatch: %s != %s", deserialized.Version, snap.Version) } - if deserialized.TestName != snap.TestName { - t.Errorf("test name mismatch: %s != %s", deserialized.TestName, snap.TestName) + if deserialized.Name != snap.Name { + t.Errorf("test name mismatch: %s != %s", deserialized.Name, snap.Name) } if deserialized.Content != snap.Content { t.Errorf("content mismatch: %s != %s", deserialized.Content, snap.Content) @@ -71,9 +71,9 @@ func TestFileOperations(t *testing.T) { snap := &freeze.Snapshot{ - Version: "0.1.0", - TestName: "TestFileOps", - Content: "file test content", + Version: "0.1.0", + Name: "TestFileOps", + Content: "file test content", } if err := freeze.SaveSnapshot(snap, "test"); err != nil { @@ -151,15 +151,15 @@ func TestDiffSnapshotBox(t *testing.T) { old := &freeze.Snapshot{ - Version: "0.1.0", - TestName: "TestDiff", - Content: "old content", + Version: "0.1.0", + Name: "TestDiff", + Content: "old content", } new := &freeze.Snapshot{ - Version: "0.1.0", - TestName: "TestDiff", - Content: "new content", + Version: "0.1.0", + Name: "TestDiff", + Content: "new content", } box := freeze.DiffSnapshotBox(old, new) @@ -174,9 +174,9 @@ func TestNewSnapshotBox(t *testing.T) { snap := &freeze.Snapshot{ - Version: "0.1.0", - TestName: "TestNew", - Content: "test content", + Version: "0.1.0", + Name: "TestNew", + Content: "test content", } box := freeze.NewSnapshotBox(snap) diff --git a/review.go b/review.go --- a/review.go +++ b/review.go @@ -17,6 +17,9 @@ Accept ReviewChoice = iota Reject Skip + AcceptAllChoice + RejectAllChoice + SkipAllChoice // ToggleDiff Quit ) @@ -97,6 +100,25 @@ } case Skip: fmt.Println(pretty.Warning("⊘ Snapshot skipped")) + case AcceptAllChoice: + for j := i; j < len(snapshots); j++ { + if err := files.AcceptSnapshot(snapshots[j]); err != nil { + fmt.Println(pretty.Error("✗ Failed to accept snapshot: " + err.Error())) + } + } + fmt.Printf(pretty.Success("✓ Accepted %d snapshot(s)\n"), len(snapshots)-i) + return nil + case RejectAllChoice: + for j := i; j < len(snapshots); j++ { + if err := files.RejectSnapshot(snapshots[j]); err != nil { + fmt.Println(pretty.Error("✗ Failed to reject snapshot: " + err.Error())) + } + } + fmt.Printf(pretty.Warning("⊘ Rejected %d snapshot(s)\n"), len(snapshots)-i) + return nil + case SkipAllChoice: + fmt.Printf(pretty.Warning("⊘ Skipped %d snapshot(s)\n"), len(snapshots)-i) + return nil // case ToggleDiff: // showDiff = !showDiff // if acceptErr == nil { @@ -119,15 +141,14 @@ } func askChoice(reader *bufio.Reader, current, total int) (ReviewChoice, error) { - // fmt.Printf("\nOptions: [a]ccept [r]eject [s]kip [d]iff [q]uit: ") - fmt.Printf("\nOptions: [a]ccept [r]eject [s]kip [q]uit: ") + fmt.Printf("\nOptions: [a]ccept [r]eject [s]kip [A]ccept All [R]eject All [S]kip All [q]uit: ") input, err := reader.ReadString('\n') if err != nil { return Quit, err } - input = strings.ToLower(strings.TrimSpace(input)) + input = strings.TrimSpace(input) switch input { case "a", "accept": @@ -136,6 +157,12 @@ return Reject, nil case "s", "skip": return Skip, nil + case "A", "Accept All": + return AcceptAllChoice, nil + case "R", "Reject All": + return RejectAllChoice, nil + case "S", "Skip All": + return SkipAllChoice, nil // case "d", "diff": // return ToggleDiff, nil case "q", "quit": diff --git a/__snapshots__/test_map.accepted b/__snapshots__/test_map.accepted deleted file mode 100644 --- a/__snapshots__/test_map.accepted +++ /dev/null @@ -1,5 +0,0 @@ ---- -version: 0.1.0 -test_name: TestMap ---- -map[string]interface {}{"foo":"bar"} \ No newline at end of file diff --git a/__snapshots__/test_map.snap b/__snapshots__/test_map.snap new file mode 100644 --- /dev/null +++ b/__snapshots__/test_map.snap @@ -0,0 +1,5 @@ +--- +version: 0.1.0 +test_name: TestMap +--- +map[string]interface {}{"foo":"bar"} \ No newline at end of file diff --git a/__snapshots__/test_snap_custom_type.accepted b/__snapshots__/test_snap_custom_type.accepted deleted file mode 100644 --- a/__snapshots__/test_snap_custom_type.accepted +++ /dev/null @@ -1,5 +0,0 @@ ---- -version: 0.1.0 -test_name: TestSnapCustomType ---- -CustomStruct{Name: Alice, Age: } \ No newline at end of file diff --git a/__snapshots__/test_snap_custom_type.snap b/__snapshots__/test_snap_custom_type.snap new file mode 100644 --- /dev/null +++ b/__snapshots__/test_snap_custom_type.snap @@ -0,0 +1,5 @@ +--- +version: 0.1.0 +test_name: TestSnapCustomType +--- +CustomStruct{Name: Alice, Age: } \ No newline at end of file diff --git a/__snapshots__/test_snap_multiple.accepted b/__snapshots__/test_snap_multiple.accepted deleted file mode 100644 --- a/__snapshots__/test_snap_multiple.accepted +++ /dev/null @@ -1,13 +0,0 @@ ---- -version: 0.1.0 -test_name: TestSnapMultiple ---- -value1 -value2 -42 -foo -bar -baz -wibble -wobble -tick \ No newline at end of file diff --git a/__snapshots__/test_snap_multiple.new b/__snapshots__/test_snap_multiple.new deleted file mode 100644 --- a/__snapshots__/test_snap_multiple.new +++ /dev/null @@ -1,13 +0,0 @@ ---- -version: 0.1.0 -test_name: TestSnapMultiple ---- -value1 -value2 -42 -foo -bar -baz -wibble -wobble -tock \ No newline at end of file diff --git a/__snapshots__/test_snap_multiple.snap b/__snapshots__/test_snap_multiple.snap new file mode 100644 --- /dev/null +++ b/__snapshots__/test_snap_multiple.snap @@ -0,0 +1,13 @@ +--- +version: 0.1.0 +test_name: TestSnapMultiple +--- +value1 +value2 +42 +foo +bar +baz +wibble +wobble +tock \ No newline at end of file diff --git a/__snapshots__/test_snap_string.accepted b/__snapshots__/test_snap_string.accepted deleted file mode 100644 --- a/__snapshots__/test_snap_string.accepted +++ /dev/null @@ -1,5 +0,0 @@ ---- -version: 0.1.0 -test_name: TestSnapString ---- -hello world \ No newline at end of file diff --git a/__snapshots__/test_snap_string.snap b/__snapshots__/test_snap_string.snap new file mode 100644 --- /dev/null +++ b/__snapshots__/test_snap_string.snap @@ -0,0 +1,5 @@ +--- +version: 0.1.0 +test_name: TestSnapString +--- +hello world \ No newline at end of file diff --git a/internal/files/files.go b/internal/files/files.go --- a/internal/files/files.go +++ b/internal/files/files.go @@ -9,13 +9,13 @@ ) type Snapshot struct { - Version string - TestName string - Content string + Version string + Name string + Content string } func (s *Snapshot) Serialize() string { - header := fmt.Sprintf("---\nversion: %s\ntest_name: %s\n---\n", s.Version, s.TestName) + header := fmt.Sprintf("---\nversion: %s\ntest_name: %s\n---\n", s.Version, s.Name) return header + s.Content } @@ -48,7 +48,7 @@ case "version": snap.Version = value case "test_name": - snap.TestName = value + snap.Name = value } } @@ -110,7 +110,15 @@ return err } - fileName := SnapshotFileName(snap.TestName) + "." + state + var fileName string + switch state { + case "accepted": + fileName = SnapshotFileName(snap.Name) + ".snap" + case "new": + fileName = SnapshotFileName(snap.Name) + ".snap.new" + default: + fileName = SnapshotFileName(snap.Name) + "." + state + } filePath := filepath.Join(snapshotDir, fileName) return os.WriteFile(filePath, []byte(snap.Serialize()), 0644) @@ -122,7 +130,15 @@ return nil, err } - fileName := SnapshotFileName(testName) + "." + state + var fileName string + switch state { + case "accepted": + fileName = SnapshotFileName(testName) + ".snap" + case "new": + fileName = SnapshotFileName(testName) + ".snap.new" + default: + fileName = SnapshotFileName(testName) + "." + state + } filePath := filepath.Join(snapshotDir, fileName) data, err := os.ReadFile(filePath) @@ -134,7 +150,7 @@ } func ReadAccepted(testName string) (*Snapshot, error) { - return ReadSnapshot(testName, "accepted") + return ReadSnapshot(testName, "snap") } func ReadNew(testName string) (*Snapshot, error) { @@ -154,8 +170,8 @@ var newSnapshots []string for _, entry := range entries { - if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".new") { - name := strings.TrimSuffix(entry.Name(), ".new") + if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".snap.new") { + name := strings.TrimSuffix(entry.Name(), ".snap.new") newSnapshots = append(newSnapshots, name) } } @@ -170,8 +186,8 @@ } fileName := SnapshotFileName(testName) - newPath := filepath.Join(snapshotDir, fileName+".new") - acceptedPath := filepath.Join(snapshotDir, fileName+".accepted") + newPath := filepath.Join(snapshotDir, fileName+".snap.new") + acceptedPath := filepath.Join(snapshotDir, fileName+".snap") data, err := os.ReadFile(newPath) if err != nil { @@ -191,7 +207,7 @@ return err } - fileName := SnapshotFileName(testName) + ".new" + fileName := SnapshotFileName(testName) + ".snap.new" filePath := filepath.Join(snapshotDir, fileName) return os.Remove(filePath) diff --git a/internal/pretty/boxes.go b/internal/pretty/boxes.go --- a/internal/pretty/boxes.go +++ b/internal/pretty/boxes.go @@ -29,7 +29,7 @@ sb.WriteString(strings.Repeat("─", width+2) + "\n") // TODO: "New Snapshot" should be above this line, in default color. // - color should be on test name and path - sb.WriteString(fmt.Sprintf(" %s \n", Blue("New Snapshot -- \""+snap.TestName+"\""))) + sb.WriteString(fmt.Sprintf(" %s \n", Blue("New Snapshot -- \""+snap.Name+"\""))) lines := strings.Split(snap.Content, "\n") numLines := len(lines) -- tangled.sh