1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.package zipfs
16 "golang.org/x/tools/godoc/vfs"
21 // files to use to build zip used by zipfs in testing; maps path : contents
22 files = map[string]string{"foo": "foo", "bar/baz": "baz", "a/b/c": "c"}
24 // expected info for each entry in a file system described by files
33 {"/", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
34 {"//", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
35 {"/foo", false, true, "foo", "foo", nil},
36 {"/foo/", false, true, "foo", "foo", nil},
37 {"/foo//", false, true, "foo", "foo", nil},
38 {"/bar", true, false, "bar", "", map[string]bool{"baz": true}},
39 {"/bar/", true, false, "bar", "", map[string]bool{"baz": true}},
40 {"/bar/baz", false, true, "baz", "baz", nil},
41 {"//bar//baz", false, true, "baz", "baz", nil},
42 {"/a/b", true, false, "b", "", map[string]bool{"c": true}},
45 // to be initialized in setup()
50 type statFunc struct {
52 Func func(string) (os.FileInfo, error)
55 func TestMain(t *testing.M) {
56 if err := setup(); err != nil {
57 fmt.Fprintf(os.Stderr, "Error setting up zipfs testing state: %v.\n", err)
63 // setups state each of the tests uses
66 b := new(bytes.Buffer)
67 zw := zip.NewWriter(b)
68 for file, contents := range files {
69 w, err := zw.Create(file)
73 _, err = io.WriteString(w, contents)
79 zr, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
83 rc := &zip.ReadCloser{
88 // pull out different stat functions
89 statFuncs = []statFunc{
97 func TestZipFSReadDir(t *testing.T) {
98 for _, test := range tests {
100 infos, err := fs.ReadDir(test.Path)
102 t.Errorf("Failed to read directory %v\n", test.Path)
105 got := make(map[string]bool)
106 for _, info := range infos {
107 got[info.Name()] = true
109 if want := test.Files; !reflect.DeepEqual(got, want) {
110 t.Errorf("ReadDir %v got %v\nwanted %v\n", test.Path, got, want)
116 func TestZipFSStatFuncs(t *testing.T) {
117 for _, test := range tests {
118 for _, statFunc := range statFuncs {
121 info, err := statFunc.Func(test.Path)
123 t.Errorf("Unexpected error using %v for %v: %v\n", statFunc.Name, test.Path, err)
128 if got, want := info.Name(), test.Name; got != want {
129 t.Errorf("Using %v for %v info.Name() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
132 if got, want := info.IsDir(), test.IsDir; got != want {
133 t.Errorf("Using %v for %v info.IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
135 // test info.Mode().IsDir()
136 if got, want := info.Mode().IsDir(), test.IsDir; got != want {
137 t.Errorf("Using %v for %v info.Mode().IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
139 // test info.Mode().IsRegular()
140 if got, want := info.Mode().IsRegular(), test.IsRegular; got != want {
141 t.Errorf("Using %v for %v info.Mode().IsRegular() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
145 if got, want := info.Size(), int64(len(test.Contents)); got != want {
146 t.Errorf("Using %v for %v inf.Size() got %v wanted %v", statFunc.Name, test.Path, got, want)
153 func TestZipFSNotExist(t *testing.T) {
154 _, err := fs.Open("/does-not-exist")
156 t.Fatalf("Expected an error.\n")
158 if !os.IsNotExist(err) {
159 t.Errorf("Expected an error satisfying os.IsNotExist: %v\n", err)
163 func TestZipFSOpenSeek(t *testing.T) {
164 for _, test := range tests {
168 f, err := fs.Open(test.Path)
175 // test Seek() multiple times
176 for i := 0; i < 3; i++ {
177 all, err := ioutil.ReadAll(f)
182 if got, want := string(all), test.Contents; got != want {
183 t.Errorf("File contents for %v got %v wanted %v\n", test.Path, got, want)
191 func TestRootType(t *testing.T) {
196 {"/src/net/http", vfs.RootTypeGoRoot},
197 {"/src/badpath", ""},
198 {"/", vfs.RootTypeGoRoot},
201 for _, item := range tests {
202 if fs.RootType(item.path) != item.fsType {
203 t.Errorf("unexpected fsType. Expected- %v, Got- %v", item.fsType, fs.RootType(item.path))