MCPcopy Create free account
hub / github.com/compozy/agh / TestAttachCommandLogRedactsRecentError

Function TestAttachCommandLogRedactsRecentError

internal/procutil/env_test.go:106–148  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

104}
105
106func TestAttachCommandLogRedactsRecentError(t *testing.T) {
107 t.Run("Should redact token shaped stderr before wrapping", func(t *testing.T) {
108 logPath := filepath.Join(t.TempDir(), "command.log")
109 if err := os.WriteFile(logPath, []byte("info\nerror: token=super-secret\n"), 0o600); err != nil {
110 t.Fatalf("os.WriteFile(%q) error = %v", logPath, err)
111 }
112
113 err := attachCommandLog(errors.New("launch failed"), logPath, 0)
114 if err == nil {
115 t.Fatal("attachCommandLog() error = nil, want wrapped error")
116 }
117 if strings.Contains(err.Error(), "super-secret") {
118 t.Fatalf("attachCommandLog() = %v, want redacted secret", err)
119 }
120 if !strings.Contains(err.Error(), "token=[REDACTED]") {
121 t.Fatalf("attachCommandLog() = %v, want redacted token marker", err)
122 }
123 })
124
125 t.Run("Should read only a bounded tail from large logs", func(t *testing.T) {
126 logPath := filepath.Join(t.TempDir(), "command.log")
127 oldPrefix := "old prefix that must not be read\n"
128 recentError := "error: recent failure\n"
129 body := oldPrefix + strings.Repeat("noisy log line\n", maxDetachedCommandErrorBytes*4) + recentError
130 if err := os.WriteFile(logPath, []byte(body), 0o600); err != nil {
131 t.Fatalf("os.WriteFile(%q) error = %v", logPath, err)
132 }
133
134 text, err := readCommandLog(logPath, 0)
135 if err != nil {
136 t.Fatalf("readCommandLog() error = %v", err)
137 }
138 if len(text) > maxDetachedCommandErrorBytes*8 {
139 t.Fatalf("len(readCommandLog()) = %d, want bounded tail", len(text))
140 }
141 if strings.Contains(text, oldPrefix) {
142 t.Fatalf("readCommandLog() included old prefix in bounded tail")
143 }
144 if !strings.Contains(text, strings.TrimSpace(recentError)) {
145 t.Fatalf("readCommandLog() = %q, want recent error", text)
146 }
147 })
148}
149
150func containsEnvEntry(env []string, target string) bool {
151 return slices.Contains(env, target)

Callers

nothing calls this directly

Calls 6

attachCommandLogFunction · 0.85
readCommandLogFunction · 0.85
ContainsMethod · 0.80
RunMethod · 0.65
WriteFileMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected