2019-07-31 18:53:36 +03:00
|
|
|
package pkger
|
|
|
|
|
|
|
|
import (
|
2019-08-02 22:14:11 +03:00
|
|
|
"encoding/json"
|
2019-07-31 23:29:49 +03:00
|
|
|
"io"
|
2019-07-31 18:53:36 +03:00
|
|
|
"io/ioutil"
|
2019-07-31 23:29:49 +03:00
|
|
|
"strings"
|
2019-07-31 18:53:36 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_File_Open(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
f, err := Open("/file_test.go")
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-08-09 05:35:01 +03:00
|
|
|
r.Equal("file_test.go", f.Name())
|
2019-07-31 18:53:36 +03:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Contains(string(b), "Test_File_Open")
|
|
|
|
r.NoError(f.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_File_Open_Dir(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
f, err := Open("/cmd")
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-08-09 05:35:01 +03:00
|
|
|
r.Equal("cmd", f.Name())
|
2019-07-31 18:53:36 +03:00
|
|
|
|
|
|
|
r.NoError(f.Close())
|
|
|
|
}
|
2019-07-31 23:29:49 +03:00
|
|
|
|
|
|
|
func Test_File_Read_Memory(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
f, err := Open("/file_test.go")
|
|
|
|
r.NoError(err)
|
|
|
|
f.data = []byte("hi!")
|
|
|
|
|
2019-08-09 05:35:01 +03:00
|
|
|
r.Equal("file_test.go", f.Name())
|
2019-07-31 23:29:49 +03:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(string(b), "hi!")
|
|
|
|
r.NoError(f.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_File_Write(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
2019-08-09 05:35:01 +03:00
|
|
|
f, err := Create("/hello.txt")
|
2019-07-31 23:29:49 +03:00
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(f)
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
r.NoError(err)
|
|
|
|
r.Zero(fi.Size())
|
|
|
|
|
2019-08-09 05:35:01 +03:00
|
|
|
r.Equal("hello.txt", fi.Name())
|
2019-07-31 23:29:49 +03:00
|
|
|
|
|
|
|
mt := fi.ModTime()
|
|
|
|
r.NotZero(mt)
|
|
|
|
|
|
|
|
sz, err := io.Copy(f, strings.NewReader(radio))
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(int64(1381), sz)
|
|
|
|
|
|
|
|
r.NoError(f.Close())
|
|
|
|
r.Equal(int64(1381), fi.Size())
|
|
|
|
r.NotZero(fi.ModTime())
|
|
|
|
r.NotEqual(mt, fi.ModTime())
|
|
|
|
}
|
2019-08-02 22:14:11 +03:00
|
|
|
|
|
|
|
func Test_File_JSON(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
f, err := createFile("radio.radio")
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(f)
|
|
|
|
|
|
|
|
bi, err := f.Stat()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
mj, err := json.Marshal(f)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
f2 := &File{}
|
|
|
|
|
|
|
|
r.NoError(json.Unmarshal(mj, f2))
|
|
|
|
|
|
|
|
ai, err := f2.Stat()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
r.Equal(bi.Size(), ai.Size())
|
|
|
|
|
|
|
|
r.Equal(string(f.data), string(f2.data))
|
|
|
|
}
|