Adds error for invalid skip records argument and exits csv importer (#2522)

This commit is contained in:
zcstarr
2016-09-06 17:37:05 -07:00
committed by GitHub
parent b1c0aeb9c5
commit ef817db179
3 changed files with 34 additions and 3 deletions

View File

@@ -134,7 +134,12 @@ func main() {
}
cr := csv.NewCSVReader(r, delim)
csv.SkipRecords(cr, *skipRecords)
err = csv.SkipRecords(cr, *skipRecords)
if err == io.EOF {
err = fmt.Errorf("skip-records skipped past EOF")
}
d.CheckErrorNoUsage(err)
var headers []string
if *header == "" {

View File

@@ -311,6 +311,7 @@ func (s *testSuite) TestCSVImportSkipRecords() {
setName := "csv"
dataspec := spec.CreateValueSpecString("ldb", s.LdbDir, setName)
stdout, stderr := s.MustRun(main, []string{"--no-progress", "--skip-records", "2", input.Name(), dataspec})
s.Equal("", stdout)
s.Equal("", stderr)
@@ -328,6 +329,26 @@ func (s *testSuite) TestCSVImportSkipRecords() {
s.Equal(types.String("8"), st.Get("b"))
}
func (s *testSuite) TestCSVImportSkipRecordsTooMany() {
input, err := ioutil.TempFile(s.TempDir, "")
d.Chk.NoError(err)
defer input.Close()
defer os.Remove(input.Name())
_, err = input.WriteString("a,b\n")
d.Chk.NoError(err)
setName := "csv"
dataspec := spec.CreateValueSpecString("ldb", s.LdbDir, setName)
stdout, stderr, recoveredErr := s.Run(main, []string{"--no-progress", "--skip-records", "100", input.Name(), dataspec})
s.Equal("", stdout)
s.Equal("error: skip-records skipped past EOF\n", stderr)
s.Equal(clienttest.ExitError{-1}, recoveredErr)
}
func (s *testSuite) TestCSVImportSkipRecordsCustomHeader() {
input, err := ioutil.TempFile(s.TempDir, "")
d.Chk.NoError(err)

View File

@@ -32,10 +32,15 @@ func (r reader) Read(p []byte) (n int, err error) {
return
}
func SkipRecords(r *csv.Reader, n uint) {
func SkipRecords(r *csv.Reader, n uint) error {
var err error
for i := uint(0); i < n; i++ {
r.Read()
_, err = r.Read()
if err != nil {
return err
}
}
return err
}
// NewCSVReader returns a new csv.Reader that splits on comma