Repository: zeromicro/go-zero
Pull request #3572 (2023-09-16 12:13:56)

chore: refactor log limit in rest

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3572?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3572](https://app.codecov.io/gh/zeromicro/go-zero/pull/3572?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (52de686) into [master](https://app.codecov.io/gh/zeromicro/go-zero/commit/cc21f5fae2356382e1564e43bb8192b0812b7d32?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (cc21f5f) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.


| [Files Changed](https://app.codecov.io/gh/zeromicro/go-zero/pull/3572?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/iox/tee.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3572?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9pb3gvdGVlLmdv) | `100.00% <ø> (ø)` | |
| [core/iox/read.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3572?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9pb3gvcmVhZC5nbw==) | `90.00% <100.00%> (ø)` | |

... and [2 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3572/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/core/iox/read.go b/core/iox/read.go
index 1f27f445..3ec024ec 100644
--- a/core/iox/read.go
+++ b/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
diff --git a/core/iox/tee.go b/core/iox/tee.go
index 07b00158..11c5221e 100644
--- a/core/iox/tee.go
+++ b/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
 }
diff --git a/core/iox/textfile.go b/core/iox/textfile.go
index 8feced43..89083cea 100644
--- a/core/iox/textfile.go
+++ b/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++
 			}


Test patch:
diff --git a/core/iox/nopcloser_test.go b/core/iox/nopcloser_test.go
new file mode 100644
index 00000000..5dfd4d56
--- /dev/null
+++ b/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())
+}
diff --git a/core/iox/read_test.go b/core/iox/read_test.go
index b31ce283..d2eb2435 100644
--- a/core/iox/read_test.go
+++ b/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))
diff --git a/core/iox/tee_test.go b/core/iox/tee_test.go
index 56e457c7..87f5267e 100644
--- a/core/iox/tee_test.go
+++ b/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)
 	}
 }
diff --git a/core/iox/textfile_test.go b/core/iox/textfile_test.go
index 011d674b..c58b3048 100644
--- a/core/iox/textfile_test.go
+++ b/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)
+}