Files
gocryptotrader/common/file/archive/zip_test.go
Adrian Gallagher a5b638bfb7 GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues

These checks include:
- Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`).
- Replacing `%s` with %q
- Enforcing consistent usage of should/must wording for testify assert/require messages

* Add support for checking backticked string format specifiers and fix issues

* tests: Fix error comparisons

* tests: Replace errors.Is(err, nil) usage with testify and automate check

* refactor: Rename ExtractPort to ExtractPortOrDefault

* tests: Replace assert with require for error handling in multiple test files

* tests: Replace assert with require for error handling and improve assertions in data tests

* tests: Fix typo in assertion message for StreamVol test

* OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions

* OKX: Revert intentional error check

* Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
2025-05-28 12:26:51 +10:00

76 lines
2.3 KiB
Go

package archive
import (
"archive/zip"
"errors"
"io/fs"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUnZip(t *testing.T) {
tempDir := t.TempDir()
zipFile := filepath.Join("..", "..", "..", "testdata", "testdata.zip")
files, err := UnZip(zipFile, tempDir)
if err != nil {
t.Fatal(err)
}
if len(files) != 2 {
t.Fatalf("expected 2 files to be extracted received: %v ", len(files))
}
zipFile = filepath.Join("..", "..", "..", "testdata", "zip-slip.zip")
_, err = UnZip(zipFile, tempDir)
if err == nil {
t.Fatal("Zip() expected to error due to ZipSlip detection but extracted successfully")
}
zipFile = filepath.Join("..", "..", "..", "testdata", "configtest.json")
_, err = UnZip(zipFile, tempDir)
if err == nil {
t.Fatal("Zip() expected to error due to invalid zipfile")
}
}
func TestZip(t *testing.T) {
tempDir := t.TempDir()
outFile := filepath.Join(tempDir, "out.zip")
err := Zip(filepath.Join("..", "..", "..", "testdata", "configtest.json"), outFile)
require.NoError(t, err, "Zip must not error")
o, err := UnZip(outFile, tempDir)
require.NoError(t, err, "UnZip must not error")
assert.Len(t, o, 1, "Should extract 1 file")
folder := filepath.Join("..", "..", "..", "testdata", "gctscript")
outFolderZip := filepath.Join(tempDir, "out_folder.zip")
err = Zip(folder, outFolderZip)
require.NoError(t, err, "Zip must not error")
o, err = UnZip(outFolderZip, tempDir)
require.NoError(t, err, "UnZip must not error")
var found bool
for i := range o {
if filepath.Base(o[i]) == "timer.gct" {
found = true
}
}
assert.True(t, found, "Should find a gctscript in the zip")
assert.GreaterOrEqual(t, len(o), 6, "Should extract at least 6 files")
folder = filepath.Join("..", "..", "..", "testdata", "invalid_file.json")
err = Zip(folder, filepath.Join(tempDir, "invalid.zip"))
assert.ErrorIs(t, err, fs.ErrNotExist, "Zip should error correctly")
addFilesToZip = addFilesToZipTestWrapper
folder = filepath.Join("..", "..", "..", "testdata", "http_mock")
outFolderZip = filepath.Join(tempDir, "error_zip.zip")
err = Zip(folder, outFolderZip)
assert.ErrorContains(t, err, "specific error", "Zip should error correctly")
}
func addFilesToZipTestWrapper(_ *zip.Writer, _ string, _ bool) error {
return errors.New("specific error")
}