MCPcopy Create free account
hub / github.com/JordanCoin/codemap / captureMainStreams

Function captureMainStreams

main_more_test.go:101–140  ·  view source on GitHub ↗
(t *testing.T, fn func())

Source from the content-addressed store, hash-verified

99}
100
101func captureMainStreams(t *testing.T, fn func()) (string, string) {
102 t.Helper()
103
104 oldOut := os.Stdout
105 oldErr := os.Stderr
106 outFile, err := os.CreateTemp("", "codemap-stdout-*")
107 if err != nil {
108 t.Fatal(err)
109 }
110 errFile, err := os.CreateTemp("", "codemap-stderr-*")
111 if err != nil {
112 t.Fatal(err)
113 }
114 defer func() {
115 _ = os.Remove(outFile.Name())
116 _ = os.Remove(errFile.Name())
117 }()
118
119 func() {
120 defer func() {
121 _ = outFile.Close()
122 _ = errFile.Close()
123 os.Stdout = oldOut
124 os.Stderr = oldErr
125 }()
126 os.Stdout = outFile
127 os.Stderr = errFile
128 fn()
129 }()
130
131 stdout, err := os.ReadFile(outFile.Name())
132 if err != nil {
133 t.Fatalf("read stdout capture: %v", err)
134 }
135 stderr, err := os.ReadFile(errFile.Name())
136 if err != nil {
137 t.Fatalf("read stderr capture: %v", err)
138 }
139 return string(stdout), string(stderr)
140}
141
142func runCodemapWithInput(input string, args ...string) (string, string, error) {
143 cmd := exec.Command(codemapTestBinaryPath, args...)

Calls 2

RemoveMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected