@@ -3,10 +3,13 @@ package executor
33import (
44 "bytes"
55 "context"
6+ "encoding/json"
7+ "fmt"
68 "io"
79 "math/rand/v2"
810 "os"
911 "regexp"
12+ "strings"
1013 "testing"
1114 "time"
1215
@@ -69,7 +72,6 @@ func TestRunAndLogLines(t *testing.T) {
6972
7073 _ , err = ex .RunAndLogLines (context .Background (), map [string ]string {"a" : "b" })
7174 assert .NoError (t , err )
72-
7375 reg := regexp .MustCompile (`{"level":"info","msg":"hook result","a":"b","hook":{"truncated":".*:truncated"},"output":"stdout","proxyJsonLog":true,"time":"2006-01-02T15:04:05Z"` )
7476 assert .Regexp (t , reg , buf .String ())
7577
@@ -105,9 +107,26 @@ func TestRunAndLogLines(t *testing.T) {
105107
106108 _ , err := ex .RunAndLogLines (context .Background (), map [string ]string {"a" : "b" })
107109 assert .NoError (t , err )
110+ lines := strings .Split (strings .TrimSpace (buf .String ()), "\n " )
108111
109- assert .Equal (t , buf .String (), `{"level":"debug","msg":"json log line not map[string]interface{}","source":"executor/executor.go:211","a":"b","line":["a","b","c"],"output":"stdout","time":"2006-01-02T15:04:05Z"}` + "\n " +
110- `{"level":"info","msg":"[\"a\",\"b\",\"c\"]\n","source":"executor/executor.go:214","a":"b","output":"stdout","time":"2006-01-02T15:04:05Z"}` + "\n " )
112+ var debugLine map [string ]interface {}
113+ err = json .Unmarshal ([]byte (lines [0 ]), & debugLine )
114+ assert .NoError (t , err )
115+ assert .Equal (t , "debug" , debugLine ["level" ])
116+ assert .Equal (t , "json log line not map[string]interface{}" , debugLine ["msg" ])
117+ assert .Equal (t , []interface {}{"a" , "b" , "c" }, debugLine ["line" ])
118+ assert .Equal (t , "b" , debugLine ["a" ])
119+ assert .Equal (t , "stdout" , debugLine ["output" ])
120+ assert .Equal (t , "2006-01-02T15:04:05Z" , debugLine ["time" ])
121+
122+ var infoLine map [string ]interface {}
123+ err = json .Unmarshal ([]byte (lines [1 ]), & infoLine )
124+ assert .NoError (t , err )
125+ assert .Equal (t , "info" , infoLine ["level" ])
126+ assert .Equal (t , "[\" a\" ,\" b\" ,\" c\" ]\n " , infoLine ["msg" ])
127+ assert .Equal (t , "b" , infoLine ["a" ])
128+ assert .Equal (t , "stdout" , infoLine ["output" ])
129+ assert .Equal (t , "2006-01-02T15:04:05Z" , infoLine ["time" ])
111130 buf .Reset ()
112131 })
113132
189208
190209 buf .Reset ()
191210 })
211+
212+ t .Run ("multiline json several logs" , func (t * testing.T ) {
213+ logger .SetLevel (log .LevelInfo )
214+ arg := `
215+ {"a":"b","c":"d"}
216+ {"b":"c","d":"a"}
217+ {"c":"d","a":"b"}
218+ {"d":"a","b":"c"}
219+ plain text
220+ {
221+ "a":"b",
222+ "c":"d"
223+ }`
224+ ex := NewExecutor ("" , "echo" , []string {arg }, []string {}).
225+ WithLogProxyHookJSON (true ).
226+ WithLogger (logger )
227+
228+ _ , err := ex .RunAndLogLines (context .Background (), map [string ]string {"foor" : "baar" })
229+ assert .NoError (t , err )
230+ fmt .Println ("actual" , buf .String ())
231+ expected := ""
232+ expected += `{"level":"info","msg":"hook result","foor":"baar","hook_a":"b","hook_c":"d","output":"stdout","proxyJsonLog":true,"time":"2006-01-02T15:04:05Z"}` + "\n "
233+ expected += `{"level":"info","msg":"hook result","foor":"baar","hook_b":"c","hook_d":"a","output":"stdout","proxyJsonLog":true,"time":"2006-01-02T15:04:05Z"}` + "\n "
234+ expected += `{"level":"info","msg":"hook result","foor":"baar","hook_a":"b","hook_c":"d","output":"stdout","proxyJsonLog":true,"time":"2006-01-02T15:04:05Z"}` + "\n "
235+ expected += `{"level":"info","msg":"hook result","foor":"baar","hook_b":"c","hook_d":"a","output":"stdout","proxyJsonLog":true,"time":"2006-01-02T15:04:05Z"}` + "\n "
236+ expected += `{"level":"info","msg":"plain text","foor":"baar","output":"stdout","time":"2006-01-02T15:04:05Z"}` + "\n "
237+ expected += `{"level":"info","msg":"hook result","foor":"baar","hook_a":"b","hook_c":"d","output":"stdout","proxyJsonLog":true,"time":"2006-01-02T15:04:05Z"}` + "\n "
238+ assert .Equal (t , expected , buf .String ())
239+
240+ buf .Reset ()
241+ })
192242}
193243
194244var letterRunes = []rune ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" )
0 commit comments