golang.org/x/tools@v0.21.0/godoc/vfs/zipfs/zipfs_test.go (about)

     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
     4  package zipfs
     5  
     6  import (
     7  	"archive/zip"
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"golang.org/x/tools/godoc/vfs"
    16  )
    17  
    18  var (
    19  
    20  	// files to use to build zip used by zipfs in testing; maps path : contents
    21  	files = map[string]string{"foo": "foo", "bar/baz": "baz", "a/b/c": "c"}
    22  
    23  	// expected info for each entry in a file system described by files
    24  	tests = []struct {
    25  		Path      string
    26  		IsDir     bool
    27  		IsRegular bool
    28  		Name      string
    29  		Contents  string
    30  		Files     map[string]bool
    31  	}{
    32  		{"/", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
    33  		{"//", true, false, "", "", map[string]bool{"foo": true, "bar": true, "a": true}},
    34  		{"/foo", false, true, "foo", "foo", nil},
    35  		{"/foo/", false, true, "foo", "foo", nil},
    36  		{"/foo//", false, true, "foo", "foo", nil},
    37  		{"/bar", true, false, "bar", "", map[string]bool{"baz": true}},
    38  		{"/bar/", true, false, "bar", "", map[string]bool{"baz": true}},
    39  		{"/bar/baz", false, true, "baz", "baz", nil},
    40  		{"//bar//baz", false, true, "baz", "baz", nil},
    41  		{"/a/b", true, false, "b", "", map[string]bool{"c": true}},
    42  	}
    43  
    44  	// to be initialized in setup()
    45  	fs        vfs.FileSystem
    46  	statFuncs []statFunc
    47  )
    48  
    49  type statFunc struct {
    50  	Name string
    51  	Func func(string) (os.FileInfo, error)
    52  }
    53  
    54  func TestMain(t *testing.M) {
    55  	if err := setup(); err != nil {
    56  		fmt.Fprintf(os.Stderr, "Error setting up zipfs testing state: %v.\n", err)
    57  		os.Exit(1)
    58  	}
    59  	os.Exit(t.Run())
    60  }
    61  
    62  // setups state each of the tests uses
    63  func setup() error {
    64  	// create zipfs
    65  	b := new(bytes.Buffer)
    66  	zw := zip.NewWriter(b)
    67  	for file, contents := range files {
    68  		w, err := zw.Create(file)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		_, err = io.WriteString(w, contents)
    73  		if err != nil {
    74  			return err
    75  		}
    76  	}
    77  	zw.Close()
    78  	zr, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
    79  	if err != nil {
    80  		return err
    81  	}
    82  	rc := &zip.ReadCloser{
    83  		Reader: *zr,
    84  	}
    85  	fs = New(rc, "foo")
    86  
    87  	// pull out different stat functions
    88  	statFuncs = []statFunc{
    89  		{"Stat", fs.Stat},
    90  		{"Lstat", fs.Lstat},
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  func TestZipFSReadDir(t *testing.T) {
    97  	for _, test := range tests {
    98  		if test.IsDir {
    99  			infos, err := fs.ReadDir(test.Path)
   100  			if err != nil {
   101  				t.Errorf("Failed to read directory %v\n", test.Path)
   102  				continue
   103  			}
   104  			got := make(map[string]bool)
   105  			for _, info := range infos {
   106  				got[info.Name()] = true
   107  			}
   108  			if want := test.Files; !reflect.DeepEqual(got, want) {
   109  				t.Errorf("ReadDir %v got %v\nwanted %v\n", test.Path, got, want)
   110  			}
   111  		}
   112  	}
   113  }
   114  
   115  func TestZipFSStatFuncs(t *testing.T) {
   116  	for _, test := range tests {
   117  		for _, statFunc := range statFuncs {
   118  
   119  			// test can stat
   120  			info, err := statFunc.Func(test.Path)
   121  			if err != nil {
   122  				t.Errorf("Unexpected error using %v for %v: %v\n", statFunc.Name, test.Path, err)
   123  				continue
   124  			}
   125  
   126  			// test info.Name()
   127  			if got, want := info.Name(), test.Name; got != want {
   128  				t.Errorf("Using %v for %v info.Name() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
   129  			}
   130  			// test info.IsDir()
   131  			if got, want := info.IsDir(), test.IsDir; got != want {
   132  				t.Errorf("Using %v for %v info.IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
   133  			}
   134  			// test info.Mode().IsDir()
   135  			if got, want := info.Mode().IsDir(), test.IsDir; got != want {
   136  				t.Errorf("Using %v for %v info.Mode().IsDir() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
   137  			}
   138  			// test info.Mode().IsRegular()
   139  			if got, want := info.Mode().IsRegular(), test.IsRegular; got != want {
   140  				t.Errorf("Using %v for %v info.Mode().IsRegular() got %v wanted %v\n", statFunc.Name, test.Path, got, want)
   141  			}
   142  			// test info.Size()
   143  			if test.IsRegular {
   144  				if got, want := info.Size(), int64(len(test.Contents)); got != want {
   145  					t.Errorf("Using %v for %v inf.Size() got %v wanted %v", statFunc.Name, test.Path, got, want)
   146  				}
   147  			}
   148  		}
   149  	}
   150  }
   151  
   152  func TestZipFSNotExist(t *testing.T) {
   153  	_, err := fs.Open("/does-not-exist")
   154  	if err == nil {
   155  		t.Fatalf("Expected an error.\n")
   156  	}
   157  	if !os.IsNotExist(err) {
   158  		t.Errorf("Expected an error satisfying os.IsNotExist: %v\n", err)
   159  	}
   160  }
   161  
   162  func TestZipFSOpenSeek(t *testing.T) {
   163  	for _, test := range tests {
   164  		if test.IsRegular {
   165  
   166  			// test Open()
   167  			f, err := fs.Open(test.Path)
   168  			if err != nil {
   169  				t.Error(err)
   170  				return
   171  			}
   172  			defer f.Close()
   173  
   174  			// test Seek() multiple times
   175  			for i := 0; i < 3; i++ {
   176  				all, err := io.ReadAll(f)
   177  				if err != nil {
   178  					t.Error(err)
   179  					return
   180  				}
   181  				if got, want := string(all), test.Contents; got != want {
   182  					t.Errorf("File contents for %v got %v wanted %v\n", test.Path, got, want)
   183  				}
   184  				f.Seek(0, 0)
   185  			}
   186  		}
   187  	}
   188  }
   189  
   190  func TestRootType(t *testing.T) {
   191  	tests := []struct {
   192  		path   string
   193  		fsType vfs.RootType
   194  	}{
   195  		{"/src/net/http", vfs.RootTypeGoRoot},
   196  		{"/src/badpath", ""},
   197  		{"/", vfs.RootTypeGoRoot},
   198  	}
   199  
   200  	for _, item := range tests {
   201  		if fs.RootType(item.path) != item.fsType {
   202  			t.Errorf("unexpected fsType. Expected- %v, Got- %v", item.fsType, fs.RootType(item.path))
   203  		}
   204  	}
   205  }