Approval-based snapshot testing library for Go (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: misc cleanup

+80 -50
__snapshots__/test_map.accepted __snapshots__/test_map.snap
__snapshots__/test_snap_custom_type.accepted __snapshots__/test_snap_custom_type.snap
-13
__snapshots__/test_snap_multiple.accepted
··· 1 - --- 2 - version: 0.1.0 3 - test_name: TestSnapMultiple 4 - --- 5 - value1 6 - value2 7 - 42 8 - foo 9 - bar 10 - baz 11 - wibble 12 - wobble 13 - tick
__snapshots__/test_snap_multiple.new __snapshots__/test_snap_multiple.snap
__snapshots__/test_snap_string.accepted __snapshots__/test_snap_string.snap
+3 -3
freeze.go
··· 38 38 t.Helper() 39 39 40 40 snapshot := &files.Snapshot{ 41 - Version: version, 42 - TestName: title, 43 - Content: content, 41 + Version: version, 42 + Name: title, 43 + Content: content, 44 44 } 45 45 46 46 accepted, err := files.ReadAccepted(title)
+17 -17
freeze_test.go
··· 42 42 43 43 func TestSerializeDeserialize(t *testing.T) { 44 44 snap := &freeze.Snapshot{ 45 - Version: "1.0.0", 46 - TestName: "TestExample", 47 - Content: "test content\nmultiline", 45 + Version: "1.0.0", 46 + Name: "TestExample", 47 + Content: "test content\nmultiline", 48 48 } 49 49 50 50 serialized := snap.Serialize() ··· 61 61 if deserialized.Version != snap.Version { 62 62 t.Errorf("version mismatch: %s != %s", deserialized.Version, snap.Version) 63 63 } 64 - if deserialized.TestName != snap.TestName { 65 - t.Errorf("test name mismatch: %s != %s", deserialized.TestName, snap.TestName) 64 + if deserialized.Name != snap.Name { 65 + t.Errorf("test name mismatch: %s != %s", deserialized.Name, snap.Name) 66 66 } 67 67 if deserialized.Content != snap.Content { 68 68 t.Errorf("content mismatch: %s != %s", deserialized.Content, snap.Content) ··· 71 71 72 72 func TestFileOperations(t *testing.T) { 73 73 snap := &freeze.Snapshot{ 74 - Version: "0.1.0", 75 - TestName: "TestFileOps", 76 - Content: "file test content", 74 + Version: "0.1.0", 75 + Name: "TestFileOps", 76 + Content: "file test content", 77 77 } 78 78 79 79 if err := freeze.SaveSnapshot(snap, "test"); err != nil { ··· 151 151 152 152 func TestDiffSnapshotBox(t *testing.T) { 153 153 old := &freeze.Snapshot{ 154 - Version: "0.1.0", 155 - TestName: "TestDiff", 156 - Content: "old content", 154 + Version: "0.1.0", 155 + Name: "TestDiff", 156 + Content: "old content", 157 157 } 158 158 159 159 new := &freeze.Snapshot{ 160 - Version: "0.1.0", 161 - TestName: "TestDiff", 162 - Content: "new content", 160 + Version: "0.1.0", 161 + Name: "TestDiff", 162 + Content: "new content", 163 163 } 164 164 165 165 box := freeze.DiffSnapshotBox(old, new) ··· 174 174 175 175 func TestNewSnapshotBox(t *testing.T) { 176 176 snap := &freeze.Snapshot{ 177 - Version: "0.1.0", 178 - TestName: "TestNew", 179 - Content: "test content", 177 + Version: "0.1.0", 178 + Name: "TestNew", 179 + Content: "test content", 180 180 } 181 181 182 182 box := freeze.NewSnapshotBox(snap)
+29 -13
internal/files/files.go
··· 9 9 ) 10 10 11 11 type Snapshot struct { 12 - Version string 13 - TestName string 14 - Content string 12 + Version string 13 + Name string 14 + Content string 15 15 } 16 16 17 17 func (s *Snapshot) Serialize() string { 18 - header := fmt.Sprintf("---\nversion: %s\ntest_name: %s\n---\n", s.Version, s.TestName) 18 + header := fmt.Sprintf("---\nversion: %s\ntest_name: %s\n---\n", s.Version, s.Name) 19 19 return header + s.Content 20 20 } 21 21 ··· 48 48 case "version": 49 49 snap.Version = value 50 50 case "test_name": 51 - snap.TestName = value 51 + snap.Name = value 52 52 } 53 53 } 54 54 ··· 110 110 return err 111 111 } 112 112 113 - fileName := SnapshotFileName(snap.TestName) + "." + state 113 + var fileName string 114 + switch state { 115 + case "accepted": 116 + fileName = SnapshotFileName(snap.Name) + ".snap" 117 + case "new": 118 + fileName = SnapshotFileName(snap.Name) + ".snap.new" 119 + default: 120 + fileName = SnapshotFileName(snap.Name) + "." + state 121 + } 114 122 filePath := filepath.Join(snapshotDir, fileName) 115 123 116 124 return os.WriteFile(filePath, []byte(snap.Serialize()), 0644) ··· 122 130 return nil, err 123 131 } 124 132 125 - fileName := SnapshotFileName(testName) + "." + state 133 + var fileName string 134 + switch state { 135 + case "accepted": 136 + fileName = SnapshotFileName(testName) + ".snap" 137 + case "new": 138 + fileName = SnapshotFileName(testName) + ".snap.new" 139 + default: 140 + fileName = SnapshotFileName(testName) + "." + state 141 + } 126 142 filePath := filepath.Join(snapshotDir, fileName) 127 143 128 144 data, err := os.ReadFile(filePath) ··· 134 150 } 135 151 136 152 func ReadAccepted(testName string) (*Snapshot, error) { 137 - return ReadSnapshot(testName, "accepted") 153 + return ReadSnapshot(testName, "snap") 138 154 } 139 155 140 156 func ReadNew(testName string) (*Snapshot, error) { ··· 154 170 155 171 var newSnapshots []string 156 172 for _, entry := range entries { 157 - if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".new") { 158 - name := strings.TrimSuffix(entry.Name(), ".new") 173 + if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".snap.new") { 174 + name := strings.TrimSuffix(entry.Name(), ".snap.new") 159 175 newSnapshots = append(newSnapshots, name) 160 176 } 161 177 } ··· 170 186 } 171 187 172 188 fileName := SnapshotFileName(testName) 173 - newPath := filepath.Join(snapshotDir, fileName+".new") 174 - acceptedPath := filepath.Join(snapshotDir, fileName+".accepted") 189 + newPath := filepath.Join(snapshotDir, fileName+".snap.new") 190 + acceptedPath := filepath.Join(snapshotDir, fileName+".snap") 175 191 176 192 data, err := os.ReadFile(newPath) 177 193 if err != nil { ··· 191 207 return err 192 208 } 193 209 194 - fileName := SnapshotFileName(testName) + ".new" 210 + fileName := SnapshotFileName(testName) + ".snap.new" 195 211 filePath := filepath.Join(snapshotDir, fileName) 196 212 197 213 return os.Remove(filePath)
+1 -1
internal/pretty/boxes.go
··· 29 29 sb.WriteString(strings.Repeat("─", width+2) + "\n") 30 30 // TODO: "New Snapshot" should be above this line, in default color. 31 31 // - color should be on test name and path 32 - sb.WriteString(fmt.Sprintf(" %s \n", Blue("New Snapshot -- \""+snap.TestName+"\""))) 32 + sb.WriteString(fmt.Sprintf(" %s \n", Blue("New Snapshot -- \""+snap.Name+"\""))) 33 33 34 34 lines := strings.Split(snap.Content, "\n") 35 35 numLines := len(lines)
+30 -3
review.go
··· 17 17 Accept ReviewChoice = iota 18 18 Reject 19 19 Skip 20 + AcceptAllChoice 21 + RejectAllChoice 22 + SkipAllChoice 20 23 // ToggleDiff 21 24 Quit 22 25 ) ··· 97 100 } 98 101 case Skip: 99 102 fmt.Println(pretty.Warning("⊘ Snapshot skipped")) 103 + case AcceptAllChoice: 104 + for j := i; j < len(snapshots); j++ { 105 + if err := files.AcceptSnapshot(snapshots[j]); err != nil { 106 + fmt.Println(pretty.Error("✗ Failed to accept snapshot: " + err.Error())) 107 + } 108 + } 109 + fmt.Printf(pretty.Success("✓ Accepted %d snapshot(s)\n"), len(snapshots)-i) 110 + return nil 111 + case RejectAllChoice: 112 + for j := i; j < len(snapshots); j++ { 113 + if err := files.RejectSnapshot(snapshots[j]); err != nil { 114 + fmt.Println(pretty.Error("✗ Failed to reject snapshot: " + err.Error())) 115 + } 116 + } 117 + fmt.Printf(pretty.Warning("⊘ Rejected %d snapshot(s)\n"), len(snapshots)-i) 118 + return nil 119 + case SkipAllChoice: 120 + fmt.Printf(pretty.Warning("⊘ Skipped %d snapshot(s)\n"), len(snapshots)-i) 121 + return nil 100 122 // case ToggleDiff: 101 123 // showDiff = !showDiff 102 124 // if acceptErr == nil { ··· 119 141 } 120 142 121 143 func askChoice(reader *bufio.Reader, current, total int) (ReviewChoice, error) { 122 - // fmt.Printf("\nOptions: [a]ccept [r]eject [s]kip [d]iff [q]uit: ") 123 - fmt.Printf("\nOptions: [a]ccept [r]eject [s]kip [q]uit: ") 144 + fmt.Printf("\nOptions: [a]ccept [r]eject [s]kip [A]ccept All [R]eject All [S]kip All [q]uit: ") 124 145 125 146 input, err := reader.ReadString('\n') 126 147 if err != nil { 127 148 return Quit, err 128 149 } 129 150 130 - input = strings.ToLower(strings.TrimSpace(input)) 151 + input = strings.TrimSpace(input) 131 152 132 153 switch input { 133 154 case "a", "accept": ··· 136 157 return Reject, nil 137 158 case "s", "skip": 138 159 return Skip, nil 160 + case "A", "Accept All": 161 + return AcceptAllChoice, nil 162 + case "R", "Reject All": 163 + return RejectAllChoice, nil 164 + case "S", "Skip All": 165 + return SkipAllChoice, nil 139 166 // case "d", "diff": 140 167 // return ToggleDiff, nil 141 168 case "q", "quit":