1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00
hybridgroup.gobot/sysfs/fs_mock_test.go

107 lines
2.8 KiB
Go
Raw Normal View History

2014-11-07 16:21:39 -08:00
package sysfs
import (
"sort"
"testing"
"gobot.io/x/gobot/gobottest"
)
2014-11-07 16:21:39 -08:00
func TestMockFilesystemOpen(t *testing.T) {
2022-11-05 07:42:28 +01:00
fs := newMockFilesystem([]string{"foo"})
2014-11-07 16:21:39 -08:00
f1 := fs.Files["foo"]
gobottest.Assert(t, f1.Opened, false)
2022-11-05 07:42:28 +01:00
f2, err := fs.openFile("foo", 0, 0666)
gobottest.Assert(t, f1, f2)
gobottest.Assert(t, err, nil)
2014-11-07 16:21:39 -08:00
err = f2.Sync()
gobottest.Assert(t, err, nil)
2014-11-07 16:21:39 -08:00
2022-11-05 07:42:28 +01:00
_, err = fs.openFile("bar", 0, 0666)
gobottest.Assert(t, err.Error(), " : bar: No such file.")
2014-11-07 16:21:39 -08:00
fs.Add("bar")
2022-11-05 07:42:28 +01:00
f4, _ := fs.openFile("bar", 0, 0666)
gobottest.Refute(t, f4.Fd(), f1.Fd())
}
func TestMockFilesystemStat(t *testing.T) {
2022-11-05 07:42:28 +01:00
fs := newMockFilesystem([]string{"foo", "bar/baz"})
2022-11-05 07:42:28 +01:00
fileStat, err := fs.stat("foo")
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fileStat.IsDir(), false)
2022-11-05 07:42:28 +01:00
dirStat, err := fs.stat("bar")
gobottest.Assert(t, err, nil)
gobottest.Assert(t, dirStat.IsDir(), true)
2022-11-05 07:42:28 +01:00
_, err = fs.stat("plonk")
gobottest.Assert(t, err.Error(), " : plonk: No such file.")
}
func TestMockFilesystemFind(t *testing.T) {
// arrange
2022-11-05 07:42:28 +01:00
fs := newMockFilesystem([]string{"/foo", "/bar/foo", "/bar/foo/baz", "/bar/baz/foo", "/bar/foo/bak"})
var tests = map[string]struct {
baseDir string
pattern string
want []string
}{
"flat": {baseDir: "/", pattern: "foo", want: []string{"/foo"}},
"in directory no slash": {baseDir: "/bar", pattern: "foo", want: []string{"/bar/foo", "/bar/foo", "/bar/foo"}},
"file": {baseDir: "/bar/baz/", pattern: "foo", want: []string{"/bar/baz/foo"}},
"file pattern": {baseDir: "/bar/foo/", pattern: "ba.?", want: []string{"/bar/foo/bak", "/bar/foo/baz"}},
"empty": {baseDir: "/", pattern: "plonk", want: nil},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// act
2022-11-05 07:42:28 +01:00
dirs, err := fs.find(tt.baseDir, tt.pattern)
// assert
gobottest.Assert(t, err, nil)
sort.Strings(dirs)
gobottest.Assert(t, dirs, tt.want)
})
}
}
2014-11-07 16:21:39 -08:00
func TestMockFilesystemWrite(t *testing.T) {
2022-11-05 07:42:28 +01:00
fs := newMockFilesystem([]string{"bar"})
2014-11-07 16:21:39 -08:00
f1 := fs.Files["bar"]
2022-11-05 07:42:28 +01:00
f2, err := fs.openFile("bar", 0, 0666)
gobottest.Assert(t, err, nil)
// Never been read or written.
gobottest.Assert(t, f1.Seq <= 0, true)
f2.WriteString("testing")
// Was written.
gobottest.Assert(t, f1.Seq > 0, true)
gobottest.Assert(t, f1.Contents, "testing")
}
2014-11-07 16:21:39 -08:00
func TestMockFilesystemRead(t *testing.T) {
2022-11-05 07:42:28 +01:00
fs := newMockFilesystem([]string{"bar"})
2014-11-07 16:21:39 -08:00
f1 := fs.Files["bar"]
f1.Contents = "Yip"
2022-11-05 07:42:28 +01:00
f2, err := fs.openFile("bar", 0, 0666)
gobottest.Assert(t, err, nil)
// Never been read or written.
gobottest.Assert(t, f1.Seq <= 0, true)
buffer := make([]byte, 20)
n, _ := f2.Read(buffer)
// Was read.
gobottest.Assert(t, f1.Seq > 0, true)
gobottest.Assert(t, n, 3)
gobottest.Assert(t, string(buffer[:3]), "Yip")
2014-11-07 16:21:39 -08:00
n, _ = f2.ReadAt(buffer, 10)
gobottest.Assert(t, n, 3)
}