Skip to content

Commit

Permalink
Add sysfs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Oct 30, 2014
1 parent 45c5140 commit a7cee22
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PACKAGES := gobot gobot/api gobot/platforms/intel-iot/edison $(shell ls ./platforms | sed -e 's/^/gobot\/platforms\//')
PACKAGES := gobot gobot/api gobot/platforms/intel-iot/edison gobot/sysfs $(shell ls ./platforms | sed -e 's/^/gobot\/platforms\//')

.PHONY: test cover robeaux

Expand Down
2 changes: 1 addition & 1 deletion scripts/travis.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
PACKAGES=('gobot' 'gobot/api' 'gobot/platforms/intel-iot/edison' $(ls ./platforms | sed -e 's/^/gobot\/platforms\//'))
PACKAGES=('gobot' 'gobot/api' 'gobot/platforms/intel-iot/edison' 'gobot/sysfs' $(ls ./platforms | sed -e 's/^/gobot\/platforms\//'))
EXITCODE=0

echo "mode: count" > profile.cov
Expand Down
11 changes: 7 additions & 4 deletions sysfs/digital_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (d *DigitalPin) Write(b int) error {

// Read reads the current value of the pin
func (d *DigitalPin) Read() (n int, err error) {
buf, err := ioutil.ReadFile(fmt.Sprintf("%v/%v/value", GPIOPATH, d.label))
buf, err := readFile(fmt.Sprintf("%v/%v/value", GPIOPATH, d.label))
if err != nil {
return
}
Expand All @@ -72,13 +72,16 @@ func (d *DigitalPin) Unexport() error {
return err
}

// writeFile validates file existence and writes data into it
func writeFile(name string, data []byte) (i int, err error) {
file, err := os.OpenFile(name, os.O_WRONLY, 0644)
var writeFile = func(path string, data []byte) (i int, err error) {
file, err := os.OpenFile(path, os.O_WRONLY, 0644)
defer file.Close()
if err != nil {
return
}

return file.Write(data)
}

var readFile = func(path string) (b []byte, err error) {
return ioutil.ReadFile(path)
}
59 changes: 59 additions & 0 deletions sysfs/digital_pin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sysfs

import (
"testing"

"github.com/hybridgroup/gobot"
)

func TestDigitalPin(t *testing.T) {
lastPath := ""
lastData := []byte{}

writeFile = func(path string, data []byte) (i int, err error) {
lastPath = path
lastData = data
return
}

readFile = func(path string) (b []byte, err error) {
lastPath = path
return []byte("0"), nil
}

pin := NewDigitalPin(10, "custom")
gobot.Assert(t, pin.pin, "10")
gobot.Assert(t, pin.label, "custom")

pin = NewDigitalPin(10)
gobot.Assert(t, pin.label, "gpio10")

pin.Unexport()
gobot.Assert(t, lastPath, "/sys/class/gpio/unexport")
gobot.Assert(t, string(lastData), "10")

pin.Export()
gobot.Assert(t, lastPath, "/sys/class/gpio/export")
gobot.Assert(t, string(lastData), "10")

pin.Write(1)
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
gobot.Assert(t, string(lastData), "1")

pin.Write(1)
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
gobot.Assert(t, string(lastData), "1")

pin.SetDirection(IN)
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/direction")
gobot.Assert(t, string(lastData), "in")

pin.Direction()
gobot.Assert(t, pin.direction, "in")
pin.SetDirection(OUT)
gobot.Assert(t, pin.direction, "out")

data, _ := pin.Read()
gobot.Assert(t, data, 0)
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
}
12 changes: 12 additions & 0 deletions sysfs/i2c_device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package sysfs

import (
"io"
"os"
"testing"
)

func TestNewI2cDevice(t *testing.T) {
i, _ := NewI2cDevice(os.DevNull, 0xff)
var _ io.ReadWriteCloser = i
}

0 comments on commit a7cee22

Please sign in to comment.