Skip to content

Commit 82e28ad

Browse files
committed
slog: TextHandler.appendSource: quote entire value, fix test
If the filename value of the source attribute needs quoting, quote the entire "file:line". Previously we were just quoting the filename. Also, fix the test for source to accept quoted values. Change-Id: Ief560123633983ba6ab8679c52e8605714d9f193 Reviewed-on: https://go-review.googlesource.com/c/exp/+/430076 Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0d63173 commit 82e28ad

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

slog/text_handler.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ func (a *textAppender) appendTime(t time.Time) error {
105105
}
106106

107107
func (a *textAppender) appendSource(file string, line int) {
108-
a.appendString(file)
109-
a.buf().WriteByte(':')
110-
itoa((*[]byte)(a.buf()), line, -1)
108+
if needsQuoting(file) {
109+
a.appendString(file + ":" + strconv.Itoa(line))
110+
} else {
111+
// common case: no quoting needed.
112+
a.appendString(file)
113+
a.buf().WriteByte(':')
114+
itoa((*[]byte)(a.buf()), line, -1)
115+
}
111116
}
112117

113118
func (ap *textAppender) appendAttrValue(a Attr) error {

slog/text_handler_test.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
"testing"
1515
"time"
16+
17+
"golang.org/x/exp/slog/internal/buffer"
1618
)
1719

1820
var testTime = time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)
@@ -89,21 +91,49 @@ func (t text) MarshalText() ([]byte, error) {
8991
return []byte(fmt.Sprintf("text{%q}", t.s)), nil
9092
}
9193

94+
func TestTextAppendSource(t *testing.T) {
95+
var buf []byte
96+
app := (*textAppender)((*buffer.Buffer)(&buf))
97+
for _, test := range []struct {
98+
file string
99+
want string
100+
}{
101+
{"a/b.go", "a/b.go:1"},
102+
{"a b.go", `"a b.go:1"`},
103+
{`C:\windows\b.go`, `C:\windows\b.go:1`},
104+
} {
105+
app.appendSource(test.file, 1)
106+
got := string(buf)
107+
if got != test.want {
108+
t.Errorf("%s:\ngot %s\nwant %s", test.file, got, test.want)
109+
}
110+
buf = buf[:0]
111+
}
112+
}
113+
92114
func TestTextHandlerSource(t *testing.T) {
93115
var buf bytes.Buffer
94116
h := HandlerOptions{AddSource: true}.NewTextHandler(&buf)
95117
r := MakeRecord(testTime, InfoLevel, "m", 2)
96118
if err := h.Handle(r); err != nil {
97119
t.Fatal(err)
98120
}
99-
got := buf.String()
100-
wantRE := `source=([A-Z]:)?[^:]+text_handler_test\.go:\d+ msg`
101-
matched, err := regexp.MatchString(wantRE, got)
102-
if err != nil {
103-
t.Fatal(err)
121+
if got := buf.String(); !sourceRegexp.MatchString(got) {
122+
t.Errorf("got\n%q\nwanted to match %s", got, sourceRegexp)
104123
}
105-
if !matched {
106-
t.Errorf("got\n%q\nwanted to match %s", got, wantRE)
124+
}
125+
126+
var sourceRegexp = regexp.MustCompile(`source="?([A-Z]:)?[^:]+text_handler_test\.go:\d+"? msg`)
127+
128+
func TestSourceRegexp(t *testing.T) {
129+
for _, s := range []string{
130+
`source=/tmp/path/to/text_handler_test.go:23 msg=m`,
131+
`source=C:\windows\path\text_handler_test.go:23 msg=m"`,
132+
`source="/tmp/tmp.XcGZ9cG9Xb/with spaces/exp/slog/text_handler_test.go:95" msg=m`,
133+
} {
134+
if !sourceRegexp.MatchString(s) {
135+
t.Errorf("failed to match %s", s)
136+
}
107137
}
108138
}
109139

0 commit comments

Comments
 (0)