mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 11:44:51 +00:00
try some more tsting
This commit is contained in:
36
internal/fswatcher/inotify/sys/syscalls.go
Normal file
36
internal/fswatcher/inotify/sys/syscalls.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// +build linux
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const events = unix.IN_ALL_EVENTS
|
||||
|
||||
type InotifySyscallsUNIX struct{}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) Init() (int, error) {
|
||||
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
|
||||
if fd < 0 {
|
||||
return fd, fmt.Errorf("errno: %d", errno)
|
||||
}
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) AddWatch(fd int, dir string) (int, error) {
|
||||
wd, errno := unix.InotifyAddWatch(fd, dir, events)
|
||||
if wd < 0 {
|
||||
return wd, fmt.Errorf("errno: %d", errno)
|
||||
}
|
||||
return wd, nil
|
||||
}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) Close(fd int) error {
|
||||
if err := unix.Close(fd); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
45
internal/fswatcher/inotify/sys/syscalls_test.go
Normal file
45
internal/fswatcher/inotify/sys/syscalls_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// +build linux
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSyscalls(t *testing.T) {
|
||||
is := &InotifySyscallsUNIX{}
|
||||
|
||||
fd, err := is.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error for inotify init: %v", err)
|
||||
}
|
||||
|
||||
_, err = is.AddWatch(fd, "testdata")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error adding watch to dir 'testdata': %v", err)
|
||||
}
|
||||
|
||||
err = is.Close(fd)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error closing inotify: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyscallsError(t *testing.T) {
|
||||
is := &InotifySyscallsUNIX{}
|
||||
|
||||
fd, err := is.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error for inotify init: %v", err)
|
||||
}
|
||||
|
||||
_, err = is.AddWatch(fd, "non-existing-dir")
|
||||
if err == nil || err.Error() != "errno: 2" {
|
||||
t.Fatalf("Expected errno 2 for non-existing-dir but got: %v", err)
|
||||
}
|
||||
|
||||
err = is.Close(fd)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error closing inotify: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user