...

Source file src/github.com/spf13/afero/zipfs/zipfs_test.go

Documentation: github.com/spf13/afero/zipfs

     1  package zipfs
     2  
     3  import (
     4  	"archive/zip"
     5  	"path/filepath"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/spf13/afero"
    10  )
    11  
    12  func TestZipFS(t *testing.T) {
    13  	zrc, err := zip.OpenReader("testdata/t.zip")
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	zfs := New(&zrc.Reader)
    18  	a := &afero.Afero{Fs: zfs}
    19  
    20  	buf, err := a.ReadFile("testFile")
    21  	if err != nil {
    22  		t.Error(err)
    23  	}
    24  	if len(buf) != 8192 {
    25  		t.Errorf("short read: %d != 8192", len(buf))
    26  	}
    27  
    28  	buf = make([]byte, 8)
    29  	f, err := a.Open("testFile")
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  	if n, err := f.ReadAt(buf, 4092); err != nil {
    34  		t.Error(err)
    35  	} else if n != 8 {
    36  		t.Errorf("expected to read 8 bytes, got %d", n)
    37  	} else if string(buf) != "aaaabbbb" {
    38  		t.Errorf("expected to get <aaaabbbb>, got <%s>", string(buf))
    39  	}
    40  
    41  	d, err := a.Open("/")
    42  	if d == nil {
    43  		t.Error(`Open("/") returns nil`)
    44  	}
    45  	if err != nil {
    46  		t.Errorf(`Open("/"): err = %v`, err)
    47  	}
    48  	if s, _ := d.Stat(); !s.IsDir() {
    49  		t.Error(`expected root ("/") to be a directory`)
    50  	}
    51  	if n := d.Name(); n != string(filepath.Separator) {
    52  		t.Errorf("Wrong Name() of root directory: Expected: '%c', got '%s'", filepath.Separator, n)
    53  	}
    54  
    55  	buf = make([]byte, 8192)
    56  	if n, err := f.Read(buf); err != nil {
    57  		t.Error(err)
    58  	} else if n != 8192 {
    59  		t.Errorf("expected to read 8192 bytes, got %d", n)
    60  	} else if buf[4095] != 'a' || buf[4096] != 'b' {
    61  		t.Error("got wrong contents")
    62  	}
    63  
    64  	for _, s := range []struct {
    65  		path string
    66  		dir  bool
    67  	}{
    68  		{"/", true},
    69  		{"testDir1", true},
    70  		{"testDir1/testFile", false},
    71  		{"testFile", false},
    72  		{"sub", true},
    73  		{"sub/testDir2", true},
    74  		{"sub/testDir2/testFile", false},
    75  	} {
    76  		if dir, _ := a.IsDir(s.path); dir == s.dir {
    77  			t.Logf("%s: directory check ok", s.path)
    78  		} else {
    79  			t.Errorf("%s: directory check NOT ok: %t, expected %t", s.path, dir, s.dir)
    80  		}
    81  	}
    82  
    83  	for _, s := range []struct {
    84  		glob    string
    85  		entries []string
    86  	}{
    87  		{filepath.FromSlash("/*"), []string{filepath.FromSlash("/sub"), filepath.FromSlash("/testDir1"), filepath.FromSlash("/testFile")}},
    88  		{filepath.FromSlash("*"), []string{filepath.FromSlash("sub"), filepath.FromSlash("testDir1"), filepath.FromSlash("testFile")}},
    89  		{filepath.FromSlash("sub/*"), []string{filepath.FromSlash("sub/testDir2")}},
    90  		{filepath.FromSlash("sub/testDir2/*"), []string{filepath.FromSlash("sub/testDir2/testFile")}},
    91  		{filepath.FromSlash("testDir1/*"), []string{filepath.FromSlash("testDir1/testFile")}},
    92  	} {
    93  		entries, err := afero.Glob(zfs, s.glob)
    94  		if err != nil {
    95  			t.Error(err)
    96  		}
    97  		if reflect.DeepEqual(entries, s.entries) {
    98  			t.Logf("glob: %s: glob ok", s.glob)
    99  		} else {
   100  			t.Errorf("glob: %s: got %#v, expected %#v", s.glob, entries, s.entries)
   101  		}
   102  	}
   103  }
   104  

View as plain text