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) {
31 for _, tc := range cases {
32 t.Logf("Testing: %s", tc.Input)
34 // Temporary dir to store stuff
35 td, err := ioutil.TempDir("", "getter")
37 t.Fatalf("err: %s", err)
40 // Destination is always joining result so that we have a new path
41 dst := filepath.Join(td, "subdir", "result")
43 // We use a function so defers work
45 defer os.RemoveAll(td)
48 err := d.Decompress(dst, tc.Input, tc.Dir)
49 if (err != nil) != tc.Err {
50 t.Fatalf("err %s: %s", tc.Input, err)
56 // If it isn't a directory, then check for a single file
58 fi, err := os.Stat(dst)
60 t.Fatalf("err %s: %s", tc.Input, err)
63 t.Fatalf("err %s: expected file, got directory", tc.Input)
66 actual := testMD5(t, dst)
67 expected := tc.FileMD5
68 if actual != expected {
69 t.Fatalf("err %s: expected MD5 %s, got %s", tc.Input, expected, actual)
74 actual := fi.ModTime()
76 if actual != expected {
77 t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), dst, actual.String())
84 // Convert expected for windows
85 expected := tc.DirList
86 if runtime.GOOS == "windows" {
87 for i, v := range expected {
88 expected[i] = strings.Replace(v, "/", "\\", -1)
92 // Directory, check for the correct contents
93 actual := testListDir(t, dst)
94 if !reflect.DeepEqual(actual, expected) {
95 t.Fatalf("bad %s\n\n%#v\n\n%#v", tc.Input, actual, expected)
97 // Check for correct atime/mtime
98 for _, dir := range actual {
99 path := filepath.Join(dst, dir)
101 fi, err := os.Stat(path)
103 t.Fatalf("err: %s", err)
105 actual := fi.ModTime()
106 expected := *tc.Mtime
107 if actual != expected {
108 t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), path, actual.String())
116 func testListDir(t testing.T, path string) []string {
118 err := filepath.Walk(path, func(sub string, info os.FileInfo, err error) error {
123 sub = strings.TrimPrefix(sub, path)
127 sub = sub[1:] // Trim the leading path sep.
129 // If it is a dir, add trailing sep
131 sub += string(os.PathSeparator)
134 result = append(result, sub)
138 t.Fatalf("err: %s", err)
145 func testMD5(t testing.T, path string) string {
146 f, err := os.Open(path)
148 t.Fatalf("err: %s", err)
153 _, err = io.Copy(h, f)
155 t.Fatalf("err: %s", err)
159 return hex.EncodeToString(result)