MCPcopy Create free account
hub / github.com/bracesdev/errtrace / testGoldenContents

Function testGoldenContents

cmd/errtrace/main_test.go:98–236  ·  view source on GitHub ↗
(t *testing.T, additionalFlags []string, file string, giveSrc, wantSrc []byte)

Source from the content-addressed store, hash-verified

96}
97
98func testGoldenContents(t *testing.T, additionalFlags []string, file string, giveSrc, wantSrc []byte) {
99 wantLogs, err := extractLogs(giveSrc)
100 if err != nil {
101 t.Fatal(err)
102 }
103
104 // Copy into a temporary directory so that we can run with -w.
105 srcPath := filepath.Join(t.TempDir(), "src.go")
106 if err := os.WriteFile(srcPath, []byte(giveSrc), 0o600); err != nil {
107 t.Fatal(err)
108 }
109
110 // If the source is expected to change,
111 // also verify that running with -l lists the file.
112 // Otherwise, verify that running with -l does not list the file.
113 t.Run("list", func(t *testing.T) {
114 var out bytes.Buffer
115 exitCode := (&mainCmd{
116 Stdout: &out,
117 Stderr: testWriter{t},
118 }).Run(append(additionalFlags, "-l", srcPath))
119 if want := 0; exitCode != want {
120 t.Errorf("exit code = %d, want %d", exitCode, want)
121 }
122
123 if bytes.Equal(giveSrc, wantSrc) {
124 if want, got := "", out.String(); got != want {
125 t.Errorf("expected no output, got:\n%s", indent(got))
126 }
127 } else {
128 if want, got := srcPath+"\n", out.String(); got != want {
129 t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", indent(got), indent(want), indent(diff.Lines(want, got)))
130 }
131 }
132 })
133
134 var stdout, stderr bytes.Buffer
135 defer func() {
136 if t.Failed() {
137 t.Logf("stdout:\n%s", indent(stdout.String()))
138 t.Logf("stderr:\n%s", indent(stderr.String()))
139 }
140 }()
141
142 exitCode := (&mainCmd{
143 Stdout: &stdout, // We don't care about stdout.
144 Stderr: &stderr,
145 }).Run(append(additionalFlags, "-format=never", "-w", srcPath))
146
147 if want := 0; exitCode != want {
148 t.Errorf("exit code = %d, want %d", exitCode, want)
149 }
150
151 gotSrc, err := os.ReadFile(srcPath)
152 if err != nil {
153 t.Fatal(err)
154 }
155

Callers 1

testGoldenFileFunction · 0.85

Calls 8

LinesFunction · 0.92
DiffFunction · 0.92
extractLogsFunction · 0.85
indentFunction · 0.85
parseLogOutputFunction · 0.85
chdirFunction · 0.85
RunMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected