Skip to content

Commit

Permalink
SootWrapper release archive changed to one file
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Nov 20, 2024
1 parent dcde316 commit 7f11131
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
9 changes: 7 additions & 2 deletions internal/io/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io

import (
"encoding/base64"
"fmt"
"path"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func (arc *Archive) ZipFile(sourcePath string, targetPath string, zippedName str
}
defer fs.CloseFile(zipFile)
zipWriter := zip.NewWriter(zipFile)
defer zip.CloseWriter(zipWriter)
defer zip.CloseWriter(zipWriter) //nolint

info, err := fs.StatFile(zipFile)
if err != nil {
Expand Down Expand Up @@ -88,7 +89,11 @@ func (arc *Archive) UnzipFile(sourcePath string, targetPath string) error {
}
defer arc.zip.CloseReader(r) //nolint

f := r.File[1] //TODO: Change to first file and error-check for multiple once sootwrapper builds only one
if len(r.File) != 1 {
return fmt.Errorf("cannot unzip archive which does not contain exactly one file")
}

f := r.File[0]
outFile, err := arc.fs.Create(targetPath)
if err != nil {
return err
Expand Down
26 changes: 21 additions & 5 deletions internal/io/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestUnzipFileReadFileError(t *testing.T) {

func TestUnzipFileCreateError(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
File: []*zip.File{nil},
}
readCloser := zip.ReadCloser{Reader: reader} //nolint
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf("%s", t.Name())}
Expand All @@ -223,7 +223,7 @@ func TestUnzipFileCreateError(t *testing.T) {

func TestUnzipFileOpenError(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
File: []*zip.File{nil},
}
readCloser := zip.ReadCloser{Reader: reader} //nolint
fsMock := ioTestData.FileSystemMock{}
Expand All @@ -237,10 +237,8 @@ func TestUnzipFileOpenError(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, err.Error(), t.Name())
}

func TestUnzipFileCopyError(t *testing.T) {
r, err := zipStruct.OpenReader("testdata/text.zip")
r.File = append(r.File, r.File[0]) // Hack solution (:
assert.NoError(t, err)
defer zipStruct.CloseReader(r) //nolint

Expand All @@ -251,8 +249,26 @@ func TestUnzipFileCopyError(t *testing.T) {
fs: fsMock,
zip: zipMock,
}
fmt.Println(t.Name())
err = a.UnzipFile("", "")
assert.Error(t, err)
assert.Equal(t, err.Error(), t.Name())
}

func TestUnzipFileNotSingleFile(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
}
readCloser := zip.ReadCloser{Reader: reader} //nolint
defer zipStruct.CloseReader(&readCloser) //nolint

fsMock := ioTestData.FileSystemMock{}
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser}
a := Archive{
workingDirectory: ".",
fs: fsMock,
zip: zipMock,
}
err := a.UnzipFile("", "")
assert.Error(t, err)
assert.Equal(t, err.Error(), "cannot unzip archive which does not contain exactly one file")
}

0 comments on commit 7f11131

Please sign in to comment.