-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
7 changed files
with
120 additions
and
50 deletions.
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
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
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,31 @@ | ||
package zeaburpack | ||
|
||
import ( | ||
"github.com/containerd/console" | ||
) | ||
|
||
// HandledWriter is a console-compatible writer that copies the output | ||
// to the specified log and calls handler with the log. | ||
type HandledWriter struct { | ||
console.File | ||
handler func(log string) | ||
} | ||
|
||
// NewHandledWriter creates a new HandledWriter. | ||
func NewHandledWriter(w console.File, handler *func(log string)) console.File { | ||
if handler == nil { | ||
return w | ||
} | ||
|
||
return &HandledWriter{ | ||
File: w, | ||
handler: *handler, | ||
} | ||
} | ||
|
||
func (h HandledWriter) Write(p []byte) (n int, err error) { | ||
go h.handler(string(p)) | ||
return h.File.Write(p) | ||
} | ||
|
||
var _ console.File = (*HandledWriter)(nil) |
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,58 @@ | ||
package zeaburpack | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/containerd/console" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockWriter struct { | ||
written string | ||
} | ||
|
||
func (m *MockWriter) Read(p []byte) (n int, err error) { | ||
n = copy(p, m.written) | ||
return | ||
} | ||
|
||
func (m *MockWriter) Close() error { | ||
return nil | ||
} | ||
|
||
func (m *MockWriter) Fd() uintptr { | ||
return 0 | ||
} | ||
|
||
func (m *MockWriter) Name() string { | ||
return "mock" | ||
} | ||
|
||
func (m *MockWriter) Write(p []byte) (n int, err error) { | ||
m.written = string(p) | ||
return len(p), nil | ||
} | ||
|
||
func (m *MockWriter) Flush() {} | ||
|
||
var _ console.File = &MockWriter{} | ||
|
||
func TestHandledWriter_Write(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockWriter := &MockWriter{} | ||
receivedLog := "" | ||
handler := func(log string) { | ||
receivedLog = log | ||
} | ||
|
||
handledWriter := NewHandledWriter(mockWriter, &handler) | ||
|
||
_, _ = handledWriter.Write([]byte("test")) | ||
// wait for 3 ms – `go h.handler(string(p))` is async | ||
time.Sleep(3 * time.Millisecond) | ||
|
||
assert.Equal(t, "test", mockWriter.written) | ||
assert.Equal(t, "test", receivedLog) | ||
} |