1
0

revert flushing underlying writer

This commit is contained in:
Arpad Ryszka 2025-11-01 20:59:45 +01:00
parent a77a4e4b52
commit 0569369ef5
2 changed files with 6 additions and 32 deletions

14
lib.go
View File

@ -140,15 +140,6 @@ func (w *Writer) flush() error {
return w.err return w.err
} }
if f, ok := w.out.(interface{ Flush() error }); ok {
if err := f.Flush(); err != nil {
w.err = err
return w.err
}
} else if f, ok := w.out.(interface{ Flush() }); ok {
f.Flush()
}
return nil return nil
} }
@ -169,10 +160,7 @@ func (w *Writer) WriteRune(r rune) (int, error) {
// Flush makes the underlying editors to release their associated state, and writes out the resulting text to // Flush makes the underlying editors to release their associated state, and writes out the resulting text to
// the underlying io.Writer, but first passes it to the subsequent editors for editing. When the used editor // the underlying io.Writer, but first passes it to the subsequent editors for editing. When the used editor
// instances comply with the expectations of the Editor interface, the writer will have a fresh state and can be // instances comply with the expectations of the Editor interface, the writer will have a fresh state and can be
// reused for further editing. // reused for further editing. The Flush method of underlying writers is not called.
//
// If the underlying io.Writer also implements Flusher (Flush() error or Flush()), then it will be implicitly
// called.
func (w *Writer) Flush() error { func (w *Writer) Flush() error {
return w.flush() return w.flush()
} }

View File

@ -139,7 +139,7 @@ func TestFailingWriter(t *testing.T) {
} }
func TestFlush(t *testing.T) { func TestFlush(t *testing.T) {
t.Run("with err succeed", func(t *testing.T) { t.Run("with err", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
fw := flusherWriter{out: &b} fw := flusherWriter{out: &b}
w := textedit.New(&fw, textedit.Replace("o", "e")) w := textedit.New(&fw, textedit.Replace("o", "e"))
@ -155,22 +155,8 @@ func TestFlush(t *testing.T) {
t.Fatal(b.String()) t.Fatal(b.String())
} }
if !fw.flushed { if fw.flushed {
t.Fatal("failed to flush underlying flusher") t.Fatal("should not flush")
}
})
t.Run("with err fail", func(t *testing.T) {
var b bytes.Buffer
fw := flusherWriter{out: &b}
w := textedit.New(&fw, textedit.Replace("o", "e"))
if _, err := w.Write([]byte("foo bar")); err != nil {
t.Fatal(err)
}
fw.fail = true
if err := w.Flush(); !errors.Is(err, errTest) {
t.Fatal("failed to fail with the right error", err)
} }
}) })
@ -190,8 +176,8 @@ func TestFlush(t *testing.T) {
t.Fatal(b.String()) t.Fatal(b.String())
} }
if !fw.flushed { if fw.flushed {
t.Fatal("failed to flush underlying flusher") t.Fatal("should not flush")
} }
}) })
} }