...

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

Documentation: github.com/spf13/afero/zipfs

     1  package zipfs
     2  
     3  import (
     4  	"archive/zip"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  func TestFileRead(t *testing.T) {
    10  	zrc, err := zip.OpenReader("testdata/small.zip")
    11  	if err != nil {
    12  		t.Fatal(err)
    13  	}
    14  	zfs := New(&zrc.Reader)
    15  	f, err := zfs.Open("smallFile")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	info, err := f.Stat()
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	chunkSize := info.Size() * 2 // read with extra large buffer
    24  
    25  	buf := make([]byte, chunkSize)
    26  	n, err := f.Read(buf)
    27  	if err != io.EOF {
    28  		t.Fatal("Failed to read file to completion:", err)
    29  	}
    30  	if n != int(info.Size()) {
    31  		t.Errorf("Expected read length to be %d, found: %d", info.Size(), n)
    32  	}
    33  
    34  	// read a second time to check f.offset and f.buf are correct
    35  	buf = make([]byte, chunkSize)
    36  	n, err = f.Read(buf)
    37  	if err != io.EOF {
    38  		t.Fatal("Failed to read a fully read file:", err)
    39  	}
    40  	if n != 0 {
    41  		t.Errorf("Expected read length to be 0, found: %d", n)
    42  	}
    43  }
    44  

View as plain text