File: packages_test.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (112 lines) | stat: -rw-r--r-- 2,747 bytes parent folder | download
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
package thirdparty

import (
	"flag"
	"os/exec"
	"path/filepath"
	"runtime"
	"testing"

	"github.com/mmcloughlin/avo/internal/test"
)

//go:generate go run make_workflow.go -suite suite.json -output ../../.github/workflows/packages.yml

// Custom flags.
var (
	suitefilename = flag.String("suite", "", "projects configuration")
	preserve      = flag.Bool("preserve", false, "preserve working directories")
	latest        = flag.Bool("latest", false, "use latest versions of each project")
)

// TestPackages runs integration tests on all packages specified by projects
// file given on the command line.
func TestPackages(t *testing.T) {
	// Load suite.
	if *suitefilename == "" {
		t.Skip("no suite specified")
	}

	s, err := LoadSuiteFile(*suitefilename)
	if err != nil {
		t.Fatal(err)
	}

	for _, tst := range s.Projects.Tests() {
		tst := tst // scopelint
		t.Run(tst.ID(), func(t *testing.T) {
			if tst.Project.Skip() {
				t.Skipf("skip: %s", tst.Project.Reason())
			}
			dir, clean := test.TempDir(t)
			if !*preserve {
				defer clean()
			} else {
				t.Logf("working directory: %s", dir)
			}
			pt := PackageTest{
				T:       t,
				Test:    tst,
				WorkDir: dir,
				Latest:  *latest,
			}
			pt.Run()
		})
	}
}

// PackageTest executes an integration test based on a given third-party package.
type PackageTest struct {
	*testing.T
	*Test

	WorkDir string // working directory for the test
	Latest  bool   // use latest version of the project

	repopath string // path the repo is cloned to
}

// Run the test.
func (t *PackageTest) Run() {
	t.checkout()
	t.steps()
}

// checkout the code at the specified version.
func (t *PackageTest) checkout() {
	// Determine the version we want to checkout.
	version := t.Project.Version
	if t.Latest {
		version = t.Project.DefaultBranch
	}

	// 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.Project.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")
}

func (t *PackageTest) steps() {
	// 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), "..", "..")

	// Run steps.
	c := &Context{
		AvoDirectory:        avodir,
		RepositoryDirectory: t.repopath,
	}

	for _, s := range t.Package.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)
		}
	}
}