sysfs: add file read/write testing for failure conditions

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-27 08:41:34 +02:00
parent f35a4decce
commit df11a4e6af
2 changed files with 18 additions and 3 deletions

View File

@ -88,7 +88,8 @@ func (d *digitalPin) Export() error {
_, err = writeFile(export, []byte(d.pin))
if err != nil {
// If EBUSY then the pin has already been exported
if err.(*os.PathError).Err != syscall.EBUSY {
e, ok := err.(*os.PathError)
if !ok || e.Err != syscall.EBUSY {
return err
}
}

View File

@ -15,6 +15,8 @@ var _ Filesystem = (*MockFilesystem)(nil)
type MockFilesystem struct {
Seq int // Increases with each write or read.
Files map[string]*MockFile
WithReadError bool
WithWriteError bool
}
// A MockFile represents a mock file that contains a single string. Any write
@ -29,8 +31,16 @@ type MockFile struct {
fs *MockFilesystem
}
var (
readErr = errors.New("read error")
writeErr = errors.New("write error")
)
// Write writes string(b) to f.Contents
func (f *MockFile) Write(b []byte) (n int, err error) {
if f.fs.WithWriteError {
return 0, writeErr
}
return f.WriteString(string(b))
}
@ -53,6 +63,10 @@ func (f *MockFile) Sync() (err error) {
// Read copies b bytes from f.Contents
func (f *MockFile) Read(b []byte) (n int, err error) {
if f.fs.WithReadError {
return 0, readErr
}
count := len(b)
if len(f.Contents) < count {
count = len(f.Contents)