From 3ac62b559678924aeda07eb7a725c3d382a06ccd Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 22 Nov 2023 16:35:48 +0400 Subject: [PATCH] Writer: initialize the writer on Flush() if wasn't initialized before This allows compressing empty files, because without this change the proper headers were not emitted when reading an empty file via e.g. io.Copy(). Signed-off-by: Nikolay Edigaryev --- testdata/empty.txt | 0 writer.go | 4 ++++ writer_test.go | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 testdata/empty.txt diff --git a/testdata/empty.txt b/testdata/empty.txt new file mode 100644 index 00000000..e69de29b diff --git a/writer.go b/writer.go index 77699f2b..4358adee 100644 --- a/writer.go +++ b/writer.go @@ -150,6 +150,10 @@ func (w *Writer) Flush() (err error) { case writeState: case errorState: return w.state.err + case newState: + if err = w.init(); w.state.next(err) { + return + } default: return nil } diff --git a/writer_test.go b/writer_test.go index 48baede9..379fb13f 100644 --- a/writer_test.go +++ b/writer_test.go @@ -17,6 +17,7 @@ import ( func TestWriter(t *testing.T) { goldenFiles := []string{ + "testdata/empty.txt", "testdata/e.txt", "testdata/gettysburg.txt", "testdata/Mark.Twain-Tom.Sawyer.txt", @@ -75,7 +76,7 @@ func TestWriter(t *testing.T) { t.Errorf("invalid sizes: got %d; want %d", got, want) } - if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) { + if got, want := out.Bytes(), raw; !bytes.Equal(got, want) { t.Fatal("uncompressed data does not match original") }