Debian Codegraph @

tests/thirdparty/packages_test.go

raw symbols SCIP
1 package thirdparty
2
3 import (
4 "flag"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "runtime"
9 "testing"
10
11 "github.com/mmcloughlin/avo/internal/test"
12 )
13
14 //go:generate go run make_workflow.go -suite suite.json -output ../../.github/workflows/packages.yml
15
16 // Custom flags.
17 var (
18 suitefilename = flag.String("suite", "", "projects configuration")
19 preserve = flag.Bool("preserve", false, "preserve working directories")
20 latest = flag.Bool("latest", false, "use latest versions of each project")
21 )
22
23 // TestPackages runs integration tests on all packages specified by projects
24 // file given on the command line.
25 func TestPackages(t *testing.T) {
26 // Load suite.
27 if *suitefilename == "" {
28 t.Skip("no suite specified")
29 }
30
31 s, err := LoadSuiteFile(*suitefilename)
32 if err != nil {
33 t.Fatal(err)
34 }
35
36 for _, tst := range s.Projects.Tests() {
37 tst := tst // scopelint
38 t.Run(tst.ID(), func(t *testing.T) {
39 if tst.Project.Skip() {
40 t.Skipf("skip: %s", tst.Project.Reason())
41 }
42 dir, clean := test.TempDir(t)
43 if !*preserve {
44 defer clean()
45 } else {
46 t.Logf("working directory: %s", dir)
47 }
48 pt := PackageTest{
49 T: t,
50 Test: tst,
51 WorkDir: dir,
52 Latest: *latest,
53 }
54 pt.Run()
55 })
56 }
57 }
58
59 // PackageTest executes an integration test based on a given third-party package.
60 type PackageTest struct {
61 *testing.T
62 *Test
63
64 WorkDir string // working directory for the test
65 Latest bool // use latest version of the project
66
67 repopath string // path the repo is cloned to
68 }
69
70 // Run the test.
71 func (t *PackageTest) Run() {
72 t.checkout()
73 t.validate()
74 t.steps()
75 }
76
77 // checkout the code at the specified version.
78 func (t *PackageTest) checkout() {
79 // Determine the version we want to checkout.
80 version := t.Project.Version
81 if t.Latest {
82 version = t.Project.DefaultBranch
83 }
84
85 // Clone. Use a shallow clone to speed up large repositories.
86 t.repopath = filepath.Join(t.WorkDir, t.Name())
87 test.Exec(t.T, "git", "init", t.repopath)
88 test.Exec(t.T, "git", "-C", t.repopath, "remote", "add", "origin", t.Project.Repository.CloneURL())
89 test.Exec(t.T, "git", "-C", t.repopath, "fetch", "--depth=1", "origin", version)
90 test.Exec(t.T, "git", "-C", t.repopath, "checkout", "FETCH_HEAD")
91 }
92
93 // validate the test configuration relative to the checked out project.
94 func (t *PackageTest) validate() {
95 // Confirm expected directories exist.
96 expect := map[string]string{
97 "package": t.Package.SubPackage,
98 "root": t.Package.WorkingDirectory(),
99 }
100 for name, subdir := range expect {
101 if subdir == "" {
102 continue
103 }
104 path := filepath.Join(t.repopath, subdir)
105 if _, err := os.Stat(path); err != nil {
106 t.Fatalf("expected %s directory: %s", name, err)
107 }
108 }
109 }
110
111 func (t *PackageTest) steps() {
112 // Determine the path to avo.
113 _, self, _, ok := runtime.Caller(1)
114 if !ok {
115 t.Fatal("failed to determine path to avo")
116 }
117 avodir := filepath.Join(filepath.Dir(self), "..", "..")
118
119 // Run steps.
120 c := &Context{
121 AvoDirectory: avodir,
122 RepositoryDirectory: t.repopath,
123 }
124
125 for _, s := range t.Package.Steps(c) {
126 for _, command := range s.Commands {
127 cmd := exec.Command("sh", "-c", command)
128 cmd.Dir = filepath.Join(t.repopath, s.WorkingDirectory)
129 test.ExecCommand(t.T, cmd)
130 }
131 }
132 }