|
4 | 4 | "bytes" |
5 | 5 | "context" |
6 | 6 | "encoding/json" |
| 7 | + "errors" |
7 | 8 | "io" |
8 | 9 | "net/http" |
9 | 10 | "net/http/httptest" |
@@ -413,19 +414,56 @@ func TestRunDocsTabExport_JSONOutput(t *testing.T) { |
413 | 414 | } |
414 | 415 | } |
415 | 416 |
|
| 417 | +func TestRunDocsTabExport_RequiresOverwrite(t *testing.T) { |
| 418 | + ctx, _ := newTabExportTestContext(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 419 | + w.Header().Set("Content-Type", "application/pdf") |
| 420 | + _, _ = w.Write([]byte("replacement")) |
| 421 | + }), false) |
| 422 | + |
| 423 | + outPath := filepath.Join(t.TempDir(), "output.pdf") |
| 424 | + if err := os.WriteFile(outPath, []byte("original"), 0o600); err != nil { |
| 425 | + t.Fatalf("WriteFile: %v", err) |
| 426 | + } |
| 427 | + params := tabExportParams{ |
| 428 | + DocID: "doc1", |
| 429 | + OutFlag: outPath, |
| 430 | + Format: "pdf", |
| 431 | + TabQuery: "First Tab", |
| 432 | + } |
| 433 | + |
| 434 | + if err := runDocsTabExport( ctx, &RootFlags{ Account: "[email protected]"}, params); !errors. Is( err, os. ErrExist) { |
| 435 | + t.Fatalf("expected existing-file error, got %v", err) |
| 436 | + } |
| 437 | + if got, err := os.ReadFile(outPath); err != nil || string(got) != "original" { |
| 438 | + t.Fatalf("existing file changed: data=%q err=%v", got, err) |
| 439 | + } |
| 440 | + |
| 441 | + params.Overwrite = true |
| 442 | + if err := runDocsTabExport( ctx, &RootFlags{ Account: "[email protected]"}, params); err != nil { |
| 443 | + t.Fatalf("runDocsTabExport overwrite: %v", err) |
| 444 | + } |
| 445 | + if got, err := os.ReadFile(outPath); err != nil || string(got) != "replacement" { |
| 446 | + t.Fatalf("overwrite failed: data=%q err=%v", got, err) |
| 447 | + } |
| 448 | +} |
| 449 | + |
416 | 450 | func TestDocsExportCmd_TabRouting(t *testing.T) { |
417 | 451 | ctx, _ := newTabExportTestContext(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
418 | 452 | w.Header().Set("Content-Type", "application/pdf") |
419 | 453 | _, _ = w.Write([]byte("tab pdf")) |
420 | 454 | }), false) |
421 | 455 |
|
422 | 456 | outPath := filepath.Join(t.TempDir(), "out.pdf") |
| 457 | + if err := os.WriteFile(outPath, []byte("original"), 0o600); err != nil { |
| 458 | + t.Fatalf("WriteFile: %v", err) |
| 459 | + } |
423 | 460 |
|
424 | 461 | cmd := &DocsExportCmd{ |
425 | | - DocID: "doc1", |
426 | | - Format: "pdf", |
427 | | - Tab: "Second Tab", |
428 | | - Output: OutputPathFlag{Path: outPath}, |
| 462 | + DocID: "doc1", |
| 463 | + Format: "pdf", |
| 464 | + Tab: "Second Tab", |
| 465 | + Output: OutputPathFlag{Path: outPath}, |
| 466 | + Overwrite: true, |
429 | 467 | } |
430 | 468 |
|
431 | 469 | if err := cmd. Run( ctx, &RootFlags{ Account: "[email protected]"}); err != nil { |
|
0 commit comments