-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_test.go
461 lines (394 loc) · 11 KB
/
parse_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package parse_test
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"maps"
"math"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"testing"
"github.com/bradleyjkemp/cupaloy"
"github.com/google/go-cmp/cmp"
"github.com/rstudio/python-distribution-parser"
"github.com/rstudio/python-distribution-parser/types"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testdata is the path that we should store cloned repositories
var testdata = "testdata/repositories/"
type Repository struct {
url string
reference string
}
// repositories is the list of repositories that we should test
var repositories = []Repository{
{
url: "https://github.com/ActiveState/appdirs",
reference: "1.4.4",
},
{
url: "https://github.com/pallets/click",
reference: "8.1.7",
},
{
url: "https://github.com/python/importlib_metadata",
reference: "v7.0.0",
},
{
url: "https://github.com/matplotlib/matplotlib",
reference: "v3.8.2",
},
{
url: "https://github.com/pytest-dev/pytest",
reference: "v7.4.3",
},
{
url: "https://github.com/tkem/cachetools",
reference: "v5.3.2",
},
{
url: "https://github.com/certifi/python-certifi",
reference: "v1.0.1",
},
{
url: "https://github.com/chardet/chardet",
reference: "5.2.0",
},
{
url: "https://github.com/jaraco/configparser",
reference: "v6.0.0",
},
{
url: "https://github.com/nedbat/coveragepy",
reference: "coverage-5.5",
},
{
url: "https://github.com/micheles/decorator",
reference: "4.4.2",
},
{
url: "https://github.com/tiran/defusedxml",
reference: "v0.7.1",
},
}
// toRepositoryName converts a repository name to the name of the folder the repository will be cloned in
// for example, https://github.com/ActiveState/appdirs => appdirs
func toRepositoryName(repositoryUrl string) string {
result, err := url.Parse(repositoryUrl)
if err != nil {
log.Fatalf("%v", err)
}
return path.Base(result.Path)
}
// getRepositoryPath returns the path the a repository is cloned at
func getRepositoryPath(repository string) string {
return fmt.Sprintf("%s%s/", testdata, repository)
}
// getDistributionPath returns the path that distribution tarballs are kept
func getDistributionPath(repository string) string {
return fmt.Sprintf("%sdist/", getRepositoryPath(repository))
}
// getArtifactPath returns the path to a built tarball/wheel for a repository
func getArtifactPath(path string, extension string) (string, error) {
files, err := os.ReadDir(path)
if err != nil {
return "", err
}
var artifacts = lo.Filter(files, func(file os.DirEntry, _ int) bool {
return filepath.Ext(file.Name()) == extension
})
if len(artifacts) != 1 {
return "", fmt.Errorf("expected exactly 1 file with extension %s in %s, but found %d", extension, path, len(artifacts))
}
return fmt.Sprintf("%s%s", path, artifacts[0].Name()), nil
}
// clone will clone a Git repository to disk if it does not already exist
func clone(repository Repository, destination string) error {
_, err := os.Stat(destination)
if os.IsNotExist(err) {
cmd := exec.Command("git", "clone", repository.url, destination, "--branch", repository.reference)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
return nil
}
// buildDistribution builds a Python package with python -m build
func buildDistribution(directory string) error {
_, err := os.Stat(fmt.Sprintf("%s/dist", directory))
if os.IsNotExist(err) {
cmd := exec.Command("python", "-m", "build")
cmd.Dir = directory
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
return nil
}
// signDistribution will use gpg to sign a file, and return the path to the resulting `.asc` file
func signDistribution(file string) (string, error) {
signatureFile := fmt.Sprintf("%s.asc", file)
_, err := os.Stat(signatureFile)
if os.IsNotExist(err) {
cmd := exec.Command("gpg", "--detach-sign", "-a", file)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return "", err
}
return signatureFile, nil
}
return signatureFile, nil
}
// getTwineFile returns the Metadata that Twine generates
func getTwineFile(file string, signatureFile string) (ParserData, error) {
var metadata map[string][]string
var gpgSignature []byte
var err error
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = r.ParseMultipartForm(math.MaxInt64)
metadata = r.MultipartForm.Value
file, _, err := r.FormFile("gpg_signature")
if err != nil {
if errors.Is(err, http.ErrMissingFile) {
return
} else {
log.Fatalf("error while reading multipart file: %v", err)
}
}
defer func(file multipart.File) {
err := file.Close()
if err != nil {
log.Fatalf("error while closing multipart file: %v", err)
}
}(file)
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
log.Fatalf("error while reading multipart file: %v", err)
}
gpgSignature = buf.Bytes()
}))
defer ts.Close()
var cmd *exec.Cmd
if signatureFile == "" {
cmd = exec.Command("twine", "upload", file)
} else {
cmd = exec.Command("twine", "upload", file, signatureFile)
}
cmd.Env = append(cmd.Env, fmt.Sprintf("TWINE_REPOSITORY_URL=%s", ts.URL))
// twine requires these variable to be set
cmd.Env = append(cmd.Env, "TWINE_USERNAME=user")
cmd.Env = append(cmd.Env, "TWINE_PASSWORD=password")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return ParserData{}, err
}
return ParserData{
Metadata: metadata,
GpgSignature: gpgSignature,
}, nil
}
// getParserFile runs the Go parser and returns the resulting Metadata
func getParserFile(file string, signatureFile string) (ParserData, error) {
var packageFiles []*types.PackageFile
var err error
if signatureFile == "" {
packageFiles, err = parse.Parse(file)
} else {
packageFiles, err = parse.Parse(file, signatureFile)
}
if err != nil {
return ParserData{}, err
}
if len(packageFiles) != 1 {
return ParserData{}, fmt.Errorf("unexpected length: %d", len(packageFiles))
}
distribution := packageFiles[0]
metadata := distribution.MetadataMap()
if distribution.GPGSignature == nil {
return ParserData{
Metadata: metadata,
GpgSignature: nil,
}, nil
} else {
return ParserData{
Metadata: metadata,
GpgSignature: distribution.GPGSignature.Bytes,
}, nil
}
}
// checkRequirements ensures that all test requirements are installed
func checkRequirements() error {
_, err := exec.LookPath("python")
if err != nil {
return errors.New("python is not installed")
}
_, err = exec.LookPath("twine")
if err != nil {
return errors.New("twine is not installed")
}
_, err = exec.LookPath("cargo")
if err != nil {
return errors.New("rust is not installed")
}
_, err = exec.LookPath("git")
if err != nil {
return errors.New("git is not installed")
}
_, err = exec.LookPath("gpg")
if err != nil {
return errors.New("gpg is not installed")
}
cmd := exec.Command("pip", "show", "build")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return errors.New("build package is not installed")
}
cmd = exec.Command("pip", "show", "wheel")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return errors.New("wheel package is not installed")
}
return nil
}
// createDistribution builds a Python distribution and optionally signs it.
// The method returns the location of the directory, built artifact, and signature file if any.
func createDistribution(repository Repository, format string, isSigned bool) (string, string, error) {
repoUrl := repository.url
repositoryName := toRepositoryName(repoUrl)
repositoryPath := getRepositoryPath(repositoryName)
err := clone(repository, repositoryPath)
if err != nil {
return "", "", err
}
err = buildDistribution(repositoryPath)
if err != nil {
return "", "", err
}
distributionPath := getDistributionPath(repositoryName)
artifact, err := getArtifactPath(distributionPath, format)
if err != nil {
return "", "", err
}
var signature string
if isSigned {
signature, err = signDistribution(artifact)
if err != nil {
return "", "", err
}
}
return artifact, signature, nil
}
// signedString returns the correct form of "signed", so that unit test names look appropriate
func signedString(value bool) string {
if value {
return "signed"
} else {
return "unsigned"
}
}
// formatString returns a string for unit test names
func formatString(value string) string {
if value == ".gz" {
return "tarball"
} else if value == ".whl" {
return "wheel"
} else {
return ""
}
}
type testCase struct {
repository Repository
isSigned bool
format string
}
type ParserData struct {
Metadata map[string][]string
GpgSignature []byte
}
// filterData replaces some generated fields with static strings
func filterData(data ParserData) ParserData {
copiedData := maps.Clone(data.Metadata)
fields := []string{
"blake2_256_digest",
"md5_digest",
"sha256_digest",
}
for _, field := range fields {
if value, ok := copiedData[field]; ok {
values := lo.Map(value, func(_ string, _ int) string {
return fmt.Sprintf("%s exists", field)
})
copiedData[field] = values
}
}
if data.GpgSignature == nil {
return ParserData{
Metadata: copiedData,
}
} else {
return ParserData{
Metadata: copiedData,
GpgSignature: []byte("GPG signature exists"),
}
}
}
func TestParse(t *testing.T) {
err := checkRequirements()
require.NoError(t, err)
testCases := lo.FlatMap(repositories, func(repository Repository, _ int) []testCase {
return lo.FlatMap([]bool{true, false}, func(isSigned bool, _ int) []testCase {
return lo.FlatMap([]string{".gz", ".whl"}, func(format string, _ int) []testCase {
repositoryName := toRepositoryName(repository.url)
noWheels := []string{}
if lo.Contains(noWheels, repositoryName) && format == ".whl" {
return []testCase{}
}
return []testCase{
{
repository: repository,
isSigned: isSigned,
format: format,
},
}
})
})
})
for _, testCase := range testCases {
repositoryName := toRepositoryName(testCase.repository.url)
require.NoError(t, err)
repository := testCase.repository
isSigned := testCase.isSigned
format := testCase.format
t.Run(fmt.Sprintf("%s %s %s", repositoryName, signedString(isSigned), formatString(format)), func(t *testing.T) {
artifact, signature, err := createDistribution(repository, format, isSigned)
require.NoError(t, err)
expectedMetadata, err := getTwineFile(artifact, signature)
require.NoError(t, err)
actualMetadata, err := getParserFile(artifact, signature)
require.NoError(t, err)
// compare against the normalized outputs to account for expects differences between the two parsers
assert.Empty(t, cmp.Diff(expectedMetadata, actualMetadata))
cupaloy.SnapshotT(t, filterData(actualMetadata))
})
}
}