Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent d8293b3 commit e6e63b6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
6 changes: 2 additions & 4 deletions pkg/io/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ func UnCompress(src string, dst string) error {
switch format {
case Zip:
cmd = exec.Command("unzip", "-qo", src, "-d", dst)
case Gz:
case Gz, TarGz:
cmd = exec.Command("tar", "-xzf", src, "-C", dst)
case Bz2:
cmd = exec.Command("tar", "-xjf", src, "-C", dst)
case Tar:
cmd = exec.Command("tar", "-xf", src, "-C", dst)
case TarGz:
cmd = exec.Command("tar", "-xzf", src, "-C", dst)
case Xz:
cmd = exec.Command("tar", "-xJf", src, "-C", dst)
case SevenZip:
Expand All @@ -119,7 +117,7 @@ func formatArchiveByPath(path string) (FormatArchive, error) {
return Bz2, nil
case ".tar":
return Tar, nil
case ".tar.gz":
case ".tar.gz", ".tgz":
return TarGz, nil
case ".xz":
return Xz, nil
Expand Down
80 changes: 67 additions & 13 deletions pkg/io/io_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io

import (
"github.com/go-rat/utils/debug"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -68,16 +67,18 @@ func (s *IOTestSuite) TestCompress() {
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.bz2"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.tar"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.tar.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.tgz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.xz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.7z"))
s.NoError(err)

s.NoError(Remove("testdata"))
}

func (s *IOTestSuite) TestUnCompress() {
Expand All @@ -95,30 +96,83 @@ func (s *IOTestSuite) TestUnCompress() {
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.bz2"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.tar"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.tar.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.tgz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.xz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.7z"))
s.NoError(err)

err = UnCompress(filepath.Join(abs, "uncompress_test.zip"), filepath.Join(abs, "uncompressed"))
err = UnCompress(filepath.Join(abs, "uncompress_test.zip"), filepath.Join(abs, "uncompressed_zip"))
s.NoError(err)

debug.Dump(ReadDir("testdata"))
debug.Dump(ReadDir(abs))
debug.Dump(ReadDir(filepath.Join(abs, "uncompressed")))
data, err := Read("testdata/uncompressed/uncompress_test1.txt")
data, err := Read("testdata/uncompressed_zip/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)

data, err = Read("testdata/uncompressed/uncompress_test2.txt")
data, err = Read("testdata/uncompressed_zip/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.gz"), filepath.Join(abs, "uncompressed_gz"))
s.NoError(err)
data, err = Read("testdata/uncompressed_gz/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_gz/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.bz2"), filepath.Join(abs, "uncompressed_bz2"))
s.NoError(err)
data, err = Read("testdata/uncompressed_bz2/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_bz2/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.tar"), filepath.Join(abs, "uncompressed_tar"))
s.NoError(err)
data, err = Read("testdata/uncompressed_tar/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_tar/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.tar.gz"), filepath.Join(abs, "uncompressed_tar_gz"))
s.NoError(err)
data, err = Read("testdata/uncompressed_tar_gz/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_tar_gz/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.tgz"), filepath.Join(abs, "uncompressed_tgz"))
s.NoError(err)
data, err = Read("testdata/uncompressed_tgz/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_tgz/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.xz"), filepath.Join(abs, "uncompressed_xz"))
s.NoError(err)
data, err = Read("testdata/uncompressed_xz/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_xz/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.7z"), filepath.Join(abs, "uncompressed_7z"))
s.NoError(err)
data, err = Read("testdata/uncompressed_7z/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_7z/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)

s.NoError(Remove("testdata"))
}

func (s *IOTestSuite) TestRemoveDeletesFileOrDirectory() {
Expand Down

0 comments on commit e6e63b6

Please sign in to comment.