Refactor acceptance test environment handling

This commit is contained in:
William Martin 2024-10-11 15:07:32 +02:00
parent 5745eb1c1c
commit 502daff2a1
2 changed files with 60 additions and 2 deletions

View file

@ -25,6 +25,18 @@ A full example invocation can be found below:
GH_HOST=<host> GH_ACCEPTANCE_ORG=<org> GH_TOKEN=<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.

View file

@ -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
}