mirror of
https://github.com/docker/compose.git
synced 2026-02-09 01:59:22 +08:00
assume we receive logs by lines and don't ignore those without EOL
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
committed by
Nicolas De loof
parent
e21a8d6293
commit
4bf2fe9fed
@@ -43,11 +43,17 @@ func (s *splitWriter) Write(b []byte) (int, error) {
|
||||
for {
|
||||
b = s.buffer.Bytes()
|
||||
index := bytes.Index(b, []byte{'\n'})
|
||||
if index < 0 {
|
||||
if index > 0 {
|
||||
line := s.buffer.Next(index + 1)
|
||||
s.consumer(string(line[:len(line)-1]))
|
||||
} else {
|
||||
line := s.buffer.String()
|
||||
s.buffer.Reset()
|
||||
if len(line) > 0 {
|
||||
s.consumer(line)
|
||||
}
|
||||
break
|
||||
}
|
||||
line := s.buffer.Next(index + 1)
|
||||
s.consumer(string(line[:len(line)-1]))
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -28,13 +28,21 @@ func TestSplitWriter(t *testing.T) {
|
||||
w := GetWriter(func(line string) {
|
||||
lines = append(lines, line)
|
||||
})
|
||||
w.Write([]byte("h"))
|
||||
w.Write([]byte("e"))
|
||||
w.Write([]byte("l"))
|
||||
w.Write([]byte("l"))
|
||||
w.Write([]byte("o"))
|
||||
w.Write([]byte("\n"))
|
||||
w.Write([]byte("world!\n"))
|
||||
w.Write([]byte("hello\n"))
|
||||
w.Write([]byte("world\n"))
|
||||
w.Write([]byte("!"))
|
||||
assert.DeepEqual(t, lines, []string{"hello", "world", "!"})
|
||||
|
||||
}
|
||||
|
||||
//nolint:errcheck
|
||||
func TestSplitWriterNoEOL(t *testing.T) {
|
||||
var lines []string
|
||||
w := GetWriter(func(line string) {
|
||||
lines = append(lines, line)
|
||||
})
|
||||
w.Write([]byte("hello\n"))
|
||||
w.Write([]byte("world!"))
|
||||
assert.DeepEqual(t, lines, []string{"hello", "world!"})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user