瀏覽代碼

chore: refactor log limit in rest (#3572)

Kevin Wan 1 年之前
父節點
當前提交
0dcede6457
共有 7 個文件被更改,包括 50 次插入15 次删除
  1. 12 0
      core/iox/nopcloser_test.go
  2. 7 7
      core/iox/read.go
  3. 10 0
      core/iox/read_test.go
  4. 2 0
      core/iox/tee.go
  5. 12 7
      core/iox/tee_test.go
  6. 2 1
      core/iox/textfile.go
  7. 5 0
      core/iox/textfile_test.go

+ 12 - 0
core/iox/nopcloser_test.go

@@ -0,0 +1,12 @@
+package iox
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestNopCloser(t *testing.T) {
+	closer := NopCloser(nil)
+	assert.NoError(t, closer.Close())
+}

+ 7 - 7
core/iox/read.go

@@ -28,6 +28,13 @@ func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
 	return io.NopCloser(tee), io.NopCloser(&buf)
 }
 
+// KeepSpace customizes the reading functions to keep leading and tailing spaces.
+func KeepSpace() TextReadOption {
+	return func(o *textReadOptions) {
+		o.keepSpace = true
+	}
+}
+
 // LimitDupReadCloser returns two io.ReadCloser that read from the first will be written to the second.
 // But the second io.ReadCloser is limited to up to n bytes.
 // The first returned reader needs to be read first, because the content
@@ -38,13 +45,6 @@ func LimitDupReadCloser(reader io.ReadCloser, n int64) (io.ReadCloser, io.ReadCl
 	return io.NopCloser(tee), io.NopCloser(&buf)
 }
 
-// KeepSpace customizes the reading functions to keep leading and tailing spaces.
-func KeepSpace() TextReadOption {
-	return func(o *textReadOptions) {
-		o.keepSpace = true
-	}
-}
-
 // ReadBytes reads exactly the bytes with the length of len(buf)
 func ReadBytes(reader io.Reader, buf []byte) error {
 	var got int

+ 10 - 0
core/iox/read_test.go

@@ -51,6 +51,11 @@ b`,
 	}
 }
 
+func TestReadTextError(t *testing.T) {
+	_, err := ReadText("not-exist")
+	assert.NotNil(t, err)
+}
+
 func TestReadTextLines(t *testing.T) {
 	text := `1
 
@@ -94,6 +99,11 @@ func TestReadTextLines(t *testing.T) {
 	}
 }
 
+func TestReadTextLinesError(t *testing.T) {
+	_, err := ReadTextLines("not-exist")
+	assert.NotNil(t, err)
+}
+
 func TestDupReadCloser(t *testing.T) {
 	input := "hello"
 	reader := io.NopCloser(bytes.NewBufferString(input))

+ 2 - 0
core/iox/tee.go

@@ -27,7 +27,9 @@ func (t *limitTeeReader) Read(p []byte) (n int, err error) {
 		if n, err := t.w.Write(p[:limit]); err != nil {
 			return n, err
 		}
+
 		t.n -= limit
 	}
+
 	return
 }

+ 12 - 7
core/iox/tee_test.go

@@ -4,6 +4,8 @@ import (
 	"bytes"
 	"io"
 	"testing"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func TestLimitTeeReader(t *testing.T) {
@@ -22,14 +24,17 @@ func TestLimitTeeReader(t *testing.T) {
 	if !bytes.Equal(wb.Bytes(), src[:limit]) {
 		t.Errorf("bytes written = %q want %q", wb.Bytes(), src)
 	}
-	if n, err := r.Read(dst); n != 0 || err != io.EOF {
-		t.Errorf("r.Read at EOF = %d, %v want 0, EOF", n, err)
-	}
+
+	n, err := r.Read(dst)
+	assert.Equal(t, 0, n)
+	assert.Equal(t, io.EOF, err)
+
 	rb = bytes.NewBuffer(src)
 	pr, pw := io.Pipe()
-	pr.Close()
-	r = LimitTeeReader(rb, pw, limit)
-	if n, err := io.ReadFull(r, dst); n != 0 || err != io.ErrClosedPipe {
-		t.Errorf("closed tee: ReadFull(r, dst) = %d, %v; want 0, EPIPE", n, err)
+	if assert.NoError(t, pr.Close()) {
+		r = LimitTeeReader(rb, pw, limit)
+		n, err := io.ReadFull(r, dst)
+		assert.Equal(t, 0, n)
+		assert.Equal(t, io.ErrClosedPipe, err)
 	}
 }

+ 2 - 1
core/iox/textfile.go

@@ -2,6 +2,7 @@ package iox
 
 import (
 	"bytes"
+	"errors"
 	"io"
 	"os"
 )
@@ -26,7 +27,7 @@ func CountLines(file string) (int, error) {
 		count += bytes.Count(buf[:c], lineSep)
 
 		switch {
-		case err == io.EOF:
+		case errors.Is(err, io.EOF):
 			if noEol {
 				count++
 			}

+ 5 - 0
core/iox/textfile_test.go

@@ -24,3 +24,8 @@ func TestCountLines(t *testing.T) {
 	assert.Nil(t, err)
 	assert.Equal(t, 4, lines)
 }
+
+func TestCountLinesError(t *testing.T) {
+	_, err := CountLines("not-exist")
+	assert.NotNil(t, err)
+}