2020-01-06 22:56:38 -05:00
|
|
|
package thirdparty
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/mmcloughlin/avo/internal/test"
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-15 23:25:51 -07:00
|
|
|
//go:generate go run make_workflow.go -pkgs packages.json -output ../../.github/workflows/packages.yml
|
|
|
|
|
|
2020-01-06 22:56:38 -05:00
|
|
|
// Custom flags.
|
|
|
|
|
var (
|
|
|
|
|
pkgsfilename = flag.String("pkgs", "", "packages configuration")
|
|
|
|
|
preserve = flag.Bool("preserve", false, "preserve working directories")
|
|
|
|
|
latest = flag.Bool("latest", false, "use latest versions of each package")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TestPackages runs integration tests on all packages specified by packages
|
|
|
|
|
// file given on the command line.
|
|
|
|
|
func TestPackages(t *testing.T) {
|
|
|
|
|
// Load packages.
|
|
|
|
|
if *pkgsfilename == "" {
|
|
|
|
|
t.Skip("no packages specified")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkgs, err := LoadPackagesFile(*pkgsfilename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
|
pkg := pkg // scopelint
|
2021-09-15 23:25:51 -07:00
|
|
|
t.Run(pkg.ID(), func(t *testing.T) {
|
2020-01-06 22:56:38 -05:00
|
|
|
dir, clean := test.TempDir(t)
|
|
|
|
|
if !*preserve {
|
|
|
|
|
defer clean()
|
|
|
|
|
} else {
|
|
|
|
|
t.Logf("working directory: %s", dir)
|
|
|
|
|
}
|
|
|
|
|
pt := PackageTest{
|
|
|
|
|
T: t,
|
|
|
|
|
Package: pkg,
|
|
|
|
|
WorkDir: dir,
|
|
|
|
|
Latest: *latest,
|
|
|
|
|
}
|
|
|
|
|
pt.Run()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PackageTest executes an integration test based on a given third-party package.
|
|
|
|
|
type PackageTest struct {
|
|
|
|
|
*testing.T
|
2021-09-15 23:25:51 -07:00
|
|
|
*Package
|
2020-01-06 22:56:38 -05:00
|
|
|
|
|
|
|
|
WorkDir string // working directory for the test
|
|
|
|
|
Latest bool // use latest version of the package
|
|
|
|
|
|
|
|
|
|
repopath string // path the repo is cloned to
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run the test.
|
|
|
|
|
func (t *PackageTest) Run() {
|
|
|
|
|
t.checkout()
|
2021-09-15 23:25:51 -07:00
|
|
|
t.steps()
|
2020-01-06 22:56:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkout the code at the specified version.
|
|
|
|
|
func (t *PackageTest) checkout() {
|
2021-11-07 16:30:50 -08:00
|
|
|
// Determine the version we want to checkout.
|
|
|
|
|
version := t.Version
|
2020-01-06 22:56:38 -05:00
|
|
|
if t.Latest {
|
2021-11-07 16:30:50 -08:00
|
|
|
version = t.DefaultBranch
|
2020-01-06 22:56:38 -05:00
|
|
|
}
|
2021-11-07 16:30:50 -08:00
|
|
|
|
|
|
|
|
// Clone. Use a shallow clone to speed up large repositories.
|
|
|
|
|
t.repopath = filepath.Join(t.WorkDir, t.Name())
|
|
|
|
|
test.Exec(t.T, "git", "init", t.repopath)
|
|
|
|
|
test.Exec(t.T, "git", "-C", t.repopath, "remote", "add", "origin", t.Repository.CloneURL())
|
|
|
|
|
test.Exec(t.T, "git", "-C", t.repopath, "fetch", "--depth=1", "origin", version)
|
|
|
|
|
test.Exec(t.T, "git", "-C", t.repopath, "checkout", "FETCH_HEAD")
|
2020-01-06 22:56:38 -05:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 23:25:51 -07:00
|
|
|
func (t *PackageTest) steps() {
|
2020-01-06 22:56:38 -05:00
|
|
|
// Determine the path to avo.
|
|
|
|
|
_, self, _, ok := runtime.Caller(1)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatal("failed to determine path to avo")
|
|
|
|
|
}
|
|
|
|
|
avodir := filepath.Join(filepath.Dir(self), "..", "..")
|
|
|
|
|
|
2021-09-15 23:25:51 -07:00
|
|
|
// Run steps.
|
|
|
|
|
c := &Context{
|
|
|
|
|
AvoDirectory: avodir,
|
|
|
|
|
RepositoryDirectory: t.repopath,
|
2020-01-06 22:56:38 -05:00
|
|
|
}
|
2021-04-19 14:34:35 -07:00
|
|
|
|
2021-09-15 23:25:51 -07:00
|
|
|
for _, s := range t.Steps(c) {
|
|
|
|
|
for _, command := range s.Commands {
|
|
|
|
|
cmd := exec.Command("sh", "-c", command)
|
|
|
|
|
cmd.Dir = filepath.Join(t.repopath, s.WorkingDirectory)
|
|
|
|
|
test.ExecCommand(t.T, cmd)
|
|
|
|
|
}
|
2020-01-06 22:56:38 -05:00
|
|
|
}
|
|
|
|
|
}
|