MCPcopy Create free account
hub / github.com/github/gh-aw / TestExtractFileFromTar

Function TestExtractFileFromTar

pkg/fileutil/fileutil_test.go:583–632  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

581}
582
583func 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}

Callers

nothing calls this directly

Calls 5

ExtractFileFromTarFunction · 0.85
WriteMethod · 0.80
CloseMethod · 0.65
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected