File tree Expand file tree Collapse file tree 3 files changed +27
-21
lines changed
parser/gotest/internal/collector Expand file tree Collapse file tree 3 files changed +27
-21
lines changed Original file line number Diff line number Diff line change 3
3
package junit
4
4
5
5
import (
6
- "bytes"
7
6
"encoding/xml"
8
7
"fmt"
9
8
"strings"
10
9
"time"
10
+ "unicode"
11
11
12
12
"github.com/jstemmer/go-junit-report/v2/gtr"
13
13
)
@@ -236,16 +236,31 @@ func formatDuration(d time.Duration) string {
236
236
}
237
237
238
238
// formatOutput combines the lines from the given output into a single string.
239
- func formatOutput (output []string , _ int ) string {
240
- buf := bytes .NewBufferString ("" )
241
- for i , o := range output {
242
- err := xml .EscapeText (buf , []byte (o ))
243
- if err != nil {
244
- return "formatOutput: " + err .Error ()
239
+ func formatOutput (output []string ) string {
240
+ return escapeIllegalChars (strings .Join (output , "\n " ))
241
+ }
242
+
243
+ func escapeIllegalChars (str string ) string {
244
+ return strings .Map (func (r rune ) rune {
245
+ if unicode .IsSpace (r ) {
246
+ return r
245
247
}
246
- if i < len ( output ) - 1 {
247
- buf . WriteString ( " \n " )
248
+ if isInCharacterRange ( r ) {
249
+ return r
248
250
}
249
- }
250
- return buf .String ()
251
+ return '�'
252
+ }, str )
253
+ }
254
+
255
+ // Decide whether the given rune is in the XML Character Range, per
256
+ // the Char production of https://www.xml.com/axml/testaxml.htm,
257
+ // Section 2.2 Characters.
258
+ // Form: encoding/xml/xml.go
259
+ func isInCharacterRange (r rune ) (inrange bool ) {
260
+ return r == 0x09 ||
261
+ r == 0x0A ||
262
+ r == 0x0D ||
263
+ r >= 0x20 && r <= 0xD7FF ||
264
+ r >= 0xE000 && r <= 0xFFFD ||
265
+ r >= 0x10000 && r <= 0x10FFFF
251
266
}
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ func TestCreateFromReport(t *testing.T) {
81
81
Name : "TestEscapeOutput" ,
82
82
Classname : "package/name" ,
83
83
Time : "0.000" ,
84
- SystemOut : & Output {Data : `��� 	\` },
84
+ SystemOut : & Output {Data : "� \v \f \t \\ " },
85
85
},
86
86
{
87
87
Name : "TestFail" ,
Original file line number Diff line number Diff line change @@ -115,12 +115,3 @@ func TestActiveID(t *testing.T) {
115
115
}
116
116
117
117
}
118
-
119
- func TestSafeText (t * testing.T ) {
120
- l := line {text : "\t x\x00 y\x1b z\n " }
121
- got := l .SafeText ()
122
- want := "\t xyz\n "
123
- if diff := cmp .Diff (want , got ); diff != "" {
124
- t .Errorf ("SafeText() for %q incorrect (-want +got):\n %s" , l .text , diff )
125
- }
126
- }
You can’t perform that action at this time.
0 commit comments