1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
package tgz
import (
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"testing"
"github.com/go-git/go-billy/v5/osfs"
)
func TestExtractError(t *testing.T) {
for i, test := range [...]struct {
tgz string
errRgx *regexp.Regexp
}{
{
tgz: "not-found",
errRgx: regexp.MustCompile("open not-found: (The system cannot find the file specified|no such file).*"),
}, {
tgz: filepath.Join("fixtures", "invalid-gzip.tgz"),
errRgx: regexp.MustCompile("gzip: invalid header"),
}, {
tgz: filepath.Join("fixtures", "not-a-tar.tgz"),
errRgx: regexp.MustCompile("unexpected EOF"),
},
} {
com := fmt.Sprintf("%d) tgz path = %s", i, test.tgz)
_, err, cleanup := Extract(osfs.New(""), test.tgz)
defer cleanup() // Clean up temporary dirs.
if err == nil {
t.Errorf("%s: expect an error, but none was returned", com)
} else if errorNotMatch(err, test.errRgx) {
t.Errorf("%s:\n\treceived error: %s\n\texpected regexp: %s\n",
com, err, test.errRgx)
}
}
}
func errorNotMatch(err error, regexp *regexp.Regexp) bool {
return !regexp.MatchString(err.Error())
}
func TestExtract(t *testing.T) {
for i, test := range [...]struct {
tgz string
tree []string
}{
{
tgz: filepath.Join("fixtures", "test-01.tgz"),
tree: []string{
"foo.txt",
},
}, {
tgz: filepath.Join("fixtures", "test-02.tgz"),
tree: []string{
"baz.txt",
"bla.txt",
"foo.txt",
},
}, {
tgz: filepath.Join("fixtures", "test-03.tgz"),
tree: []string{
"bar",
filepath.Join("bar", "baz.txt"),
filepath.Join("bar", "foo.txt"),
"baz",
filepath.Join("baz", "bar"),
filepath.Join("baz", "bar", "foo.txt"),
filepath.Join("baz", "baz"),
filepath.Join("baz", "baz", "baz"),
filepath.Join("baz", "baz", "baz", "foo.txt"),
"foo.txt",
},
},
} {
com := fmt.Sprintf("%d) tgz path = %s", i, test.tgz)
path, err, cleanup := Extract(osfs.New(""), test.tgz)
defer cleanup() // Clean up temporary dirs.
if err != nil {
t.Fatalf("%s: unexpected error extracting: %s", test.tgz, err)
}
obt, err := relativeTree(path.Root())
if err != nil {
t.Errorf("%s: unexpected error calculating relative path: %s", com, err)
}
sort.Strings(test.tree)
if !reflect.DeepEqual(obt, test.tree) {
t.Fatalf("%s:\n\tobtained: %v\n\t expected: %v", com, obt, test.tree)
}
}
}
// relativeTree returns the list of relative paths to the files and
// directories inside a given directory, recursively.
func relativeTree(dir string) ([]string, error) {
dir = filepath.Clean(dir)
absPaths := []string{}
walkFn := func(path string, _ os.FileInfo, _ error) error {
absPaths = append(absPaths, path)
return nil
}
_ = filepath.Walk(dir, walkFn)
return toRelative(absPaths[1:], dir)
}
// toRelative returns the relative paths (form b) of the list of paths in l.
func toRelative(l []string, b string) ([]string, error) {
r := []string{}
for _, p := range l {
rel, err := filepath.Rel(b, p)
if err != nil {
return nil, err
}
r = append(r, rel)
}
return r, nil
}
|