Skip to content

Commit e217864

Browse files
authored
chore/testify golden diffs (#3766)
* test: replace go-cmp with testify/require in golden test utilities * test: drop go-cmp and difflib; use testify for golden diffs; tidy modules * testutil: remove DiffContextLines option; simplify API and docs * testutil: simplify golden helpers; remove go-cmp/difflib, Options, Batch; auto-format JSON/Go; unify update mode; update examples/docs
1 parent 9993f9e commit e217864

8 files changed

Lines changed: 131 additions & 579 deletions

File tree

codegen/testutil/README.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,6 @@ func TestWithFluentAPI(t *testing.T) {
9696
}
9797
```
9898

99-
### Custom Options
100-
101-
```go
102-
func TestWithOptions(t *testing.T) {
103-
opts := testutil.Options{
104-
BasePath: "testdata/custom", // Base directory for golden files
105-
FormatCode: true, // Format Go code before comparison
106-
CreateMissing: true, // Create golden files if missing
107-
DiffContextLines: 5, // Lines of context in diffs
108-
}
109-
110-
gf := testutil.WithOptions(t, opts)
111-
gf.StringContent(code).Path("output.golden").CompareContent()
112-
}
113-
```
114-
11599
### Directory Comparison
116100

117101
Compare entire directory structures:
@@ -133,12 +117,6 @@ func TestGeneratedDirectory(t *testing.T) {
133117
# Update golden files
134118
go test -update # or -u or -w
135119

136-
# Show detailed diffs
137-
go test -golden.diff
138-
139-
# Disable colored output
140-
go test -golden.color=false
141-
142120
# Sequential updates (for debugging)
143121
go test -update -golden.parallel=false
144122
```
@@ -179,9 +157,9 @@ mypackage/
179157

180158
The package automatically detects and formats content based on file extensions:
181159

182-
- `.go` files: Formatted with `go/format`
183-
- `.json` files: Pretty-printed with proper indentation
184-
- `.golden` files: Format detected from full filename (e.g., `server.go.golden` → Go)
160+
- `.go` or `.go.golden`: Formatted with `go/format`
161+
- `.json` or `.json.golden`: Pretty-printed with proper indentation
162+
- Other extensions: Treated as plain text
185163
- Other extensions: Treated as plain text
186164

187165
## Notes

codegen/testutil/example_test.go

Lines changed: 16 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -21,89 +21,19 @@ func main() {
2121
testutil.AssertString(t, "testdata/golden/hello_world.go.golden", code)
2222
}
2323

24-
// Example: Using the fluent API
25-
func TestFluentAPI(t *testing.T) {
26-
gf := testutil.NewGoldenFile(t, "testdata/golden")
27-
28-
// Generate code
29-
code := generateServiceCode()
30-
31-
// Use fluent API for comparison
32-
gf.StringContent(code).
33-
Path("service.go.golden").
34-
CompareContent()
35-
}
36-
37-
// Example: Testing multiple files with batch operations
38-
func TestBatchOperations(t *testing.T) {
39-
batch := testutil.NewBatch(t)
40-
41-
// Generate multiple files
42-
serverCode := generateServerCode()
43-
clientCode := generateClientCode()
44-
typesCode := generateTypesCode()
45-
46-
// Add all files to batch and compare
47-
batch.
48-
AddString("server.go.golden", serverCode).
49-
AddString("client.go.golden", clientCode).
50-
AddString("types.go.golden", typesCode).
51-
Compare()
52-
}
53-
54-
// Example: Custom options for specific needs
55-
func TestCustomOptions(t *testing.T) {
56-
opts := testutil.Options{
57-
BasePath: "testdata/custom",
58-
ContentType: testutil.ContentTypeGo,
59-
FormatCode: true,
60-
NormalizeWhitespace: true,
61-
CreateMissing: true, // Create golden files if they don't exist
62-
DiffContextLines: 5, // Show 5 lines of context in diffs
63-
}
64-
65-
gf := testutil.WithOptions(t, opts)
66-
67-
code := generateComplexCode()
68-
69-
gf.StringContent(code).
70-
Path("complex.go.golden").
71-
CompareContent()
72-
}
73-
7424
// Example: Format-aware comparisons
7525
func TestFormatAwareComparisons(t *testing.T) {
76-
// Test Go code - automatically formatted
26+
// Test Go code - automatically formatted (auto-detected by .go.golden)
7727
goCode := `package main
7828
import "fmt"
7929
func main(){fmt.Println("unformatted")}`
80-
30+
8131
testutil.AssertGo(t, "testdata/golden/formatted.go.golden", goCode)
8232

83-
// Test JSON - automatically pretty-printed
33+
// Test JSON - automatically pretty-printed (auto-detected by .json.golden)
8434
jsonData := []byte(`{"name":"test","value":42,"items":["a","b","c"]}`)
85-
86-
testutil.AssertJSON(t, "testdata/golden/config.json.golden", jsonData)
87-
}
8835

89-
90-
// Example: Legacy migration
91-
func TestLegacyMigration(t *testing.T) {
92-
code := generateLegacyCode()
93-
94-
// This is a drop-in replacement for the old compareOrUpdateGolden function
95-
testutil.CompareOrUpdateGolden(t, code, "testdata/golden/legacy.golden")
96-
}
97-
98-
// Example: Conditional golden file creation
99-
func TestConditionalCreation(t *testing.T) {
100-
code := generateOptionalFeature()
101-
102-
// Only create/compare if feature is enabled
103-
if code != "" {
104-
gf := testutil.NewGoldenFile(t, "testdata/golden")
105-
gf.CompareOrCreate(code, "optional_feature.golden")
106-
}
36+
testutil.AssertJSON(t, "testdata/golden/config.json.golden", jsonData)
10737
}
10838

10939
// Example: Testing with subtests
@@ -143,15 +73,15 @@ func TestWithSubtests(t *testing.T) {
14373
// Example: Parallel golden file updates
14474
func TestParallelUpdates(t *testing.T) {
14575
// When running with -update, files are updated in parallel by default
146-
files := map[string]string{
147-
"parallel1.golden": generateParallel1(),
148-
"parallel2.golden": generateParallel2(),
149-
"parallel3.golden": generateParallel3(),
150-
"parallel4.golden": generateParallel4(),
76+
files := map[string]string{}
77+
for i := 1; i <= 4; i++ {
78+
files[fmt.Sprintf("parallel%d.golden", i)] = generateParallel(i)
15179
}
15280

153-
gf := testutil.NewGoldenFile(t, "testdata/golden")
154-
gf.CompareMultiple(files)
81+
for golden, actual := range files {
82+
gf := testutil.NewGoldenFile(t, "testdata/golden")
83+
gf.StringContent(actual).Path(golden).CompareContent()
84+
}
15585
}
15686

15787
// Helper functions for examples
@@ -178,45 +108,7 @@ func (s *serviceImpl) DoSomething(ctx context.Context) error {
178108
`
179109
}
180110

181-
func generateServerCode() string {
182-
return `package main
183-
184-
import (
185-
"log"
186-
"net/http"
187-
)
188-
189-
func main() {
190-
http.HandleFunc("/", handler)
191-
log.Fatal(http.ListenAndServe(":8080", nil))
192-
}
193-
194-
func handler(w http.ResponseWriter, r *http.Request) {
195-
w.Write([]byte("Hello, Server!"))
196-
}
197-
`
198-
}
199-
200-
func generateClientCode() string {
201-
return `package client
202-
203-
import (
204-
"net/http"
205-
)
206-
207-
type Client struct {
208-
baseURL string
209-
http *http.Client
210-
}
211-
212-
func New(baseURL string) *Client {
213-
return &Client{
214-
baseURL: baseURL,
215-
http: &http.Client{},
216-
}
217-
}
218-
`
219-
}
111+
// removed verbose server/client helpers to keep file concise
220112

221113
func generateTypesCode() string {
222114
return `package types
@@ -242,18 +134,7 @@ func generateComplexCode() string {
242134
return generateServiceCode() + "\n" + generateTypesCode()
243135
}
244136

245-
246-
func generateLegacyCode() string {
247-
return "// Legacy code example\n" + generateSimpleCode()
248-
}
249-
250-
func generateOptionalFeature() string {
251-
// Simulate optional feature generation
252-
if testing.Short() {
253-
return ""
254-
}
255-
return "// Optional feature code\n"
256-
}
137+
// removed legacy code generator
257138

258139
func generateSimpleCode() string {
259140
return `package simple
@@ -277,7 +158,6 @@ func Find(id string) error {
277158
`
278159
}
279160

280-
func generateParallel1() string { return fmt.Sprintf("// Parallel 1\n%s", generateSimpleCode()) }
281-
func generateParallel2() string { return fmt.Sprintf("// Parallel 2\n%s", generateSimpleCode()) }
282-
func generateParallel3() string { return fmt.Sprintf("// Parallel 3\n%s", generateSimpleCode()) }
283-
func generateParallel4() string { return fmt.Sprintf("// Parallel 4\n%s", generateSimpleCode()) }
161+
func generateParallel(i int) string {
162+
return fmt.Sprintf("// Parallel %d\n%s", i, generateSimpleCode())
163+
}

0 commit comments

Comments
 (0)