-
Couldn't load subscription status.
- Fork 7.3k
Introduce testscript acceptance tests generally, and for the PR command specifically #9745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d7465bd
5ed237c
3717162
464a69a
99f7e04
eb9eeb1
5745eb1
502daff
320c5ab
99a9b35
ee5709a
64f5b3c
dc7c66c
9d569b3
f9b2499
fd66555
846a39d
0d7ec44
503659f
fbc72fd
4d986aa
bfa5b6a
1f94cf9
c2c88b2
f3589b2
b095d6b
f7b279d
5e02326
2a0be61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| ## Acceptance Tests | ||
|
|
||
| The acceptance tests are blackbox* tests that are expected to interact with resources on a real GitHub instance. They are built on top of the [`go-internal/testscript`](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript) package, which provides a framework for building tests for command line tools. | ||
|
|
||
| *Note: they aren't strictly blackbox because `exec gh` commands delegate to a binary set up by `testscript` that calls into `ghcmd.Main`. However, since our real `func main` is an extremely thin adapter over `ghcmd.Main`, this is reasonable. This tradeoff avoids us building the binary ourselves for the tests, and allows us to get code coverage metrics. | ||
|
|
||
| ### Running the Acceptance Tests | ||
|
|
||
| The acceptance tests have a build constraint of `//go:build acceptance`, this means that `go test ./...` will continue to work without any modifications. The `acceptance` tag must therefore be provided when running `go test`. | ||
|
|
||
| The following environment variables are required: | ||
|
|
||
| #### `GH_ACCEPTANCE_HOST` | ||
|
|
||
| The GitHub host to target e.g. `github.com` | ||
|
|
||
| #### `GH_ACCEPTANCE_ORG` | ||
|
|
||
| The organization in which the acceptance tests can manage resources in. Consider using `gh-acceptance-testing` on `github.com`. | ||
|
|
||
| #### `GH_ACCEPTANCE_TOKEN` | ||
|
|
||
| The token to use for authenticatin with the `GH_ACCEPTANCE_HOST`. This must already have the necessary scopes for each test, and must have permissions to act in the `GH_ACCEPTANCE_ORG`. See [Effective Test Authoring](#effective-test-authoring) for how tests must handle tokens without sufficient scopes. | ||
|
|
||
| It's recommended to create and use a Legacy PAT for this; Fine-Grained PATs do not offer all the necessary privileges required. You can use an OAuth token provided via `gh auth login --web` and can provide it to the acceptance tests via `GH_ACCEPTANCE_TOKEN=$(gh auth token --hostname <host>)` but this can be a bit confusing and annoying if you `gh auth login` again without `-s` and lose the required scopes. | ||
|
|
||
| --- | ||
|
|
||
| A full example invocation can be found below: | ||
|
|
||
| ``` | ||
| GH_ACCEPTANCE_HOST=<host> GH_ACCEPTANCE_ORG=<org> GH_ACCEPTANCE_TOKEN=<token> go test -tags=acceptance ./acceptance | ||
| ``` | ||
|
|
||
| #### Code Coverage | ||
|
|
||
| To get code coverage, `go test` can be invoked with `coverpkg` and `coverprofile` like so: | ||
|
|
||
| ``` | ||
| GH_ACCEPTANCE_HOST=<host> GH_ACCEPTANCE_ORG=<org> GH_ACCEPTANCE_TOKEN=<token> go test -tags=acceptance -coverprofile=coverage.out -coverpkg=./... ./acceptance | ||
jtmcg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Writing Tests | ||
|
|
||
| This section is to be expanded over time as we write more tests and learn more. | ||
|
|
||
| #### Environment Variables | ||
|
|
||
| The following custom environment variables are made available to the scripts: | ||
| * `GH_HOST`: Set to value of the `GH_ACCEPTANCE_ORG` env var provided to `go test` | ||
| * `ORG`: Set to the value of the `GH_ACCEPTANCE_ORG` env var provided to `go test` | ||
| * `GH_TOKEN`: Set to the value of the `GH_ACCEPTANCE_TOKEN` env var provided to `go test` | ||
| * `RANDOM_STRING`: Set to a length 10 random string of letters to help isolate globally visible resources | ||
| * `SCRIPT_NAME`: Set to the name of the `testscript` currently running, without extension e.g. `pr-view` | ||
| * `HOME`: Set to the initial working directory. Required for `git` operations | ||
| * `GH_CONFIG_DIR`: Set to the initial working directory. Required for `gh` operations | ||
|
|
||
| ### 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" | ||
| ] | ||
| }, | ||
| ``` | ||
|
|
||
| You can install the [`txtar`](https://marketplace.visualstudio.com/items?itemName=brody715.txtar) or [`vscode-testscript`](https://marketplace.visualstudio.com/items?itemName=twpayne.vscode-testscript) extensions to get syntax highlighting. | ||
|
|
||
| ### Debugging Tests | ||
|
|
||
| When tests fail they fail like this: | ||
|
|
||
| ``` | ||
| ➜ go test -tags=acceptance ./acceptance | ||
| --- FAIL: TestPullRequests (0.00s) | ||
| --- FAIL: TestPullRequests/pr-merge (11.07s) | ||
| testscript.go:584: WORK=/private/var/folders/45/sdnm1hp10nj1s9q57dp3bc5h0000gn/T/go-test-script2778137936/script-pr-merge | ||
| # Use gh as a credential helper (0.693s) | ||
| # Create a repository with a file so it has a default branch (1.155s) | ||
| # Defer repo cleanup (0.000s) | ||
| # Clone the repo (1.551s) | ||
| # Prepare a branch to PR with a single file (1.168s) | ||
| # Create the PR (1.903s) | ||
| # Check that the file doesn't exist on the main branch (0.059s) | ||
| # Merge the PR (2.426s) | ||
| # Check that the state of the PR is now merged (0.571s) | ||
| # Pull and check the file exists on the main branch (1.074s) | ||
| # And check we had a merge commit (0.462s) | ||
| > exec git show HEAD | ||
| [stdout] | ||
| commit 85d32c1a83ace270f6754c61f3f7e14956be0a47 | ||
| Author: William Martin <[email protected]> | ||
| Date: Fri Oct 11 15:23:56 2024 +0200 | ||
|
|
||
| Add file.txt | ||
|
|
||
| diff --git a/file.txt b/file.txt | ||
| new file mode 100644 | ||
| index 0000000..7449899 | ||
| --- /dev/null | ||
| +++ b/file.txt | ||
| @@ -0,0 +1 @@ | ||
| +Unimportant contents | ||
| > stdout 'Merge pull request #1' | ||
| FAIL: testdata/pr/pr-merge.txtar:42: no match for `Merge pull request #1` found in stdout | ||
| ``` | ||
|
|
||
| This is generally enough information to understand why a test has failed. However, we can get more information by providing the `-v` flag to `go test`, which turns on verbose mode and shows each command and any associated `stdio`. | ||
|
|
||
| > [!WARNING] | ||
| > Verbose mode dumps the `testscript` environment variables, including the `GH_TOKEN`, so be careful. | ||
|
|
||
| By default `testscript` removes the directory in which it was running the script, and if you've been a conscientious engineer, you should be cleaning up resources using the `defer` statement. However, this can be an impediment to debugging. As such you can set `GH_ACCEPTANCE_PRESERVE_WORK_DIR=true` and `GH_ACCEPTANCE_SKIP_DEFER=true` to skip these cleanup steps. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went back and forth a bit on having a single env var that could have different values but I decided until we get more usage and understand what the enumeration of values might be, it was better to be explicit. |
||
|
|
||
| ### Effective Test Authoring | ||
|
|
||
| This section is to be expanded over time as we write more tests and learn more. | ||
|
|
||
| #### Test Isolation | ||
|
|
||
| The `testscript` library creates a somewhat isolated environment for each script. Each script gets a directory with limited environment variables by default. As far as reasonable, we should look to write scripts that depend on nothing more than themselves, the GitHub resources they manage, and limited additional environmental injection from our own `testscript` setup. | ||
|
|
||
| Here are some guidelines around test isolation: | ||
| * Favour duplication in test setup over abstracting a new `testscript` command | ||
| * Favour a `testscript` owning an entire resource lifecycle over shared resource until we see a performance or rate limiting issue | ||
| * Use the `RANDOM_STRING` env var for globally visible resources to avoid conflicts | ||
|
|
||
| ### Debris | ||
|
|
||
| Since these scripts are creating resources on a GitHub instance, we should try our best to cleanup after them. Use the `defer` keyword to ensure a command runs at the end of a test even in the case of failure. | ||
|
|
||
| #### Scope Validation | ||
|
|
||
| TODO: I believe tests should early exit if the correct scopes aren't in place to execute the entire lifecycle. It's extremely annoying if a `defer` fails to clean up resources because there's no `delete_repo` scope for example. However, I'm not sure yet whether this scope checking should be in the Go tests or in the scripts themselves. It seems very cool to understand required scopes for a script just by looking at the script itself. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
|
|
||
| ### Further Reading | ||
|
|
||
| https://bitfieldconsulting.com/posts/test-scripts | ||
|
|
||
| https://atlasgo.io/blog/2024/09/09/how-go-tests-go-test | ||
|
|
||
| https://encore.dev/blog/testscript-hidden-testing-gem | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| //go:build acceptance | ||
|
|
||
| package acceptance_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "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) { | ||
| var tsEnv testScriptEnv | ||
| if err := tsEnv.fromEnv(); err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| testscript.Run(t, testScriptParamsFor(tsEnv, "pr")) | ||
| } | ||
jtmcg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func testScriptParamsFor(tsEnv testScriptEnv, dir string) testscript.Params { | ||
| return testscript.Params{ | ||
| Dir: path.Join("testdata", dir), | ||
| Files: []string{}, | ||
| Setup: sharedSetup(tsEnv), | ||
| Cmds: sharedCmds(tsEnv), | ||
| RequireExplicitExec: true, | ||
| RequireUniqueNames: true, | ||
| TestWork: tsEnv.preserveWorkDir, | ||
| } | ||
| } | ||
|
|
||
| func sharedSetup(tsEnv testScriptEnv) func(ts *testscript.Env) error { | ||
| return 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_HOST", tsEnv.host) | ||
| ts.Setenv("ORG", tsEnv.org) | ||
| ts.Setenv("GH_TOKEN", tsEnv.token) | ||
|
|
||
| ts.Setenv("RANDOM_STRING", randomString(10)) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // sharedCmds defines a collection of custom testscript commands for our use. | ||
| func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript, neg bool, args []string) { | ||
williammartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return map[string]func(ts *testscript.TestScript, neg bool, args []string){ | ||
| "defer": func(ts *testscript.TestScript, neg bool, args []string) { | ||
| if neg { | ||
| ts.Fatalf("unsupported: ! defer") | ||
| } | ||
|
|
||
| if tsEnv.skipDefer { | ||
| return | ||
| } | ||
|
|
||
| ts.Defer(func() { | ||
| ts.Check(ts.Exec(args[0], args[1:]...)) | ||
| }) | ||
| }, | ||
williammartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "stdout2env": func(ts *testscript.TestScript, neg bool, args []string) { | ||
| if neg { | ||
| ts.Fatalf("unsupported: ! stdout2env") | ||
| } | ||
| if len(args) != 1 { | ||
| ts.Fatalf("usage: stdout2env name") | ||
| } | ||
|
|
||
| ts.Setenv(args[0], strings.TrimRight(ts.ReadFile("stdout"), "\n")) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| type missingEnvError struct { | ||
| missingEnvs []string | ||
| } | ||
|
|
||
| func (e missingEnvError) Error() string { | ||
| return fmt.Sprintf("environment variables %s must be set and non-empty", strings.Join(e.missingEnvs, ", ")) | ||
| } | ||
|
|
||
| type testScriptEnv struct { | ||
| host string | ||
| org string | ||
| token string | ||
|
|
||
| skipDefer bool | ||
| preserveWorkDir bool | ||
| } | ||
|
|
||
| func (e *testScriptEnv) fromEnv() error { | ||
| envMap := map[string]string{} | ||
|
|
||
| requiredEnvVars := []string{ | ||
| "GH_ACCEPTANCE_HOST", | ||
| "GH_ACCEPTANCE_ORG", | ||
| "GH_ACCEPTANCE_TOKEN", | ||
| } | ||
|
|
||
| var missingEnvs []string | ||
| for _, key := range requiredEnvVars { | ||
| val, ok := os.LookupEnv(key) | ||
| if val == "" || !ok { | ||
| missingEnvs = append(missingEnvs, key) | ||
| continue | ||
| } | ||
|
|
||
| envMap[key] = val | ||
| } | ||
|
|
||
| if len(missingEnvs) > 0 { | ||
| return missingEnvError{missingEnvs: missingEnvs} | ||
| } | ||
|
|
||
| if envMap["GH_ACCEPTANCE_ORG"] == "github" || envMap["GH_ACCEPTANCE_ORG"] == "cli" { | ||
| return fmt.Errorf("GH_ACCEPTANCE_ORG cannot be 'github' or 'cli'") | ||
| } | ||
|
|
||
| e.host = envMap["GH_ACCEPTANCE_HOST"] | ||
| e.org = envMap["GH_ACCEPTANCE_ORG"] | ||
| e.token = envMap["GH_ACCEPTANCE_TOKEN"] | ||
|
|
||
| e.preserveWorkDir = os.Getenv("GH_ACCEPTANCE_PRESERVE_WORK_DIR") == "true" | ||
| e.skipDefer = os.Getenv("GH_ACCEPTANCE_SKIP_DEFER") == "true" | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Create a repository with a file so it has a default branch | ||
| exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --add-readme --private | ||
|
|
||
| # Defer repo cleanup | ||
| defer gh repo delete --yes $ORG/$SCRIPT_NAME-$RANDOM_STRING | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone $ORG/$SCRIPT_NAME-$RANDOM_STRING | ||
|
|
||
| # Prepare a branch to PR | ||
| cd $SCRIPT_NAME-$RANDOM_STRING | ||
| exec git checkout -b feature-branch | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push -u origin feature-branch | ||
|
|
||
| # Create the PR | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout2env PR_URL | ||
|
|
||
| # Remove the local branch | ||
| exec git checkout main | ||
| exec git branch -D feature-branch | ||
| stdout 'Deleted branch feature-branch' | ||
|
|
||
| # Checkout the PR | ||
| exec gh pr checkout $PR_URL | ||
| stderr 'Switched to a new branch ''feature-branch''' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Create a repository with a file so it has a default branch | ||
| exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --add-readme --private | ||
|
|
||
| # Defer repo cleanup | ||
| defer gh repo delete --yes $ORG/$SCRIPT_NAME-$RANDOM_STRING | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone $ORG/$SCRIPT_NAME-$RANDOM_STRING | ||
|
|
||
| # Prepare a branch to PR | ||
| cd $SCRIPT_NAME-$RANDOM_STRING | ||
| exec git checkout -b feature-branch | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push -u origin feature-branch | ||
|
|
||
| # Create the PR | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout2env PR_URL | ||
|
|
||
| # Comment on the PR | ||
| exec gh pr comment $PR_URL --body 'Looks like a great feature!' | ||
|
|
||
| # View the PR | ||
| exec gh pr view $PR_URL --comments | ||
| stdout 'Looks like a great feature!' |
Uh oh!
There was an error while loading. Please reload this page.