Skip to content

Commit

Permalink
archiver creates a single empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Aug 10, 2023
0 parents commit 04a476f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/pzip

go 1.20

require github.com/alecthomas/assert/v2 v2.3.0

require (
github.com/alecthomas/repr v0.2.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

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

func Archive(archive *os.File, r io.Reader) {
writer := zip.NewWriter(archive)
defer writer.Close()

writer.Create("hello.txt")
}

func main() {

}
42 changes: 42 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"archive/zip"
"fmt"
"os"
"strings"
"testing"

"github.com/alecthomas/assert/v2"
)

func TestArchive(t *testing.T) {
t.Run("archives a single empty file", func(t *testing.T) {
file := strings.NewReader("")
archive, cleanup := createTempArchive(t, "testdata/archive.zip")
defer cleanup()

Archive(archive, file)

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

assert.Equal(t, 1, len(archiveReader.File))
})

}

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

archive, err := os.Create(name)
assert.NoError(t, err, fmt.Sprintf("could not create archive %s: %v", name, err))

cleanup := func() {
archive.Close()
os.RemoveAll(archive.Name())
}

return archive, cleanup
}

0 comments on commit 04a476f

Please sign in to comment.