Skip to content

Commit

Permalink
Add test for zip (#65)
Browse files Browse the repository at this point in the history
* Add test for zip

Update linter config to remove warnings as project is on Go 1.19 (copyloopvar, intrange), and disable linter that forces tests to be in a separate package (testpackage)

* PR feedback

* PR feedback
  • Loading branch information
bertold authored Jul 1, 2024
1 parent 4b9d207 commit e089f64
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions zip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package xtractr_test

import (
"archive/zip"
_ "embed"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/xtractr"
)

func TestZip(t *testing.T) {
t.Parallel()
const (
testDataSize = 21
testFileCount = 5
testArchiveCount = 1
)
testFiles := []string{
"README.txt",
"subdir/",
"subdir/subdirfile.txt",
"subdir/level2/",
"subdir/level2/level2file.txt",
}

name, err := os.MkdirTemp(".", "xtractr_test_*_data")
require.NoError(t, err, "creating temp directory failed")
defer os.RemoveAll(name)

zipFile, err := os.Create(filepath.Join(name, "archive.zip"))
require.NoError(t, err)
zipWriter := zip.NewWriter(zipFile)

for _, file := range testFiles {
if file[len(file)-1] == '/' {
_, err = zipWriter.Create(file)
require.NoError(t, err)
} else {
f, err := zipWriter.Create(file)
require.NoError(t, err)
_, err = f.Write([]byte("content"))
require.NoError(t, err)
}
}
err = zipWriter.Close()
require.NoError(t, err)
err = zipFile.Close()
require.NoError(t, err)

zipTestFile := filepath.Join(name, "archive.zip")

size, files, archives, err := xtractr.ExtractFile(&xtractr.XFile{
FilePath: zipTestFile,
OutputDir: filepath.Clean(name),
FileMode: 0o600,
DirMode: 0o700,
})
require.NoError(t, err)
assert.Equal(t, int64(testDataSize), size)
assert.Len(t, files, testFileCount)
assert.Len(t, archives, testArchiveCount)
}

0 comments on commit e089f64

Please sign in to comment.