-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |