From ece9aa16f90a36dbd010763f493c9a542d996b17 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 7 Nov 2014 16:56:13 -0800 Subject: [PATCH] Add syscall mock --- sysfs/i2c_device.go | 17 ++++++----------- sysfs/i2c_device_test.go | 5 +---- sysfs/syscall.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 sysfs/syscall.go diff --git a/sysfs/i2c_device.go b/sysfs/i2c_device.go index f13eab5c..78c04365 100644 --- a/sysfs/i2c_device.go +++ b/sysfs/i2c_device.go @@ -18,7 +18,12 @@ func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) { return nil, err } - errno := ioctrl(file, address) + _, _, errno := Syscall( + syscall.SYS_IOCTL, + file.Fd(), + I2CSlave, + uintptr(address), + ) if errno != 0 { return nil, errors.New(fmt.Sprintf("Failed with syscall.Errno %v", errno)) @@ -26,13 +31,3 @@ func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) { return file, nil } - -var ioctrl = func(file File, address byte) syscall.Errno { - _, _, errno := syscall.Syscall( - syscall.SYS_IOCTL, - file.Fd(), - I2CSlave, uintptr(address), - ) - - return errno -} diff --git a/sysfs/i2c_device_test.go b/sysfs/i2c_device_test.go index 2d70f661..bd729721 100644 --- a/sysfs/i2c_device_test.go +++ b/sysfs/i2c_device_test.go @@ -4,7 +4,6 @@ import ( "github.com/hybridgroup/gobot" "io" "os" - "syscall" "testing" ) @@ -24,9 +23,7 @@ func TestNewI2cDevice(t *testing.T) { i, err = NewI2cDevice("/dev/i2c-1", 0xff) gobot.Refute(t, err, nil) - ioctrl = func(file File, address byte) syscall.Errno { - return 0 - } + SetSyscall(&MockSyscall{}) i, err = NewI2cDevice("/dev/i2c-1", 0xff) gobot.Assert(t, err, nil) diff --git a/sysfs/syscall.go b/sysfs/syscall.go new file mode 100644 index 00000000..c0ac5548 --- /dev/null +++ b/sysfs/syscall.go @@ -0,0 +1,30 @@ +package sysfs + +import ( + "syscall" +) + +type SystemCaller interface { + Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) +} + +type NativeSyscall struct{} +type MockSyscall struct{} + +var sys SystemCaller = &NativeSyscall{} + +func SetSyscall(s SystemCaller) { + sys = s +} + +func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) { + return sys.Syscall(trap, a1, a2, a3) +} + +func (sys *NativeSyscall) Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) { + return syscall.Syscall(trap, a1, a2, a3) +} + +func (sys *MockSyscall) Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) { + return 0, 0, 0 +}