From d8347310ea7a4ce6897af4cd5097fed83795e7ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 28 Oct 2024 01:36:27 +0800 Subject: [PATCH] fix: test --- pkg/io/compress.go | 7 +++++++ pkg/io/io_test.go | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/io/compress.go b/pkg/io/compress.go index c3cf051f..f286bef6 100644 --- a/pkg/io/compress.go +++ b/pkg/io/compress.go @@ -22,6 +22,9 @@ const ( // Compress 压缩文件 func Compress(dir string, src []string, dst string) error { + if !filepath.IsAbs(dir) || !filepath.IsAbs(dst) { + return errors.New("dir and dst must be absolute path") + } if len(src) == 0 { src = append(src, ".") } @@ -72,6 +75,10 @@ func Compress(dir string, src []string, dst string) error { // UnCompress 解压文件 func UnCompress(src string, dst string) error { + if !filepath.IsAbs(src) || !filepath.IsAbs(dst) { + return errors.New("src and dst must be absolute path") + } + var cmd *exec.Cmd format, err := formatArchiveByPath(src) diff --git a/pkg/io/io_test.go b/pkg/io/io_test.go index 0e453589..8888d92f 100644 --- a/pkg/io/io_test.go +++ b/pkg/io/io_test.go @@ -53,27 +53,31 @@ func (s *IOTestSuite) TestWriteAppendAppendsToFile() { } func (s *IOTestSuite) TestCompress() { - src := []string{"testdata/compress_test1.txt", "testdata/compress_test2.txt"} + src := []string{"compress_test1.txt", "compress_test2.txt"} err := Write(src[0], "File 1", 0644) s.NoError(err) err = Write(src[1], "File 2", 0644) s.NoError(err) - err = Compress("testdata", src, "testdata/compress_test.zip") + abs, err := filepath.Abs("testdata") + s.NoError(err) + err = Compress(abs, src, filepath.Join(abs, "compress_test.zip")) s.NoError(err) } func (s *IOTestSuite) TestUnCompress() { - src := []string{"testdata/uncompress_test1.txt", "testdata/uncompress_test2.txt"} + src := []string{"compress_test1.txt", "compress_test2.txt"} err := Write(src[0], "File 1", 0644) s.NoError(err) err = Write(src[1], "File 2", 0644) s.NoError(err) - err = Compress("testdata", src, "testdata/uncompress_test.zip") + abs, err := filepath.Abs("testdata") + s.NoError(err) + err = Compress(abs, src, filepath.Join(abs, "compress_test.zip")) s.NoError(err) - err = UnCompress("testdata/uncompress_test.zip", "testdata/uncompressed") + err = UnCompress(filepath.Join(abs, "compress_test.zip"), filepath.Join(abs, "uncompressed")) s.NoError(err) data, err := Read("testdata/uncompressed/uncompress_test1.txt")