Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Implemented numFiles for tar archives, added numFiles test cases and …
Browse files Browse the repository at this point in the history
…improved test errors.
  • Loading branch information
martinplaner committed Jun 7, 2017
1 parent ca1884d commit db39b63
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 25 deletions.
103 changes: 83 additions & 20 deletions archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ package archive_test
// This may change in the future if I find a way to abstract this away.

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"errors"

"github.com/martinplaner/gunarchiver/progress"

"github.com/martinplaner/gunarchiver/archive"
Expand All @@ -25,28 +24,84 @@ import (
)

var testDataDir = "testdata"
var basenames = []string{"single", "multiple", "deep", "subfolder"}
var testFiles = []struct {
basename string
numFiles int
}{
{
"single",
1,
},
{
"multiple",
3,
},
{
"deep",
7,
},
{
"subfolder",
7,
},
}
var formats = map[string][]string{
"zip": {"zip"},
"tar.gz": {"tar.gz"},
"rar": {"rar"},
}

func TestArchives(t *testing.T) {
func TestNumFiles(t *testing.T) {
for _, exts := range formats {
for _, ext := range exts {
for _, basename := range basenames {
t.Run(basename+"."+ext, func(t *testing.T) {
if err := testArchive(basename, ext); err != nil {
t.Error(err)
for _, testFile := range testFiles {
filename := testFile.basename + "." + ext
t.Run(filename, func(t *testing.T) {
if err := testNumFiles(filename, testFile.numFiles); err != nil {
t.Errorf("TestNumFiles - %s: %v", filename, err)
}
})
}
}
}
}

func testArchive(basename, ext string) error {
func testNumFiles(filename string, expected int) error {
path := filepath.Join(testDataDir, filename)
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

a, _, err := archive.Decode(file)
if err != nil {
return err
}

if got := a.NumFiles(); got != expected {
return fmt.Errorf("numFiles mismatch: got %d, expected %d", got, expected)
}

return nil
}

func TestExtraction(t *testing.T) {
for _, exts := range formats {
for _, ext := range exts {
for _, testFile := range testFiles {
filename := testFile.basename + "." + ext
t.Run(filename, func(t *testing.T) {
if err := testExtraction(testFile.basename, ext); err != nil {
t.Errorf("TestExtraction - %s: %v", filename, err)
}
})
}
}
}
}

func testExtraction(basename, ext string) error {
filename := basename + "." + ext
path := filepath.Join(testDataDir, filename)
comparePath := filepath.Join(testDataDir, basename)
Expand Down Expand Up @@ -74,39 +129,47 @@ func testArchive(basename, ext string) error {
return err
}

if !compareDirs(tempDir, comparePath) {
return errors.New("dirs to not match!")
if err := compareDirs(tempDir, comparePath); err != nil {
return fmt.Errorf("error comparing dir structure: %v", err)
}

return nil
}

func compareDirs(a, b string) bool {
func compareDirs(a, b string) error {
aFiles, aErr := getFileList(a)
bFiles, bErr := getFileList(b)

if aErr != nil || bErr != nil {
return false
if aErr != nil {
return aErr
}
if bErr != nil {
return bErr
}

if len(aFiles) != len(bFiles) {
return false
aLen := len(aFiles)
bLen := len(bFiles)
if aLen != bLen {
return fmt.Errorf("file list length does not match: %d != %d", aLen, bLen)
}

for i := range aFiles {
aRel, aErr := filepath.Rel(a, aFiles[i])
bRel, bErr := filepath.Rel(b, bFiles[i])

if aErr != nil || bErr != nil {
return false
if aErr != nil {
return aErr
}
if bErr != nil {
return bErr
}

if aRel != bRel {
return false
return fmt.Errorf("relative file paths do not match")
}
}

return true
return nil
}

func getFileList(path string) ([]string, error) {
Expand Down
21 changes: 16 additions & 5 deletions archive/tar/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,23 @@ func decodeTarGz(file *os.File) (archive.Archive, error) {

tr := tar.NewReader(r)

// TODO: implement numFiles
count := 0
for {
_, err := tr.Next()
if err != nil {
break
}
count++
}

return &tarArchive{
file: file,
tar: tr,
}, nil
a := &tarArchive{
file: file,
tar: tr,
numFiles: count,
}
a.Reset() // necessary because file counting iterates the whole archive

return a, nil
}

func init() {
Expand Down

0 comments on commit db39b63

Please sign in to comment.