tests/thirdparty: package metadata (#223)

Fetches third-party package metadata from Github.
This commit is contained in:
Michael McLoughlin
2021-11-07 16:13:33 -08:00
committed by GitHub
parent afe2d539b8
commit f355d27b13
9 changed files with 544 additions and 24 deletions

View File

@@ -27,11 +27,23 @@ func (r GithubRepository) CloneURL() string {
return fmt.Sprintf("https://github.com/%s.git", r)
}
// Metadata about the repository.
type Metadata struct {
// Repository description.
Description string `json:"description,omitempty"`
// Homepage URL. Not the same as the Github page.
Homepage string `json:"homepage,omitempty"`
// Number of Github stars.
Stars int `json:"stars,omitempty"`
}
// Step represents a set of commands to run as part of the testing plan for a
// third-party package.
type Step struct {
Name string `json:"name"`
WorkingDirectory string `json:"dir"`
Name string `json:"name,omitempty"`
WorkingDirectory string `json:"dir,omitempty"`
Commands []string `json:"commands"`
}
@@ -52,13 +64,19 @@ type Package struct {
// available on github.
Repository GithubRepository `json:"repository"`
// Repository metadata.
Metadata Metadata `json:"metadata"`
// Default git branch. This is used when testing against the latest version.
DefaultBranch string `json:"default_branch,omitempty"`
// Version as a git sha, tag or branch.
Version string `json:"version"`
// Sub-package within the repository under test. All file path references
// will be relative to this directory. If empty the root of the repository
// is used.
SubPackage string `json:"pkg"`
SubPackage string `json:"pkg,omitempty"`
// Path to the module file for the avo generator package. This is necessary
// so the integration test can insert replace directives to point at the avo
@@ -68,13 +86,13 @@ type Package struct {
// Setup steps. These run prior to the insertion of avo replace directives,
// therefore should be used if it's necessary to initialize new go modules
// within the repository.
Setup []*Step `json:"setup"`
Setup []*Step `json:"setup,omitempty"`
// Steps to run the avo code generator.
Generate []*Step `json:"generate"` // generate commands to run
// Test steps. If empty, defaults to "go test ./...".
Test []*Step `json:"test"`
Test []*Step `json:"test,omitempty"`
}
// ID returns an identifier for the package.
@@ -83,9 +101,8 @@ func (p *Package) ID() string {
return strings.ReplaceAll(pkgpath, "/", "-")
}
// setdefaults fills in missing parameters to help make the input package
// descriptions less verbose.
func (p *Package) setdefaults() {
// defaults sets or removes default field values.
func (p *Package) defaults(set bool) {
for _, stage := range []struct {
Steps []*Step
DefaultName string
@@ -94,14 +111,28 @@ func (p *Package) setdefaults() {
{p.Generate, "Generate"},
{p.Test, "Test"},
} {
if len(stage.Steps) == 1 && stage.Steps[0].Name == "" {
stage.Steps[0].Name = stage.DefaultName
if len(stage.Steps) == 1 {
stage.Steps[0].Name = applydefault(set, stage.Steps[0].Name, stage.DefaultName)
}
}
}
func applydefault(set bool, s, def string) string {
switch {
case set && s == "":
return def
case !set && s == def:
return ""
default:
return s
}
}
// Validate package definition.
func (p *Package) Validate() error {
if p.DefaultBranch == "" {
return errors.New("missing default branch")
}
if p.Version == "" {
return errors.New("missing version")
}
@@ -193,9 +224,9 @@ func (p *Package) Steps(c *Context) []*Step {
// Packages is a collection of third-party integration tests.
type Packages []*Package
func (p Packages) setdefaults() {
func (p Packages) defaults(set bool) {
for _, pkg := range p {
pkg.setdefaults()
pkg.defaults(set)
}
}
@@ -217,7 +248,7 @@ func LoadPackages(r io.Reader) (Packages, error) {
if err := d.Decode(&pkgs); err != nil {
return nil, err
}
pkgs.setdefaults()
pkgs.defaults(true)
return pkgs, nil
}
@@ -230,3 +261,23 @@ func LoadPackagesFile(filename string) (Packages, error) {
defer f.Close()
return LoadPackages(f)
}
// StorePackages writes a list of package configurations in JSON format.
func StorePackages(w io.Writer, pkgs Packages) error {
e := json.NewEncoder(w)
e.SetIndent("", " ")
pkgs.defaults(false)
err := e.Encode(pkgs)
pkgs.defaults(true)
return err
}
// StorePackagesFile writes a list of package configurations to a JSON file.
func StorePackagesFile(filename string, pkgs Packages) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return StorePackages(f, pkgs)
}

View File

@@ -1,6 +1,8 @@
package thirdparty
import (
"bytes"
"reflect"
"strings"
"testing"
)
@@ -24,35 +26,46 @@ func TestValidateErrors(t *testing.T) {
ErrorSubstring: "missing commands",
},
{
Name: "package_missing_version",
Name: "package_missing_default_branch",
Item: &Package{
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
},
ErrorSubstring: "missing default branch",
},
{
Name: "package_missing_version",
Item: &Package{
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
DefaultBranch: "main",
},
ErrorSubstring: "missing version",
},
{
Name: "package_missing_module",
Item: &Package{
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
Version: "v1.0.1",
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
DefaultBranch: "main",
Version: "v1.0.1",
},
ErrorSubstring: "missing module",
},
{
Name: "package_no_generate_commands",
Item: &Package{
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
Version: "v1.0.1",
Module: "avo/go.mod",
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
DefaultBranch: "main",
Version: "v1.0.1",
Module: "avo/go.mod",
},
ErrorSubstring: "no generate commands",
},
{
Name: "package_invalid_generate_commands",
Item: &Package{
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
Version: "v1.0.1",
Module: "avo/go.mod",
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
DefaultBranch: "main",
Version: "v1.0.1",
Module: "avo/go.mod",
Generate: []*Step{
{},
},
@@ -66,7 +79,7 @@ func TestValidateErrors(t *testing.T) {
Repository: GithubRepository{Owner: "octocat", Name: "hello-world"},
},
},
ErrorSubstring: "missing version",
ErrorSubstring: "missing default branch",
},
}
for _, c := range cases {
@@ -134,3 +147,26 @@ func TestPackagesFileStepsValid(t *testing.T) {
}
}
}
func TestPackagesFileRoundtrip(t *testing.T) {
pkgs, err := LoadPackagesFile("packages.json")
if err != nil {
t.Fatal(err)
}
// Write and read back.
buf := bytes.NewBuffer(nil)
if err := StorePackages(buf, pkgs); err != nil {
t.Fatal(err)
}
roundtrip, err := LoadPackages(buf)
if err != nil {
t.Fatal(err)
}
// Should be identical.
if !reflect.DeepEqual(pkgs, roundtrip) {
t.Fatal("roundtrip mismatch")
}
}

56
tests/thirdparty/metadata_test.go vendored Normal file
View File

@@ -0,0 +1,56 @@
package thirdparty
import (
"context"
"flag"
"testing"
"github.com/mmcloughlin/avo/internal/github"
"github.com/mmcloughlin/avo/internal/test"
)
var update = flag.Bool("update", false, "update package metadata")
func TestPackagesFileMetadata(t *testing.T) {
test.RequiresNetwork(t)
ctx := context.Background()
pkgs, err := LoadPackagesFile("packages.json")
if err != nil {
t.Fatal(err)
}
g := github.NewClient(github.WithTokenFromEnvironment())
for _, pkg := range pkgs {
// Fetch metadata.
r, err := g.Repository(ctx, pkg.Repository.Owner, pkg.Repository.Name)
if err != nil {
t.Fatal(err)
}
// Update, if requested.
if *update {
pkg.DefaultBranch = r.DefaultBranch
pkg.Metadata.Description = r.Description
pkg.Metadata.Homepage = r.Homepage
pkg.Metadata.Stars = r.StargazersCount
t.Logf("%s: metadata updated", pkg.ID())
}
// Check up to date. Potentially fast-changing properties not included.
uptodate := true
uptodate = pkg.DefaultBranch == r.DefaultBranch && uptodate
uptodate = pkg.Metadata.Description == r.Description && uptodate
uptodate = pkg.Metadata.Homepage == r.Homepage && uptodate
if !uptodate {
t.Errorf("%s: metadata out of date (use -update flag to fix)", pkg.ID())
}
}
if err := StorePackagesFile("packages.json", pkgs); err != nil {
t.Fatal(err)
}
}

View File

@@ -4,6 +4,11 @@
"owner": "zeebo",
"name": "xxh3"
},
"metadata": {
"description": "XXH3 algorithm in Go",
"stars": 198
},
"default_branch": "master",
"version": "v1.0.0-rc1",
"module": "avo/go.mod",
"generate": [
@@ -21,6 +26,11 @@
"owner": "dgryski",
"name": "go-sip13"
},
"metadata": {
"description": "siphash 1-3",
"stars": 31
},
"default_branch": "master",
"version": "62edffca92457b3a66125c686137cc5f0fe81672",
"module": "_avo/go.mod",
"setup": [
@@ -55,6 +65,11 @@
"owner": "phoreproject",
"name": "bls"
},
"metadata": {
"description": "Go implementation of the BLS12-381 pairing",
"stars": 81
},
"default_branch": "master",
"version": "a88a5ae26844d7293359422888d7c7f69f43c845",
"module": "asm/go.mod",
"setup": [
@@ -90,6 +105,11 @@
"owner": "minio",
"name": "md5-simd"
},
"metadata": {
"description": "Accelerate aggregated MD5 hashing performance up to 8x for AVX512 and 4x for AVX2. Useful for server applications that need to compute many MD5 sums in parallel.",
"stars": 87
},
"default_branch": "master",
"version": "30ad8af83f6868c2a30c615f3edf1a9366bf3f89",
"module": "_gen/go.mod",
"generate": [
@@ -106,6 +126,11 @@
"owner": "zeebo",
"name": "blake3"
},
"metadata": {
"description": "Pure Go implementation of BLAKE3 with AVX2 and SSE4.1 acceleration",
"stars": 252
},
"default_branch": "master",
"version": "25dba572f0e78ec108f0dd79c9c15288f542d7d9",
"module": "avo/go.mod",
"generate": [
@@ -123,6 +148,11 @@
"owner": "klauspost",
"name": "reedsolomon"
},
"metadata": {
"description": "Reed-Solomon Erasure Coding in Go",
"stars": 1343
},
"default_branch": "master",
"version": "922778284547557265cff0f903ab5f4c27e40ae9",
"module": "_gen/go.mod",
"generate": [
@@ -139,6 +169,11 @@
"owner": "orisano",
"name": "wyhash"
},
"metadata": {
"description": "A pure-Go wyhash implementation.",
"stars": 21
},
"default_branch": "master",
"version": "32a3f7f6ba4797e2d87dab2969cc9dd63d39cce9",
"module": "avo/go.mod",
"setup": [
@@ -164,6 +199,11 @@
"owner": "klauspost",
"name": "compress"
},
"metadata": {
"description": "Optimized Go Compression Packages",
"stars": 2442
},
"default_branch": "master",
"version": "2adf487b3e02f95ce7efd6e4953fda0ae7ecd080",
"pkg": "s2",
"module": "_generate/go.mod",
@@ -181,6 +221,11 @@
"owner": "dgryski",
"name": "go-bloomindex"
},
"metadata": {
"description": "Bloom-filter based search index",
"stars": 107
},
"default_branch": "master",
"version": "0902316dce158c154b958ee5cfc706c62af29a42",
"module": "avo/go.mod",
"setup": [
@@ -221,6 +266,11 @@
"owner": "dgryski",
"name": "go-marvin32"
},
"metadata": {
"description": "Assembly-optimized Marvin32 hash function",
"stars": 12
},
"default_branch": "master",
"version": "7d18f4c6ea7c24b29d1c7a670f8ae40b0812f2e3",
"module": "avo/go.mod",
"setup": [
@@ -262,6 +312,11 @@
"owner": "dgryski",
"name": "go-speck"
},
"metadata": {
"description": "SPECK cipher",
"stars": 10
},
"default_branch": "master",
"version": "5b36d4c08d8840c352a153bf37281434ad550ec0",
"module": "avo/go.mod",
"setup": [
@@ -304,6 +359,11 @@
"owner": "dgryski",
"name": "go-chaskey"
},
"metadata": {
"description": "go-chaskey: an implementation of chaskey, an efficient MAC for microcontrollers",
"stars": 7
},
"default_branch": "master",
"version": "ba454392bc5ab6daae103e15147185f8f4a27dcc",
"module": "avo/go.mod",
"setup": [
@@ -346,6 +406,11 @@
"owner": "lukechampine",
"name": "us"
},
"metadata": {
"description": "An alternative interface to Sia",
"stars": 49
},
"default_branch": "master",
"version": "dff56a80f83653cb14eeeb57ba6ba3c3e942c412",
"pkg": "merkle/blake2b",
"module": "avo/go.mod",
@@ -380,6 +445,11 @@
"owner": "segmentio",
"name": "asm"
},
"metadata": {
"description": "Go library providing algorithms optimized to leverage the characteristics of modern CPUs",
"stars": 548
},
"default_branch": "main",
"version": "v1.0.0",
"module": "build/go.mod",
"generate": [
@@ -395,6 +465,11 @@
"owner": "ericlagergren",
"name": "lwcrypto"
},
"metadata": {
"description": "NIST Lightweight Cryptography finalists",
"stars": 2
},
"default_branch": "main",
"version": "0c42b05eddc34c58bf8e0cd4250c5cd2c256ea57",
"pkg": "ascon",
"module": "asm/go.mod",
@@ -413,6 +488,11 @@
"owner": "ericlagergren",
"name": "lwcrypto"
},
"metadata": {
"description": "NIST Lightweight Cryptography finalists",
"stars": 2
},
"default_branch": "main",
"version": "0c42b05eddc34c58bf8e0cd4250c5cd2c256ea57",
"pkg": "grain",
"module": "asm/go.mod",
@@ -431,6 +511,11 @@
"owner": "oasisprotocol",
"name": "curve25519-voi"
},
"metadata": {
"description": "High-performance Curve25519/ristretto255 for Go",
"stars": 32
},
"default_branch": "master",
"version": "d5a936accd94ef9da4c0fe9db0a6342dcdcfeadf",
"module": "internal/asm/amd64/go.mod",
"generate": [
@@ -447,6 +532,12 @@
"owner": "golang",
"name": "crypto"
},
"metadata": {
"description": "[mirror] Go supplementary cryptography libraries",
"homepage": "https://golang.org/x/crypto",
"stars": 2283
},
"default_branch": "master",
"version": "089bfa5675191fd96a44247682f76ebca03d7916",
"pkg": "curve25519",
"module": "internal/field/_asm/go.mod",