Previously this case was hidden because ReadString() can return a string
and an EOF error. To prevent this issue from going unnoticed error out.
t, err := s.ReadString('\n')
if err != nil {
if err == io.EOF {
+ if t != "" {
+ return nil, fmt.Errorf(
+ "no newline in last line: %q",
+ t)
+ }
break
}
return nil, err
package main
import (
+ "fmt"
"reflect"
"strings"
"testing"
tests := []struct {
data string
exp []Group
+ err error
}{
{
"",
nil,
+ nil,
+ },
+ {
+ "root:x:0:",
+ nil,
+ fmt.Errorf("no newline in last line: \"root:x:0:\""),
},
{
"root:x:0:\n",
Members: nil,
},
},
+ nil,
},
{
"root:x:0:foo\n",
},
},
},
+ nil,
},
{
"root:x:0:foo,bar\n",
},
},
},
+ nil,
},
}
for n, tc := range tests {
res, err := ParseGroups(strings.NewReader(tc.data))
- if err != nil {
+ if !reflect.DeepEqual(err, tc.err) {
t.Errorf("%d: err = %v, want %v",
- n, res, nil)
+ n, err, tc.err)
}
if !reflect.DeepEqual(res, tc.exp) {
t.Errorf("%d: res = %v, want %v",
t, err := s.ReadString('\n')
if err != nil {
if err == io.EOF {
+ if t != "" {
+ return nil, fmt.Errorf(
+ "no newline in last line: %q",
+ t)
+ }
break
}
return nil, err