Skip to content

Commit

Permalink
add archive of single non-empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Aug 10, 2023
1 parent a4c5960 commit f7d0490
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"archive/zip"
"fmt"
"io"
"os"
)

Expand All @@ -14,8 +16,9 @@ func NewArchiver(archive *os.File) *Archiver {
return &Archiver{Dest: archive, w: zip.NewWriter(archive)}
}

func (a *Archiver) Archive() {
a.w.Create("hello.txt")
func (a *Archiver) Archive(r io.Reader) {
writer, _ := a.w.Create("hello.txt")
fmt.Fprint(writer, r)
a.w.Close()
}

Expand Down
22 changes: 21 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"archive/zip"
"bytes"
"fmt"
"os"
"testing"
Expand All @@ -11,11 +12,12 @@ import (

func TestArchive(t *testing.T) {
t.Run("archives a single empty file called hello.txt", func(t *testing.T) {
file := bytes.NewBufferString("")
archive, cleanup := createTempArchive(t, "testdata/archive.zip")
defer cleanup()

archiver := NewArchiver(archive)
archiver.Archive()
archiver.Archive(file)

archiveReader, err := zip.OpenReader(archive.Name())
assert.NoError(t, err)
Expand All @@ -24,6 +26,24 @@ func TestArchive(t *testing.T) {
assert.Equal(t, 1, len(archiveReader.File))
assert.Equal(t, "hello.txt", archiveReader.File[0].Name)
})

t.Run("archives a single non-empty file called hello.txt", func(t *testing.T) {
file := bytes.NewBufferString("hello, world!")
archive, cleanup := createTempArchive(t, "testdata/archive.zip")
defer cleanup()

archiver := NewArchiver(archive)
archiver.Archive(file)

archiveReader, err := zip.OpenReader(archive.Name())
assert.NoError(t, err)
defer archiveReader.Close()

got := archiveReader.File[0].UncompressedSize64
want := uint64(file.Len())

assert.Equal(t, want, got, "expected %s to have size %d but got %d", "hello.txt", want, got)
})
}

func createTempArchive(t testing.TB, name string) (*os.File, func()) {
Expand Down

0 comments on commit f7d0490

Please sign in to comment.