From 45a56423e4020e8c6a1a67aa49101279ea4eaa2d Mon Sep 17 00:00:00 2001 From: Bertold Kolics Date: Sun, 30 Jun 2024 17:37:35 -0500 Subject: [PATCH 1/3] 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) --- .golangci.yml | 4 ++++ test_data/archive.zip | Bin 0 -> 537 bytes zip_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 test_data/archive.zip create mode 100644 zip_test.go diff --git a/.golangci.yml b/.golangci.yml index 1a2df62..eae7345 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,10 @@ run: linters: enable-all: true disable: + - testpackage + # disabled because of Go 1.19 + - copyloopvar + - intrange # deprecated - gomnd - execinquery diff --git a/test_data/archive.zip b/test_data/archive.zip new file mode 100644 index 0000000000000000000000000000000000000000..585c79f8bb47c059544b6a217a0924a6236743c7 GIT binary patch literal 537 zcmWIWW@h1H0D+lp_aeXyD8bGk!%$qBl#*Gb9~#2Rz`XHtW7=ULF0J5ZU}X8q$iTn? zQW=0^z{4wSHvB+Q5SB+6kda!Hs+(D?o0ykh0%Yr^W#**nl~k0#4X(m8*e}0CA-6O+ zLjj~&kqco@E6}0u)jdy&0PO%_E}%U@u8uCgu5cr}fX1!FG}5cIxI`gQp&%zQIW;3c zCndE=4`?nUlN>XyAdmo>4*~)VZyiB2JV00>0fG@2sD_~i2gER7z%eXo1epdk40qrV mwgfXQkS$pWv;;Y1faZZh2E);;Y(Rw!%s|KlbR8qeg$w`}FLn3; literal 0 HcmV?d00001 diff --git a/zip_test.go b/zip_test.go new file mode 100644 index 0000000..5d15159 --- /dev/null +++ b/zip_test.go @@ -0,0 +1,40 @@ +package xtractr + +import ( + _ "embed" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +//go:embed test_data/archive.zip +var zipData []byte + +func TestZip(t *testing.T) { + t.Parallel() + const ( + testDataSize = 35 + testFileCount = 3 + ) + + name, err := os.MkdirTemp(".", "xtractr_test_*_data") + require.NoError(t, err, "creating temp directory failed") + defer os.RemoveAll(name) + + zipTestFile := filepath.Join(name, "archive.zip") + err = os.WriteFile(zipTestFile, zipData, 0o600) + require.NoError(t, err) + + size, files, err := ExtractZIP(&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) +} From f59714eee0ec96b855ee45be57257d20e5917e84 Mon Sep 17 00:00:00 2001 From: Bertold Kolics Date: Sun, 30 Jun 2024 18:10:29 -0500 Subject: [PATCH 2/3] PR feedback --- .golangci.yml | 1 - test_data/archive.zip | Bin 537 -> 0 bytes zip_test.go | 44 +++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 10 deletions(-) delete mode 100644 test_data/archive.zip diff --git a/.golangci.yml b/.golangci.yml index eae7345..f7cff1d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,7 +4,6 @@ run: linters: enable-all: true disable: - - testpackage # disabled because of Go 1.19 - copyloopvar - intrange diff --git a/test_data/archive.zip b/test_data/archive.zip deleted file mode 100644 index 585c79f8bb47c059544b6a217a0924a6236743c7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 537 zcmWIWW@h1H0D+lp_aeXyD8bGk!%$qBl#*Gb9~#2Rz`XHtW7=ULF0J5ZU}X8q$iTn? zQW=0^z{4wSHvB+Q5SB+6kda!Hs+(D?o0ykh0%Yr^W#**nl~k0#4X(m8*e}0CA-6O+ zLjj~&kqco@E6}0u)jdy&0PO%_E}%U@u8uCgu5cr}fX1!FG}5cIxI`gQp&%zQIW;3c zCndE=4`?nUlN>XyAdmo>4*~)VZyiB2JV00>0fG@2sD_~i2gER7z%eXo1epdk40qrV mwgfXQkS$pWv;;Y1faZZh2E);;Y(Rw!%s|KlbR8qeg$w`}FLn3; diff --git a/zip_test.go b/zip_test.go index 5d15159..838bc2d 100644 --- a/zip_test.go +++ b/zip_test.go @@ -1,6 +1,7 @@ -package xtractr +package xtractr_test import ( + "archive/zip" _ "embed" "os" "path/filepath" @@ -8,27 +9,51 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golift.io/xtractr" ) -//go:embed test_data/archive.zip -var zipData []byte - func TestZip(t *testing.T) { t.Parallel() const ( - testDataSize = 35 - testFileCount = 3 + 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) - zipTestFile := filepath.Join(name, "archive.zip") - err = os.WriteFile(zipTestFile, zipData, 0o600) + 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, err := ExtractZIP(&XFile{ + size, files, archives, err := xtractr.ExtractFile(&xtractr.XFile{ FilePath: zipTestFile, OutputDir: filepath.Clean(name), FileMode: 0o600, @@ -37,4 +62,5 @@ func TestZip(t *testing.T) { require.NoError(t, err) assert.Equal(t, int64(testDataSize), size) assert.Len(t, files, testFileCount) + assert.Len(t, archives, testArchiveCount) } From b8be982393336b74d629f9c3842cf5f90801300b Mon Sep 17 00:00:00 2001 From: Bertold Kolics Date: Sun, 30 Jun 2024 18:12:33 -0500 Subject: [PATCH 3/3] PR feedback --- .golangci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f7cff1d..1a2df62 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,9 +4,6 @@ run: linters: enable-all: true disable: - # disabled because of Go 1.19 - - copyloopvar - - intrange # deprecated - gomnd - execinquery