-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse: fix a panic when printing a level without initalizing a level …
…formatter
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package parse | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/logrusorgru/aurora/v3" | ||
) | ||
|
||
func FuzzReadLog(f *testing.F) { | ||
f.Add([]byte(`{}`)) | ||
f.Add([]byte(`{"ts": 1234, "level": "info", "msg": "hello"}\n`)) | ||
f.Add([]byte(`{"ts": 1234, "level": "info", "msg": "hello"}\n` + | ||
`{"ts": 1235, "level": "warn", "msg": "line 2"}`)) | ||
f.Add([]byte(`{"ts": 1234, "level": "info", "msg": "hello"}\n{}\n`)) | ||
jq, err := CompileJQ(".") | ||
if err != nil { | ||
f.Fatalf("jq: %v", err) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, in []byte) { | ||
inbuf := bytes.NewReader(in) | ||
ins := &InputSchema{ | ||
Strict: false, | ||
} | ||
errbuf := new(bytes.Buffer) | ||
outs := &OutputSchema{ | ||
Formatter: &DefaultOutputFormatter{ | ||
Aurora: aurora.NewAurora(true), | ||
}, | ||
EmitErrorFn: func(msg string) { | ||
errbuf.WriteString(msg) | ||
errbuf.WriteString("\n") | ||
}, | ||
} | ||
outbuf := new(bytes.Buffer) | ||
if _, err := ReadLog(inbuf, outbuf, ins, outs, jq); err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, want := bytes.Count(outbuf.Bytes(), []byte("\n")), bytes.Count(in, []byte("\n")); got < want { | ||
t.Errorf("output: line count:\n got: %v\n want: >=%v", got, want) | ||
} | ||
if errbuf.Len() > 0 { | ||
t.Logf("errors: %v", errbuf.String()) | ||
} | ||
}) | ||
} |
2 changes: 2 additions & 0 deletions
2
...estdata/fuzz/FuzzReadLog/13d14f3a75b99f154b8bc6a3e2032b22446430e5a905f99d0d9cb673c3feda15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("{\"\":0}") |