Repository: grpc/grpc-go
File: grpclog/internal/loggerv2_test.go (Go)

Code before:
import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"os/exec"
	"regexp"
	"strings"
	"testing"
)

// check if b is in the format of:
//
//	2017/04/07 14:55:42 WARNING: WARNING
func checkLogForSeverity(s int, b []byte) error {
	expected := regexp.MustCompile(fmt.Sprintf(`^[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} %s: %s\n$`, severityName[s], severityName[s]))
	if m := expected.Match(b); !m {
		return fmt.Errorf("got: %v, want string in format of: %v", string(b), severityName[s]+": 2016/10/05 17:09:26 "+severityName[s])
	}
	return nil
}

// makeLogFuncErrr returns a logger printFunc which generates a test error,
// for testing that the log func is not called
func makeLogFuncErr(t *testing.T) func() string {
	return func() string {
		t.Errorf("got: logger func called on io.Discard logger, want: Logger func to not be called for io.Discard")
		return ""
	}
}

// logFuncStr is a string used via testLogFuncOk and
// testCheckLogContainsFuncStr to test the logger printFunc.
const logFuncStr = "called-func"

// logFuncOk is a logger printFunc to write a constant string
func logFuncOk() string {
	return logFuncStr
}

// checkLogContainsFuncStr checks that the logger buffer logBuf contains the
// string written by logFuncOk
func checkLogContainsFuncStr(t *testing.T, logBuf []byte) {
	if !bytes.Contains(logBuf, []byte(logFuncStr)) {
		t.Errorf("got '%v', want logger func to be called and print '%v'", string(logBuf), logFuncStr)
	}
}

// checkBufferWasWrittenAsExpected checks that the log buffer buf was written as expected,
// per the discard, logTYpe, msg, and isJSON arguments.

Review comment (suggestion):
```
// logFuncOk is a logger printFunc that returns a constant string to log.
```

Code after:
import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"reflect"
	"regexp"
	"strings"
	"testing"
)

// logFuncStr is a string used via testCheckLogContainsFuncStr to test the
// logger output.
const logFuncStr = "called-func"

func makeSprintfErr(t *testing.T) func(format string, a ...any) string {
	return func(string, ...any) string {
		t.Errorf("got: sprintf called on io.Discard logger, want: expensive sprintf to not be called for io.Discard")
		return ""
	}
}

func makeSprintErr(t *testing.T) func(a ...any) string {
	return func(...any) string {
		t.Errorf("got: sprint called on io.Discard logger, want: expensive sprint to not be called for io.Discard")
		return ""
	}
}

// checkLogContainsFuncStr checks that the logger buffer logBuf contains
// logFuncStr.
func checkLogContainsFuncStr(t *testing.T, logBuf []byte) {
	if !bytes.Contains(logBuf, []byte(logFuncStr)) {
		t.Errorf("got '%v', want logger func to be called and print '%v'", string(logBuf), logFuncStr)
	}
}

// checkBufferWasWrittenAsExpected checks that the log buffer buf was written as expected,
// per the discard, logTYpe, msg, and isJSON arguments.
func checkBufferWasWrittenAsExpected(t *testing.T, buf *bytes.Buffer, discard bool, logType string, msg string, isJSON bool) {
	bts, err := buf.ReadBytes('\n')
	if discard {
		if err == nil {
			t.Fatalf("got '%v', want discard %v to not write", string(bts), logType)
		} else if err != io.EOF {
			t.Fatalf("got '%v', want discard %v buffer to be EOF", err, logType)
		}
	} else {
		if err != nil {
			t.Fatalf("got '%v', want non-discard %v to not error", err, logType)