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

Function captureOutputAndError

cmd/hooks_more_test.go:56–95  ·  view source on GitHub ↗
(t *testing.T, fn func())

Source from the content-addressed store, hash-verified

54}
55
56func captureOutputAndError(t *testing.T, fn func()) (string, string) {
57 t.Helper()
58
59 oldOut := os.Stdout
60 oldErr := os.Stderr
61 outFile, err := os.CreateTemp("", "codemap-cmd-stdout-*")
62 if err != nil {
63 t.Fatal(err)
64 }
65 errFile, err := os.CreateTemp("", "codemap-cmd-stderr-*")
66 if err != nil {
67 t.Fatal(err)
68 }
69 defer func() {
70 _ = os.Remove(outFile.Name())
71 _ = os.Remove(errFile.Name())
72 }()
73
74 func() {
75 defer func() {
76 _ = outFile.Close()
77 _ = errFile.Close()
78 os.Stdout = oldOut
79 os.Stderr = oldErr
80 }()
81 os.Stdout = outFile
82 os.Stderr = errFile
83 fn()
84 }()
85
86 stdout, err := os.ReadFile(outFile.Name())
87 if err != nil {
88 t.Fatalf("read stdout capture: %v", err)
89 }
90 stderr, err := os.ReadFile(errFile.Name())
91 if err != nil {
92 t.Fatalf("read stderr capture: %v", err)
93 }
94 return string(stdout), string(stderr)
95}
96
97func withStdinInput(t *testing.T, input string, fn func()) {
98 t.Helper()

Calls 2

RemoveMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected