-
Notifications
You must be signed in to change notification settings - Fork 1
/
targz_test.go
89 lines (70 loc) · 1.35 KB
/
targz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"context"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/jonjohnsonjr/targz/gsip"
"github.com/jonjohnsonjr/targz/ranger"
"github.com/jonjohnsonjr/targz/tarfs"
)
func TestTargz(t *testing.T) {
f, err := os.Open("./tarfs/testdata/gsip.tar")
if err != nil {
t.Fatal(err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
t.Fatal(err)
}
size := info.Size()
s := httptest.NewServer(http.FileServerFS(os.DirFS("./tarfs/testdata")))
defer s.Close()
uri := s.URL + "/gsip.tar.gz"
t.Logf("uri: %q", uri)
ra := ranger.New(context.Background(), uri, s.Client().Transport)
zr, err := gsip.NewReader(ra, size)
if err != nil {
t.Fatal(err)
}
hfs, err := tarfs.New(zr, size)
if err != nil {
t.Fatal(err)
}
ffs, err := tarfs.New(f, size)
if err != nil {
t.Fatal(err)
}
if err := fs.WalkDir(ffs, ".", func(p string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
want, err := ffs.Open(p)
if err != nil {
t.Fatal(err)
}
got, err := hfs.Open(p)
if err != nil {
t.Fatal(err)
}
b1, err := io.ReadAll(want)
if err != nil {
t.Fatal(err)
}
b2, err := io.ReadAll(got)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b1, b2) {
t.Errorf("mismatched contents: %q", p)
}
return nil
}); err != nil {
t.Fatal(err)
}
}