-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
270 lines (235 loc) · 5.59 KB
/
main_test.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"testing"
"github.com/rogpeppe/go-internal/testscript"
)
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"gorepro": mainRetCode,
}))
}
func TestScripts(t *testing.T) {
params := testscript.Params{
Dir: "testdata/scripts",
Cmds: map[string]func(t *testscript.TestScript, neg bool, args []string){
"sha256check": sha256check,
"gunzip": gunzip,
"sha256cmp": sha256cmp,
"untar": untar,
"unzip": unzip,
"wget": wget,
},
Setup: func(e *testscript.Env) error {
// $HOME isn't set inside testscripts for some reason?
e.Setenv("HOME", os.Getenv("HOME"))
// tell gorepro not to use colors when printing so we can
// match on stdout/stderr
e.Setenv("NO_COLOR", "1")
return nil
},
RequireExplicitExec: true,
RequireUniqueNames: true,
}
testscript.Run(t, params)
}
func gunzip(t *testscript.TestScript, neg bool, args []string) {
if len(args) != 1 {
t.Fatalf("usage: gunzip file")
}
if neg {
t.Fatalf("gunzip: negation not supported")
}
cFile, err := os.Open(t.MkAbs(args[0]))
if err != nil {
t.Fatalf("error opening file: %v", err)
}
defer cFile.Close()
r, err := gzip.NewReader(cFile)
if err != nil {
t.Fatalf("error reading compressed file: %v", err)
}
defer r.Close()
outPath := args[0]
if strings.HasSuffix(args[0], ".gz") {
outPath = outPath[:len(outPath)-3]
} else {
outPath += ".uncmp"
}
uFile, err := os.Create(t.MkAbs(outPath))
if err != nil {
t.Fatalf("error creating file: %v", err)
}
defer uFile.Close()
if _, err := io.Copy(uFile, r); err != nil {
t.Fatalf("error uncompressing file: %v", err)
}
}
func sha256check(t *testscript.TestScript, neg bool, args []string) {
if len(args) != 2 {
t.Fatalf("usage: sha256check file hash")
}
if neg {
t.Fatalf("sha256check: negation not supported")
}
h := sha256.New()
file, err := os.Open(t.MkAbs(args[0]))
if err != nil {
t.Fatalf("error opening file: %v", err)
}
defer file.Close()
if _, err = io.Copy(h, file); err != nil {
t.Fatalf("error hashing file: %v", err)
}
checkHash, err := hex.DecodeString(args[1])
if err != nil {
t.Fatalf("error decoding hash: %v", err)
}
if !bytes.Equal(h.Sum(nil), checkHash) {
t.Fatalf("hashes not equal")
}
}
func sha256cmp(t *testscript.TestScript, neg bool, args []string) {
if len(args) != 2 {
t.Fatalf("usage: sha256cmp file file")
}
if neg {
t.Fatalf("sha256cmp: negation not supported")
}
h1 := sha256.New()
h2 := sha256.New()
file1, err := os.Open(t.MkAbs(args[0]))
if err != nil {
t.Fatalf("error opening file: %v", err)
}
defer file1.Close()
file2, err := os.Open(t.MkAbs(args[0]))
if err != nil {
t.Fatalf("error opening file: %v", err)
}
defer file2.Close()
if _, err = io.Copy(h1, file1); err != nil {
t.Fatalf("error hashing file: %v", err)
}
if _, err = io.Copy(h2, file2); err != nil {
t.Fatalf("error hashing file: %v", err)
}
if !bytes.Equal(h1.Sum(nil), h2.Sum(nil)) {
t.Fatalf("hashes not equal")
}
}
func untar(t *testscript.TestScript, neg bool, args []string) {
if len(args) != 1 {
t.Fatalf("usage: untar file")
}
if neg {
t.Fatalf("untar: negation not supported")
}
cFile, err := os.Open(t.MkAbs(args[0]))
if err != nil {
t.Fatalf("error opening file: %v", err)
}
defer cFile.Close()
r := tar.NewReader(cFile)
for {
hdr, err := r.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
t.Fatalf("error reading tar header: %v", err)
}
file, err := os.Create(t.MkAbs(hdr.Name))
if err != nil {
t.Fatalf("error creating file: %v", err)
}
defer file.Close()
n, err := io.Copy(file, r)
if err != nil {
t.Fatalf("error unzipping file: %v", err)
}
if n != hdr.Size {
t.Fatalf("tarred file truncated: %v", err)
}
if err := file.Chmod(0o777); err != nil {
t.Fatalf("error changing permissions of file: %v", err)
}
}
}
func unzip(t *testscript.TestScript, neg bool, args []string) {
if len(args) != 1 {
t.Fatalf("usage: unzip file")
}
if neg {
t.Fatalf("unzip: negation not supported")
}
r, err := zip.OpenReader(t.MkAbs(args[0]))
if err != nil {
t.Fatalf("error opening zip file: %v", err)
}
defer r.Close()
for _, f := range r.File {
zipFile, err := f.Open()
if err != nil {
t.Fatalf("error opening embedded file: %v", err)
}
defer zipFile.Close()
file, err := os.Create(t.MkAbs(f.Name))
if err != nil {
t.Fatalf("error creating file: %v", err)
}
defer file.Close()
n, err := io.Copy(file, zipFile)
if err != nil {
t.Fatalf("error unzipping file: %v", err)
}
if n != f.FileInfo().Size() {
t.Fatalf("zipped file truncated: %v", err)
}
if err := file.Chmod(0o777); err != nil {
t.Fatalf("error changing permissions of file: %v", err)
}
}
}
func wget(t *testscript.TestScript, neg bool, args []string) {
if len(args) != 1 {
t.Fatalf("usage: wget URL")
}
if neg {
t.Fatalf("wget: negation not supported")
}
fileURL, err := url.Parse(args[0])
if err != nil {
t.Fatalf("error parsing URL: %v", err)
}
filePath := path.Base(fileURL.Path)
file, err := os.Create(t.MkAbs(filePath))
if err != nil {
t.Fatalf("error creating file: %v", err)
}
defer file.Close()
resp, err := http.Get(args[0])
if err != nil {
t.Fatalf("error making HTTP request: %v", err)
}
defer resp.Body.Close()
n, err := io.Copy(file, resp.Body)
if err != nil {
t.Fatalf("error downloading file: %v", err)
}
if n != resp.ContentLength {
t.Fatalf("downloaded file truncated")
}
}