Skip to content

Commit

Permalink
Fix finding first symbol of a line
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Myskin committed Jun 2, 2019
1 parent 12e020b commit 7ea3804
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion tailor.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ func (t *Tailor) seekToLineStart() error {
return nil
}

offset, err = t.file.Seek(-2, io.SeekCurrent)
newOffset := int64(-2)
if offset-2 < 0 {
newOffset = -1
}

offset, err = t.file.Seek(newOffset, io.SeekCurrent)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions tailor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ func TestTailFileFromStart(t *testing.T) {
const fileName = "./file_from_start"
fileData := []byte(`1
2
3`)
3
`)

err := ioutil.WriteFile(fileName, fileData, os.ModePerm)
if err != nil {
t.Error(err)
return
}
defer os.Remove(fileName)

f := tailor.New(fileName, tailor.WithSeekOnStartup(0, io.SeekStart))
if fileName != f.FileName() {
t.Error("file name mismatch")
return
}

ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
err = f.Run(ctx)
if err != nil {
t.Error(err)
return
}

var i = 1
Expand All @@ -54,14 +58,16 @@ func TestTailFileFromStart(t *testing.T) {
}

if line.StringTrimmed() != strconv.Itoa(i) {
t.Error(err)
t.Errorf("want: '%d' actual '%s'", i, line.StringTrimmed())
return
}
t.Log(line.StringTrimmed())
case err, ok := <-f.Errors():
if !ok {
return
}
t.Error(err)
return
}
}
}

0 comments on commit 7ea3804

Please # to comment.