(t *testing.T)
| 581 | } |
| 582 | |
| 583 | func TestExtractFileFromTar(t *testing.T) { |
| 584 | // Helper to build an in-memory tar archive |
| 585 | buildTar := func(files map[string][]byte) []byte { |
| 586 | var buf bytes.Buffer |
| 587 | tw := tar.NewWriter(&buf) |
| 588 | for name, content := range files { |
| 589 | hdr := &tar.Header{ |
| 590 | Name: name, |
| 591 | Mode: 0600, |
| 592 | Size: int64(len(content)), |
| 593 | } |
| 594 | if err := tw.WriteHeader(hdr); err != nil { |
| 595 | t.Fatalf("buildTar: WriteHeader: %v", err) |
| 596 | } |
| 597 | if _, err := tw.Write(content); err != nil { |
| 598 | t.Fatalf("buildTar: Write: %v", err) |
| 599 | } |
| 600 | } |
| 601 | if err := tw.Close(); err != nil { |
| 602 | t.Fatalf("buildTar: Close: %v", err) |
| 603 | } |
| 604 | return buf.Bytes() |
| 605 | } |
| 606 | |
| 607 | t.Run("found file returns its content", func(t *testing.T) { |
| 608 | want := []byte("hello from tar") |
| 609 | archive := buildTar(map[string][]byte{"subdir/file.txt": want}) |
| 610 | |
| 611 | got, err := ExtractFileFromTar(archive, "subdir/file.txt") |
| 612 | require.NoError(t, err, "ExtractFileFromTar should succeed when file is present") |
| 613 | assert.Equal(t, want, got, "Extracted content should match original") |
| 614 | }) |
| 615 | |
| 616 | t.Run("file not found returns error", func(t *testing.T) { |
| 617 | archive := buildTar(map[string][]byte{"other.txt": []byte("data")}) |
| 618 | |
| 619 | got, err := ExtractFileFromTar(archive, "missing.txt") |
| 620 | require.Error(t, err, "ExtractFileFromTar should return error when file is absent") |
| 621 | assert.Contains(t, err.Error(), "missing.txt", "Error should mention the missing filename") |
| 622 | assert.Nil(t, got, "Result should be nil when file is not found") |
| 623 | }) |
| 624 | |
| 625 | t.Run("corrupted archive returns error", func(t *testing.T) { |
| 626 | corrupted := []byte("this is not a valid tar archive") |
| 627 | |
| 628 | got, err := ExtractFileFromTar(corrupted, "any.txt") |
| 629 | require.Error(t, err, "ExtractFileFromTar should return error for corrupted archive") |
| 630 | assert.Nil(t, got, "Result should be nil for corrupted archive") |
| 631 | }) |
| 632 | } |
nothing calls this directly
no test coverage detected