(t *testing.T)
| 187 | } |
| 188 | |
| 189 | func TestWriteOutput(t *testing.T) { |
| 190 | // Use platform-appropriate temp directory |
| 191 | tmpFile := filepath.Join(os.TempDir(), "test_output.sql") |
| 192 | |
| 193 | tests := []struct { |
| 194 | name string |
| 195 | content []byte |
| 196 | outputFile string |
| 197 | wantErr bool |
| 198 | cleanup func() |
| 199 | }{ |
| 200 | { |
| 201 | name: "write to stdout", |
| 202 | content: []byte("SELECT * FROM users"), |
| 203 | outputFile: "", |
| 204 | wantErr: false, |
| 205 | }, |
| 206 | { |
| 207 | name: "write to file", |
| 208 | content: []byte("SELECT * FROM users"), |
| 209 | outputFile: tmpFile, |
| 210 | wantErr: false, |
| 211 | cleanup: func() { |
| 212 | os.Remove(tmpFile) |
| 213 | }, |
| 214 | }, |
| 215 | } |
| 216 | |
| 217 | for _, tt := range tests { |
| 218 | t.Run(tt.name, func(t *testing.T) { |
| 219 | if tt.cleanup != nil { |
| 220 | defer tt.cleanup() |
| 221 | } |
| 222 | |
| 223 | var buf bytes.Buffer |
| 224 | err := WriteOutput(tt.content, tt.outputFile, &buf) |
| 225 | if (err != nil) != tt.wantErr { |
| 226 | t.Errorf("WriteOutput() error = %v, wantErr %v", err, tt.wantErr) |
| 227 | return |
| 228 | } |
| 229 | |
| 230 | // If writing to stdout, verify content |
| 231 | if tt.outputFile == "" { |
| 232 | if !bytes.Equal(buf.Bytes(), tt.content) { |
| 233 | t.Errorf("WriteOutput() stdout content mismatch") |
| 234 | } |
| 235 | } else { |
| 236 | // If writing to file, verify file exists and content |
| 237 | content, err := os.ReadFile(tt.outputFile) |
| 238 | if err != nil { |
| 239 | t.Errorf("Failed to read output file: %v", err) |
| 240 | return |
| 241 | } |
| 242 | if !bytes.Equal(content, tt.content) { |
| 243 | t.Errorf("WriteOutput() file content mismatch") |
| 244 | } |
| 245 | } |
| 246 | }) |
nothing calls this directly
no test coverage detected