2014-11-08 08:21:39 +08:00
|
|
|
package sysfs
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-12-31 22:12:25 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-10-31 04:41:24 +08:00
|
|
|
)
|
|
|
|
|
2014-11-08 08:21:39 +08:00
|
|
|
func TestMockFilesystemOpen(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{"foo"})
|
|
|
|
f1 := fs.Files["foo"]
|
2014-10-31 04:41:24 +08:00
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, f1.Opened, false)
|
2014-10-31 04:41:24 +08:00
|
|
|
f2, err := fs.OpenFile("foo", 0, 0666)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, f1, f2)
|
|
|
|
gobottest.Assert(t, err, nil)
|
2014-11-08 08:21:39 +08:00
|
|
|
|
|
|
|
err = f2.Sync()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-11-08 08:21:39 +08:00
|
|
|
|
|
|
|
_, err = fs.OpenFile("bar", 0, 0666)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Refute(t, err, nil)
|
2014-11-08 08:21:39 +08:00
|
|
|
|
|
|
|
fs.Add("bar")
|
2017-01-03 02:32:04 +08:00
|
|
|
f4, _ := fs.OpenFile("bar", 0, 0666)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Refute(t, f4.Fd(), f1.Fd())
|
2014-10-31 04:41:24 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:32:53 +08:00
|
|
|
func TestMockFilesystemStat(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{"foo", "bar/baz"})
|
|
|
|
|
|
|
|
fileStat, err := fs.Stat("foo")
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fileStat.IsDir(), false)
|
|
|
|
|
|
|
|
dirStat, err := fs.Stat("bar")
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, dirStat.IsDir(), true)
|
|
|
|
|
|
|
|
_, err = fs.Stat("plonk")
|
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2014-11-08 08:21:39 +08:00
|
|
|
func TestMockFilesystemWrite(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{"bar"})
|
|
|
|
f1 := fs.Files["bar"]
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
f2, err := fs.OpenFile("bar", 0, 0666)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-10-31 04:41:24 +08:00
|
|
|
// Never been read or written.
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, f1.Seq <= 0, true)
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
f2.WriteString("testing")
|
|
|
|
// Was written.
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, f1.Seq > 0, true)
|
|
|
|
gobottest.Assert(t, f1.Contents, "testing")
|
2014-10-31 04:41:24 +08:00
|
|
|
}
|
|
|
|
|
2014-11-08 08:21:39 +08:00
|
|
|
func TestMockFilesystemRead(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{"bar"})
|
|
|
|
f1 := fs.Files["bar"]
|
2014-10-31 04:41:24 +08:00
|
|
|
f1.Contents = "Yip"
|
|
|
|
|
|
|
|
f2, err := fs.OpenFile("bar", 0, 0666)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-10-31 04:41:24 +08:00
|
|
|
// Never been read or written.
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, f1.Seq <= 0, true)
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
buffer := make([]byte, 20)
|
2017-01-03 02:32:04 +08:00
|
|
|
n, _ := f2.Read(buffer)
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
// Was read.
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, f1.Seq > 0, true)
|
|
|
|
gobottest.Assert(t, n, 3)
|
|
|
|
gobottest.Assert(t, string(buffer[:3]), "Yip")
|
2014-11-08 08:21:39 +08:00
|
|
|
|
2017-01-03 02:32:04 +08:00
|
|
|
n, _ = f2.ReadAt(buffer, 10)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, n, 3)
|
2014-10-31 04:41:24 +08:00
|
|
|
}
|