File: tgz_test.go

package info (click to toggle)
golang-github-alcortesm-tgz 0.0~git20161220.9c5fe88-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 96 kB
  • sloc: makefile: 3
file content (135 lines) | stat: -rw-r--r-- 2,883 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
package tgz

import (
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"regexp"
	"sort"
	"testing"
)

func TestExtractError(t *testing.T) {
	for i, test := range [...]struct {
		tgz    string
		errRgx *regexp.Regexp
	}{
		{
			tgz:    "not-found",
			errRgx: regexp.MustCompile("open not-found: no such file .*"),
		}, {
			tgz:    "fixtures/invalid-gzip.tgz",
			errRgx: regexp.MustCompile("gzip: invalid header"),
		}, {
			tgz:    "fixtures/not-a-tar.tgz",
			errRgx: regexp.MustCompile("unexpected EOF"),
		},
	} {
		com := fmt.Sprintf("%d) tgz path = %s", i, test.tgz)
		path, err := Extract(test.tgz)
		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)
		}

		if path != "" {
			if err = os.RemoveAll(path); err != nil {
				t.Fatalf("%s: cannot remove temp directory: %s", com, err)
			}
		}
	}
}

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: "fixtures/test-01.tgz",
			tree: []string{
				"foo.txt",
			},
		}, {
			tgz: "fixtures/test-02.tgz",
			tree: []string{
				"baz.txt",
				"bla.txt",
				"foo.txt",
			},
		}, {
			tgz: "fixtures/test-03.tgz",
			tree: []string{
				"bar",
				"bar/baz.txt",
				"bar/foo.txt",
				"baz",
				"baz/bar",
				"baz/bar/foo.txt",
				"baz/baz",
				"baz/baz/baz",
				"baz/baz/baz/foo.txt",
				"foo.txt",
			},
		},
	} {
		com := fmt.Sprintf("%d) tgz path = %s", i, test.tgz)

		path, err := Extract(test.tgz)
		if err != nil {
			t.Fatalf("%s: unexpected error extracting: %s", err)
		}

		obt, err := relativeTree(path)
		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)
		}

		err = os.RemoveAll(path)
		if err != nil {
			t.Fatalf("%s: unexpected error removing temporal path: %s", com, err)
		}
	}
}

// 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
}