16 "github.com/mitchellh/go-testing-interface"
19 // TestDecompressCase is a single test case for testing decompressors
20 type TestDecompressCase struct {
21 Input string // Input is the complete path to the input file
22 Dir bool // Dir is whether or not we're testing directory mode
23 Err bool // Err is whether we expect an error or not
24 DirList []string // DirList is the list of files for Dir mode
25 FileMD5 string // FileMD5 is the expected MD5 for a single file
26 Mtime *time.Time // Mtime is the optionally expected mtime for a single file (or all files if in Dir mode)
29 // TestDecompressor is a helper function for testing generic decompressors.
30 func TestDecompressor(t testing.T, d Decompressor, cases []TestDecompressCase) {
33 for _, tc := range cases {
34 t.Logf("Testing: %s", tc.Input)
36 // Temporary dir to store stuff
37 td, err := ioutil.TempDir("", "getter")
39 t.Fatalf("err: %s", err)
42 // Destination is always joining result so that we have a new path
43 dst := filepath.Join(td, "subdir", "result")
45 // We use a function so defers work
47 defer os.RemoveAll(td)
50 err := d.Decompress(dst, tc.Input, tc.Dir)
51 if (err != nil) != tc.Err {
52 t.Fatalf("err %s: %s", tc.Input, err)
58 // If it isn't a directory, then check for a single file
60 fi, err := os.Stat(dst)
62 t.Fatalf("err %s: %s", tc.Input, err)
65 t.Fatalf("err %s: expected file, got directory", tc.Input)
68 actual := testMD5(t, dst)
69 expected := tc.FileMD5
70 if actual != expected {
71 t.Fatalf("err %s: expected MD5 %s, got %s", tc.Input, expected, actual)
76 actual := fi.ModTime()
77 if tc.Mtime.Unix() > 0 {
79 if actual != expected {
80 t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), dst, actual.String())
82 } else if actual.Unix() <= 0 {
83 t.Fatalf("err %s: expected mtime to be > 0, got '%s'", actual.String())
90 // Convert expected for windows
91 expected := tc.DirList
92 if runtime.GOOS == "windows" {
93 for i, v := range expected {
94 expected[i] = strings.Replace(v, "/", "\\", -1)
98 // Directory, check for the correct contents
99 actual := testListDir(t, dst)
100 if !reflect.DeepEqual(actual, expected) {
101 t.Fatalf("bad %s\n\n%#v\n\n%#v", tc.Input, actual, expected)
103 // Check for correct atime/mtime
104 for _, dir := range actual {
105 path := filepath.Join(dst, dir)
107 fi, err := os.Stat(path)
109 t.Fatalf("err: %s", err)
111 actual := fi.ModTime()
112 if tc.Mtime.Unix() > 0 {
113 expected := *tc.Mtime
114 if actual != expected {
115 t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), path, actual.String())
117 } else if actual.Unix() < 0 {
118 t.Fatalf("err %s: expected mtime to be > 0, got '%s'", actual.String())
127 func testListDir(t testing.T, path string) []string {
129 err := filepath.Walk(path, func(sub string, info os.FileInfo, err error) error {
134 sub = strings.TrimPrefix(sub, path)
138 sub = sub[1:] // Trim the leading path sep.
140 // If it is a dir, add trailing sep
142 sub += string(os.PathSeparator)
145 result = append(result, sub)
149 t.Fatalf("err: %s", err)
156 func testMD5(t testing.T, path string) string {
157 f, err := os.Open(path)
159 t.Fatalf("err: %s", err)
164 _, err = io.Copy(h, f)
166 t.Fatalf("err: %s", err)
170 return hex.EncodeToString(result)