MCPcopy Create free account
hub / github.com/goadesign/goa / TestGoldenFile

Function TestGoldenFile

codegen/testutil/golden_test.go:14–118  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestGoldenFile(t *testing.T) {
15 // Create a temporary directory for test golden files
16 tmpDir := t.TempDir()
17
18 t.Run("Compare", func(t *testing.T) {
19 // Create a test golden file
20 goldenPath := filepath.Join(tmpDir, "test.golden")
21 expectedContent := "package main\n\nfunc main() {\n\t// Test content\n}\n"
22 require.NoError(t, os.WriteFile(goldenPath, []byte(expectedContent), 0644))
23
24 gf := testutil.NewGoldenFile(t, tmpDir)
25
26 // Test successful comparison
27 gf.Compare(expectedContent, "test.golden")
28
29 // Test failed comparison would normally fail the test
30 // We can't easily test this without a full mock of testing.TB
31 })
32
33 t.Run("Update", func(t *testing.T) {
34 gf := testutil.NewGoldenFile(t, tmpDir)
35 gf.SetUpdateMode(true)
36
37 newContent := "updated content"
38 goldenFile := "update_test.golden"
39
40 // Update should create the file
41 gf.Compare(newContent, goldenFile)
42
43 // Verify file was created with correct content
44 goldenPath := filepath.Join(tmpDir, goldenFile)
45 actual, err := os.ReadFile(goldenPath)
46 require.NoError(t, err)
47 assert.Equal(t, newContent+"\n", string(actual))
48 })
49
50 t.Run("CompareOrCreate", func(t *testing.T) {
51 gf := testutil.NewGoldenFile(t, tmpDir)
52 // Test creating new file (using update override)
53 newFile := "new_file.golden"
54 content := "new file content"
55 gf.SetUpdateMode(true)
56 gf.Compare(content, newFile)
57 // Verify file was created
58 assert.True(t, gf.Exists(newFile))
59 // Now compare without update
60 gf.SetUpdateMode(false)
61 gf.Compare(content, newFile)
62
63 // Test comparing with different content would fail the test
64 // We verify the file was created correctly above
65 })
66
67 t.Run("CompareMultiple", func(t *testing.T) {
68 gf := testutil.NewGoldenFile(t, tmpDir)
69 gf.SetUpdateMode(true)
70
71 pairs := map[string]string{

Callers

nothing calls this directly

Calls 5

CompareMethod · 0.95
SetUpdateModeMethod · 0.95
ExistsMethod · 0.95
NewGoldenFileFunction · 0.92
RunMethod · 0.45

Tested by

no test coverage detected