func TestFilespaceConverter_FromMultipart(t *testing.T) { memFilespace, err := memfs.NewFilespace() if err != nil { t.Error(err) return } converter := NewFilespaceConverter(memFilespace) fh := mockmultipart.NewFileHeader([]byte(TestFileContent)) iInterface, err := converter.FromMultipart(fh) if err != nil { t.Error(err) return } file, ok := iInterface.(types.File) if !ok { t.Errorf("converter.FromString result is not types.File instance %v", file) return } fileContent, err := file.Filespace().ReadFile(file.Path()) if err != nil { t.Error(err) return } if !testbase.ByteArrayEq(fileContent, []byte(TestFileContent)) { t.Errorf("converter.FromString result is not types.File instance \n%v \n !=\n %v", string(fileContent), TestFileContent) return } }
func TestCopySingleFile(t *testing.T) { const ( srcPath = "src" destPath = "dest" file1Path = "/d1/d2/f1.ex" ) // init fs, err := memfs.NewFilespace() if err != nil { t.Error(err) } //Create data testData1 := []byte("Content of file 1") // create test model fs.WriteFile(srcPath+file1Path, testData1, 0777) // copy fs.Copy(srcPath+file1Path, destPath+file1Path) // test readData1, err := fs.ReadFile(destPath + file1Path) if err != nil { t.Error("can not read file1 after write data ", err) } else { if !testbase.ByteArrayEq(testData1, readData1) { t.Error("read1 and test1 data are diffrent ", testData1, readData1) } } }
func TestWriteAndRead(t *testing.T) { // init fs, err := memfs.NewFilespace() if err != nil { t.Error(err) } //Create data testData := []byte("There is test data") // create directories path := "/mydir1/mydir2/mydir3/myfile.ex" fs.WriteFile(path, testData, 0777) readData, err := fs.ReadFile(path) if err != nil { t.Error("can not read file after write data ", err) } if !testbase.ByteArrayEq(readData, testData) { t.Error("read data are diffrent ", readData, testData) } }
func TestWriteAndReader(t *testing.T) { // init fs, err := memfs.NewFilespace() if err != nil { t.Error(err) } //Create data testData := []byte("There is test data") // create directories path := "/mydir1/mydir2/mydir3/myfile.ex" err = fs.WriteFile(path, testData, 0777) if err != nil { t.Error(err) return } reader, err := fs.Reader(path) if err != nil { t.Error(err) return } buf := make([]byte, 222) n, err := reader.Read(buf) if err != nil { t.Error(err) return } err = reader.Close() if err != nil { t.Error(err) return } if n != len(testData) { t.Errorf("return length should be equal to data size %v %v", n, len(testData)) return } if !testbase.ByteArrayEq(buf[:n], testData) { t.Error("read data are diffrent ", buf, testData) } }
func TestWriteStreamAndRead(t *testing.T) { // init fs, err := memfs.NewFilespace() if err != nil { t.Error(err) } //Create data testData := []byte("There is test data") // create directories path := "/mydir1/mydir2/mydir3/myfile.ex" writer, err := fs.Writer(path) if err != nil { t.Error(err) return } n, err := writer.Write(testData) if err != nil { t.Error(err) return } if n != len(testData) { t.Errorf("return length should be equal to data size %v %v", n, len(testData)) return } err = writer.Close() if err != nil { t.Error(err) return } readData, err := fs.ReadFile(path) if err != nil { t.Error(err) } if !testbase.ByteArrayEq(readData, testData) { t.Error("read data are diffrent ", readData, testData) } }