File: zipfs_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.0~git20190125.d66bd3c%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 8,912 kB
  • sloc: asm: 1,394; yacc: 155; makefile: 109; sh: 108; ansic: 17; xml: 11
file content (206 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download | duplicates (3)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.package zipfs
package zipfs

import (
	"archive/zip"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"reflect"
	"testing"

	"golang.org/x/tools/godoc/vfs"
)

var (

	// files to use to build zip used by zipfs in testing; maps path : contents
	files = map[string]string{"foo": "foo", "bar/baz": "baz", "a/b/c": "c"}

	// expected info for each entry in a file system described by files
	tests = []struct {
		Path      string
		IsDir     bool
		IsRegular bool
		Name      string
		Contents  string
		Files     map[string]bool
	}{
		{"/", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
		{"//", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
		{"/foo", false, true, "foo", "foo", nil},
		{"/foo/", false, true, "foo", "foo", nil},
		{"/foo//", false, true, "foo", "foo", nil},
		{"/bar", true, false, "bar", "", map[string]bool{"baz": true}},
		{"/bar/", true, false, "bar", "", map[string]bool{"baz": true}},
		{"/bar/baz", false, true, "baz", "baz", nil},
		{"//bar//baz", false, true, "baz", "baz", nil},
		{"/a/b", true, false, "b", "", map[string]bool{"c": true}},
	}

	// to be initialized in setup()
	fs        vfs.FileSystem
	statFuncs []statFunc
)

type statFunc struct {
	Name string
	Func func(string) (os.FileInfo, error)
}

func TestMain(t *testing.M) {
	if err := setup(); err != nil {
		fmt.Fprintf(os.Stderr, "Error setting up zipfs testing state: %v.\n", err)
		os.Exit(1)
	}
	os.Exit(t.Run())
}

// setups state each of the tests uses
func setup() error {
	// create zipfs
	b := new(bytes.Buffer)
	zw := zip.NewWriter(b)
	for file, contents := range files {
		w, err := zw.Create(file)
		if err != nil {
			return err
		}
		_, err = io.WriteString(w, contents)
		if err != nil {
			return err
		}
	}
	zw.Close()
	zr, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
	if err != nil {
		return err
	}
	rc := &zip.ReadCloser{
		Reader: *zr,
	}
	fs = New(rc, "foo")

	// pull out different stat functions
	statFuncs = []statFunc{
		{"Stat", fs.Stat},
		{"Lstat", fs.Lstat},
	}

	return nil
}

func TestZipFSReadDir(t *testing.T) {
	for _, test := range tests {
		if test.IsDir {
			infos, err := fs.ReadDir(test.Path)
			if err != nil {
				t.Errorf("Failed to read directory %v\n", test.Path)
				continue
			}
			got := make(map[string]bool)
			for _, info := range infos {
				got[info.Name()] = true
			}
			if want := test.Files; !reflect.DeepEqual(got, want) {
				t.Errorf("ReadDir %v got %v\nwanted %v\n", test.Path, got, want)
			}
		}
	}
}

func TestZipFSStatFuncs(t *testing.T) {
	for _, test := range tests {
		for _, statFunc := range statFuncs {

			// test can stat
			info, err := statFunc.Func(test.Path)
			if err != nil {
				t.Errorf("Unexpected error using %v for %v: %v\n", statFunc.Name, test.Path, err)
				continue
			}

			// test info.Name()
			if got, want := info.Name(), test.Name; got != want {
				t.Errorf("Using %v for %v info.Name() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
			}
			// test info.IsDir()
			if got, want := info.IsDir(), test.IsDir; got != want {
				t.Errorf("Using %v for %v info.IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
			}
			// test info.Mode().IsDir()
			if got, want := info.Mode().IsDir(), test.IsDir; got != want {
				t.Errorf("Using %v for %v info.Mode().IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
			}
			// test info.Mode().IsRegular()
			if got, want := info.Mode().IsRegular(), test.IsRegular; got != want {
				t.Errorf("Using %v for %v info.Mode().IsRegular() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
			}
			// test info.Size()
			if test.IsRegular {
				if got, want := info.Size(), int64(len(test.Contents)); got != want {
					t.Errorf("Using %v for %v inf.Size() got %v wanted %v", statFunc.Name, test.Path, got, want)
				}
			}
		}
	}
}

func TestZipFSNotExist(t *testing.T) {
	_, err := fs.Open("/does-not-exist")
	if err == nil {
		t.Fatalf("Expected an error.\n")
	}
	if !os.IsNotExist(err) {
		t.Errorf("Expected an error satisfying os.IsNotExist: %v\n", err)
	}
}

func TestZipFSOpenSeek(t *testing.T) {
	for _, test := range tests {
		if test.IsRegular {

			// test Open()
			f, err := fs.Open(test.Path)
			if err != nil {
				t.Error(err)
				return
			}
			defer f.Close()

			// test Seek() multiple times
			for i := 0; i < 3; i++ {
				all, err := ioutil.ReadAll(f)
				if err != nil {
					t.Error(err)
					return
				}
				if got, want := string(all), test.Contents; got != want {
					t.Errorf("File contents for %v got %v wanted %v\n", test.Path, got, want)
				}
				f.Seek(0, 0)
			}
		}
	}
}

func TestRootType(t *testing.T) {
	tests := []struct {
		path   string
		fsType vfs.RootType
	}{
		{"/src/net/http", vfs.RootTypeGoRoot},
		{"/src/badpath", ""},
		{"/", vfs.RootTypeGoRoot},
	}

	for _, item := range tests {
		if fs.RootType(item.path) != item.fsType {
			t.Errorf("unexpected fsType. Expected- %v, Got- %v", item.fsType, fs.RootType(item.path))
		}
	}
}