Skip to content

Commit bc70670

Browse files
committed
Drop control characters from output
Fixes: #138
1 parent 066da07 commit bc70670

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

parser/gotest/internal/collector/collector.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ package collector
44

55
import (
66
"sort"
7+
"strings"
78
"time"
9+
"unicode"
810
)
911

1012
// line is a single line of output captured at some point in time.
1113
type line struct {
1214
Timestamp time.Time
13-
Text string
15+
text string
16+
}
17+
18+
func (l line) SafeText() string {
19+
return strings.Map(func(r rune) rune {
20+
if unicode.IsControl(r) && !unicode.IsSpace(r) {
21+
return -1
22+
}
23+
return r
24+
}, l.text)
1425
}
1526

1627
// Output stores output lines grouped by id. Output can be retrieved for one or
@@ -44,7 +55,7 @@ func (o *Output) Contains(id int) bool {
4455
func (o *Output) Get(id int) []string {
4556
var lines []string
4657
for _, line := range o.m[id] {
47-
lines = append(lines, line.Text)
58+
lines = append(lines, line.SafeText())
4859
}
4960
return lines
5061
}
@@ -61,7 +72,7 @@ func (o *Output) GetAll(ids ...int) []string {
6172
})
6273
var lines []string
6374
for _, line := range output {
64-
lines = append(lines, line.Text)
75+
lines = append(lines, line.SafeText())
6576
}
6677
return lines
6778
}

parser/gotest/internal/collector/collector_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,12 @@ func TestMerge(t *testing.T) {
8989
t.Errorf("Get(2) after Merge(2, 1) incorrect (-want +got):\n%s", diff)
9090
}
9191
}
92+
93+
func TestSafeText(t *testing.T) {
94+
l := line{text: "\tx\x00y\x1bz\n"}
95+
got := l.SafeText()
96+
want := "\txyz\n"
97+
if diff := cmp.Diff(want, got); diff != "" {
98+
t.Errorf("SafeText() for %q incorrect (-want +got):\n%s", l.text, diff)
99+
}
100+
}

testdata/035-report.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<testsuites tests="9">
3-
<testsuite name="package/whitespace" tests="9" failures="0" errors="0" id="0" hostname="hostname" time="0.001" timestamp="2022-01-01T00:00:00Z">
2+
<testsuites tests="11">
3+
<testsuite name="package/whitespace" tests="11" failures="0" errors="0" id="0" hostname="hostname" time="0.001" timestamp="2022-01-01T00:00:00Z">
44
<properties>
55
<property name="go.version" value="1.0"></property>
66
</properties>
@@ -45,6 +45,10 @@ one-newline
4545
two-newlines
4646
two-newlines
4747
two-newlines]]></system-out>
48+
</testcase>
49+
<testcase name="TestControl" classname="package/whitespace" time="0.000">
50+
<system-out><![CDATA[ whitespace_test.go:49: log control
51+
printf control]]></system-out>
4852
</testcase>
4953
<testcase name="TestSubTests" classname="package/whitespace" time="0.000"></testcase>
5054
<testcase name="TestSubTests/TestFlat" classname="package/whitespace" time="0.000">
@@ -89,5 +93,9 @@ two-newlines
8993
two-newlines
9094
two-newlines]]></system-out>
9195
</testcase>
96+
<testcase name="TestSubTests/TestControl" classname="package/whitespace" time="0.000">
97+
<system-out><![CDATA[ whitespace_test.go:49: log control
98+
printf control]]></system-out>
99+
</testcase>
92100
</testsuite>
93101
</testsuites>

testdata/035-whitespace.txt

246 Bytes
Binary file not shown.

testdata/src/whitespace/whitespace_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ func TestWithNewlinesFlat(t *testing.T) {
4545
fmt.Println("two-newlines\ntwo-newlines\ntwo-newlines")
4646
}
4747

48+
func TestControl(t *testing.T) {
49+
t.Logf("log\x00 control")
50+
fmt.Printf("printf\x00 control\n")
51+
}
52+
4853
func TestSubTests(t *testing.T) {
4954
t.Run("TestFlat", TestFlat)
5055
t.Run("TestWithSpace", TestWithSpace)
5156
t.Run("TestWithTab", TestWithTab)
5257
t.Run("TestWithNewlinesFlat", TestWithNewlinesFlat)
58+
t.Run("TestControl", TestControl)
5359
}

0 commit comments

Comments
 (0)