(t *testing.T)
| 104 | } |
| 105 | |
| 106 | func 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 | |
| 150 | func containsEnvEntry(env []string, target string) bool { |
| 151 | return slices.Contains(env, target) |
nothing calls this directly
no test coverage detected