-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.snapshots/ linguist-generated=true | ||
testdata/ linguist-generated=true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- name: install go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: 'stable' | ||
- name: install python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.x' | ||
- name: install rust | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
- name: install python dependencies | ||
run: pip install twine build | ||
- name: install go dependencies | ||
run: go install gotest.tools/gotestsum@latest | ||
- name: run golangci-lint | ||
uses: reviewdog/action-golangci-lint@v2 | ||
- name: build | ||
run: go build | ||
- name: test | ||
run: gotestsum --jsonfile unit-tests.json | ||
- name: annotate tests | ||
if: always() | ||
uses: guyarb/[email protected] | ||
with: | ||
test-results: unit-tests.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
testdata/packages/ | ||
!testdata/packages/.gitkeep | ||
.idea | ||
*.iml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package archiver_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package distributions_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package distributions_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package distributions_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package distributions_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y= | ||
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= | ||
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= | ||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= | ||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= | ||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package packages_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package packages_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package packages_test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package parse_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/rstudio/python-distribution-parser" | ||
"github.com/stretchr/testify/assert" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"path" | ||
|
||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
var twine = "twine" | ||
var testdata = "testdata/packages/" | ||
var repositoryUrls = []string{ | ||
"https://github.com/ActiveState/appdirs", | ||
"https://github.com/pallets/click", | ||
"https://github.com/python/importlib_metadata", | ||
"https://github.com/matplotlib/matplotlib", | ||
"https://github.com/sdispater/pendulum", | ||
"https://github.com/pytest-dev/pytest", | ||
} | ||
|
||
// 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, error) { | ||
result, err := url.Parse(repositoryUrl) | ||
if err != nil { | ||
return "", err | ||
} | ||
return path.Base(result.Path), nil | ||
} | ||
|
||
func getRepositoryPath(repository string) string { | ||
return fmt.Sprintf("%s%s/", testdata, repository) | ||
} | ||
|
||
func getDistributionPath(repository string) string { | ||
return fmt.Sprintf("%sdist/", getRepositoryPath(repository)) | ||
} | ||
|
||
func getTarballPath(repository string) (string, error) { | ||
distributionPath := getDistributionPath(repository) | ||
files, err := os.ReadDir(distributionPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var tarballs []string | ||
|
||
for _, file := range files { | ||
if filepath.Ext(file.Name()) == ".gz" { | ||
tarballs = append(tarballs, file.Name()) | ||
} | ||
} | ||
|
||
if len(tarballs) != 1 { | ||
return "", fmt.Errorf("unexpected number of .gz files in %s: %d", distributionPath, len(tarballs)) | ||
} | ||
return fmt.Sprintf("%s%s", distributionPath, tarballs[0]), nil | ||
} | ||
|
||
func clone(repositoryUrl string) error { | ||
repositoryName, err := toRepositoryName(repositoryUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = os.Stat(getRepositoryPath(repositoryName)) | ||
if os.IsNotExist(err) { | ||
cmd := exec.Command("git", "clone", repositoryUrl) | ||
cmd.Dir = testdata | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
return err | ||
} else { | ||
log.Printf("%s is already cloned", repositoryName) | ||
return nil | ||
} | ||
} | ||
|
||
func buildDistribution(repository string) error { | ||
_, err := os.Stat(getDistributionPath(repository)) | ||
if os.IsNotExist(err) { | ||
cmd := exec.Command("python", "-m", "build") | ||
cmd.Dir = getRepositoryPath(repository) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
return err | ||
} else { | ||
log.Printf("%s is already built", repository) | ||
return nil | ||
} | ||
|
||
} | ||
|
||
func getTwineMetadata(repository string) (map[string][]string, error) { | ||
var expectedMetadata map[string][]string | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_ = r.ParseMultipartForm(100000) | ||
expectedMetadata = r.MultipartForm.Value | ||
})) | ||
defer ts.Close() | ||
|
||
tarball, err := getTarballPath(repository) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cmd := exec.Command(twine, "upload", tarball) | ||
cmd.Env = append(cmd.Env, fmt.Sprintf("TWINE_REPOSITORY_URL=%s", ts.URL)) | ||
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 nil, err | ||
} | ||
|
||
return expectedMetadata, nil | ||
} | ||
|
||
func getParserMetadata(repository string) (map[string][]string, error) { | ||
tarball, err := getTarballPath(repository) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result, err := parse.Parse(tarball) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(result) != 1 { | ||
return nil, fmt.Errorf("unexpected length: %d", len(result)) | ||
} | ||
|
||
distribution := result[0] | ||
return distribution.MetadataMap(), nil | ||
} | ||
|
||
func TestParse(t *testing.T) { | ||
for _, repositoryUrl := range repositoryUrls { | ||
log.Printf("testing %s", repositoryUrl) | ||
|
||
err := clone(repositoryUrl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
repositoryName, err := toRepositoryName(repositoryUrl) | ||
assert.Nil(t, err) | ||
|
||
err = buildDistribution(repositoryName) | ||
assert.Nil(t, err) | ||
|
||
expectedMetadata, err := getTwineMetadata(repositoryName) | ||
assert.Nil(t, err) | ||
|
||
actualMetadata, err := getParserMetadata(repositoryName) | ||
assert.Nil(t, err) | ||
|
||
assert.Empty(t, cmp.Diff(expectedMetadata, actualMetadata)) | ||
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
Check failure on line 175 in parse_test.go GitHub Actions / test
|
||
} | ||
} |