diff options
Diffstat (limited to 'vendor/github.com/hashicorp/go-getter/decompress_testing.go')
-rw-r--r-- | vendor/github.com/hashicorp/go-getter/decompress_testing.go | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/go-getter/decompress_testing.go b/vendor/github.com/hashicorp/go-getter/decompress_testing.go new file mode 100644 index 0000000..686d6c2 --- /dev/null +++ b/vendor/github.com/hashicorp/go-getter/decompress_testing.go | |||
@@ -0,0 +1,134 @@ | |||
1 | package getter | ||
2 | |||
3 | import ( | ||
4 | "crypto/md5" | ||
5 | "encoding/hex" | ||
6 | "io" | ||
7 | "io/ioutil" | ||
8 | "os" | ||
9 | "path/filepath" | ||
10 | "reflect" | ||
11 | "runtime" | ||
12 | "sort" | ||
13 | "strings" | ||
14 | "testing" | ||
15 | ) | ||
16 | |||
17 | // TestDecompressCase is a single test case for testing decompressors | ||
18 | type TestDecompressCase struct { | ||
19 | Input string // Input is the complete path to the input file | ||
20 | Dir bool // Dir is whether or not we're testing directory mode | ||
21 | Err bool // Err is whether we expect an error or not | ||
22 | DirList []string // DirList is the list of files for Dir mode | ||
23 | FileMD5 string // FileMD5 is the expected MD5 for a single file | ||
24 | } | ||
25 | |||
26 | // TestDecompressor is a helper function for testing generic decompressors. | ||
27 | func TestDecompressor(t *testing.T, d Decompressor, cases []TestDecompressCase) { | ||
28 | for _, tc := range cases { | ||
29 | t.Logf("Testing: %s", tc.Input) | ||
30 | |||
31 | // Temporary dir to store stuff | ||
32 | td, err := ioutil.TempDir("", "getter") | ||
33 | if err != nil { | ||
34 | t.Fatalf("err: %s", err) | ||
35 | } | ||
36 | |||
37 | // Destination is always joining result so that we have a new path | ||
38 | dst := filepath.Join(td, "subdir", "result") | ||
39 | |||
40 | // We use a function so defers work | ||
41 | func() { | ||
42 | defer os.RemoveAll(td) | ||
43 | |||
44 | // Decompress | ||
45 | err := d.Decompress(dst, tc.Input, tc.Dir) | ||
46 | if (err != nil) != tc.Err { | ||
47 | t.Fatalf("err %s: %s", tc.Input, err) | ||
48 | } | ||
49 | if tc.Err { | ||
50 | return | ||
51 | } | ||
52 | |||
53 | // If it isn't a directory, then check for a single file | ||
54 | if !tc.Dir { | ||
55 | fi, err := os.Stat(dst) | ||
56 | if err != nil { | ||
57 | t.Fatalf("err %s: %s", tc.Input, err) | ||
58 | } | ||
59 | if fi.IsDir() { | ||
60 | t.Fatalf("err %s: expected file, got directory", tc.Input) | ||
61 | } | ||
62 | if tc.FileMD5 != "" { | ||
63 | actual := testMD5(t, dst) | ||
64 | expected := tc.FileMD5 | ||
65 | if actual != expected { | ||
66 | t.Fatalf("err %s: expected MD5 %s, got %s", tc.Input, expected, actual) | ||
67 | } | ||
68 | } | ||
69 | |||
70 | return | ||
71 | } | ||
72 | |||
73 | // Convert expected for windows | ||
74 | expected := tc.DirList | ||
75 | if runtime.GOOS == "windows" { | ||
76 | for i, v := range expected { | ||
77 | expected[i] = strings.Replace(v, "/", "\\", -1) | ||
78 | } | ||
79 | } | ||
80 | |||
81 | // Directory, check for the correct contents | ||
82 | actual := testListDir(t, dst) | ||
83 | if !reflect.DeepEqual(actual, expected) { | ||
84 | t.Fatalf("bad %s\n\n%#v\n\n%#v", tc.Input, actual, expected) | ||
85 | } | ||
86 | }() | ||
87 | } | ||
88 | } | ||
89 | |||
90 | func testListDir(t *testing.T, path string) []string { | ||
91 | var result []string | ||
92 | err := filepath.Walk(path, func(sub string, info os.FileInfo, err error) error { | ||
93 | if err != nil { | ||
94 | return err | ||
95 | } | ||
96 | |||
97 | sub = strings.TrimPrefix(sub, path) | ||
98 | if sub == "" { | ||
99 | return nil | ||
100 | } | ||
101 | sub = sub[1:] // Trim the leading path sep. | ||
102 | |||
103 | // If it is a dir, add trailing sep | ||
104 | if info.IsDir() { | ||
105 | sub += "/" | ||
106 | } | ||
107 | |||
108 | result = append(result, sub) | ||
109 | return nil | ||
110 | }) | ||
111 | if err != nil { | ||
112 | t.Fatalf("err: %s", err) | ||
113 | } | ||
114 | |||
115 | sort.Strings(result) | ||
116 | return result | ||
117 | } | ||
118 | |||
119 | func testMD5(t *testing.T, path string) string { | ||
120 | f, err := os.Open(path) | ||
121 | if err != nil { | ||
122 | t.Fatalf("err: %s", err) | ||
123 | } | ||
124 | defer f.Close() | ||
125 | |||
126 | h := md5.New() | ||
127 | _, err = io.Copy(h, f) | ||
128 | if err != nil { | ||
129 | t.Fatalf("err: %s", err) | ||
130 | } | ||
131 | |||
132 | result := h.Sum(nil) | ||
133 | return hex.EncodeToString(result) | ||
134 | } | ||