C2FO / vfs

Pluggable, extensible virtual file system for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mem.File doesn't store Write bytes in a way accessible to Reads

funkyshu opened this issue · comments

See:

package main

import (
	"fmt"

	"github.com/c2fo/vfs/v5/backend/mem"
)

func main() {
	m := mem.NewFileSystem()
	f, err := m.NewFile("", "/path/to/file.txt")
	if err != nil {
		panic(err)
	}

	// write to file
	b, err := f.Write([]byte("this is some sample text\n"))
	if err != nil {
		panic(err)
	}

	// check byte count
	if b != 25 {
		panic(fmt.Errorf("bytes written should be 25 but got: %d", b))
	}

	// close file (so we can read from it)
	err = f.Close()
	if err != nil {
		panic(err)
	}

	checkBytes := make([]byte, 5)
	_, err = f.Read(checkBytes)
	if err != nil {
		panic(err)
	}

	// should match, but doesn't
	if string(checkBytes) != "bytes" {
		panic(fmt.Errorf("read bytes doesn't match 'bytes', got: %s", string(checkBytes)))
	}

}

I suspect there will be issues with Seek as well.
Mem probably deserves a rewrite.

Not sure what I was thinking. checkBytes should be "this ", not "bytes". Closing.