cli/acceptance/pr_test.go
2024-10-11 16:31:16 +02:00

85 lines
1.8 KiB
Go

package acceptance_test
import (
"os"
"path"
"strings"
"testing"
"math/rand"
"github.com/cli/cli/v2/internal/ghcmd"
"github.com/rogpeppe/go-internal/testscript"
)
func ghMain() int {
return int(ghcmd.Main())
}
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"gh": ghMain,
}))
}
func TestPullRequests(t *testing.T) {
testscript.Run(t, params("pr"))
}
func params(dir string) testscript.Params {
return testscript.Params{
Dir: path.Join("testdata", dir),
Files: []string{},
Setup: sharedSetup,
Cmds: sharedCmds,
RequireExplicitExec: true,
RequireUniqueNames: true,
}
}
var sharedSetup = func(ts *testscript.Env) error {
scriptName, ok := extractScriptName(ts.Vars)
if !ok {
ts.T().Fatal("script name not found")
}
ts.Setenv("SCRIPT_NAME", scriptName)
ts.Setenv("HOME", ts.Cd)
ts.Setenv("GH_CONFIG_DIR", ts.Cd)
ts.Setenv("GH_TOKEN", os.Getenv("GH_TOKEN"))
ts.Setenv("ORG", os.Getenv("GH_ACCEPTANCE_ORG"))
ts.Setenv("RANDOM_STRING", randomString(10))
return nil
}
var sharedCmds = map[string]func(ts *testscript.TestScript, neg bool, args []string){
"defer": func(ts *testscript.TestScript, neg bool, args []string) {
ts.Defer(func() {
if err := ts.Exec(args[0], args[1:]...); err != nil {
ts.Fatalf("deferred command failed: %v", err)
}
})
}}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func extractScriptName(vars []string) (string, bool) {
for _, kv := range vars {
if strings.HasPrefix(kv, "WORK=") {
v := strings.Split(kv, "=")[1]
return strings.CutPrefix(path.Base(v), "script-")
}
}
return "", false
}