From 502daff2a1e0a0144697e799dbbfef10b182c3b5 Mon Sep 17 00:00:00 2001 From: William Martin Date: Fri, 11 Oct 2024 15:07:32 +0200 Subject: [PATCH] Refactor acceptance test environment handling --- acceptance/README.md | 12 +++++++++ acceptance/acceptance_test.go | 50 +++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/acceptance/README.md b/acceptance/README.md index 132336d00..b8dacadfd 100644 --- a/acceptance/README.md +++ b/acceptance/README.md @@ -25,6 +25,18 @@ A full example invocation can be found below: GH_HOST= GH_ACCEPTANCE_ORG= GH_TOKEN= test -tags=acceptance ./acceptance ``` +### Acceptance Test VS Code Support + +Due to the `//go:build acceptance` build constraint, some functionality is limited because `gopls` isn't being informed about the tag. To resolve this, set the following in your `settings.json`: + +```json + "gopls": { + "buildFlags": [ + "-tags=acceptance" + ] + }, +``` + ### Effective Test Authoring This section will likely extend over time. diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index ade3517af..ab6432304 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -3,6 +3,7 @@ package acceptance_test import ( + "fmt" "os" "path" "strings" @@ -25,10 +26,16 @@ func TestMain(m *testing.M) { } func TestPullRequests(t *testing.T) { - testscript.Run(t, params("pr")) + var tsEnv testScriptEnv + if err := tsEnv.fromEnv(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + testscript.Run(t, testScriptParamsFor("pr")) } -func params(dir string) testscript.Params { +func testScriptParamsFor(dir string) testscript.Params { return testscript.Params{ Dir: path.Join("testdata", dir), Files: []string{}, @@ -96,3 +103,42 @@ func extractScriptName(vars []string) (string, bool) { } return "", false } + +type missingEnvError struct { + missingEnvs []string +} + +func (e missingEnvError) Error() string { + return fmt.Sprintf("missing environment variables: %s", strings.Join(e.missingEnvs, ", ")) +} + +type testScriptEnv struct { + host string + org string + token string +} + +func (e *testScriptEnv) fromEnv() error { + envMap := map[string]string{} + + var missingEnvs []string + for _, key := range []string{"GH_HOST", "GH_ACCEPTANCE_ORG", "GH_TOKEN"} { + val, ok := os.LookupEnv(key) + if !ok { + missingEnvs = append(missingEnvs, key) + continue + } + + envMap[key] = val + } + + if len(missingEnvs) > 0 { + return missingEnvError{missingEnvs: missingEnvs} + } + + e.host = envMap["GH_HOST"] + e.org = envMap["GH_ACCEPTANCE_ORG"] + e.token = envMap["GH_TOKEN"] + + return nil +}