pkger/file_test.go

109 lines
1.8 KiB
Go
Raw Normal View History

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"
"time"
2019-07-31 18:53:36 +03:00
"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 22:30:12 +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 22:30:12 +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 22:30:12 +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 22:30:12 +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)
// because windows can't handle the time precisely
// enough, we have to *force* just a smidge of time
// to ensure the two ModTime's are different.
// i know, i hate it too.
time.Sleep(time.Millisecond)
2019-07-31 23:29:49 +03:00
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)
2019-08-09 22:30:12 +03:00
f, err := createFile("/radio.radio")
2019-08-02 22:14:11 +03:00
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))
}