diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 07954e7..ec2b4cd 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,11 +25,8 @@ jobs: uses: actions/checkout@v2 - name: Run tests - run: | - go test -v ./... - - - name: Run example env: + # Needed for the example-test to run. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - go run ./example/cmd/example/main.go csilvers + go test -v ./... diff --git a/Makefile b/Makefile index 834d5a8..120e2e1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ example: go generate ./... - go run ./example/cmd/example/main.go csilvers + go run ./example/cmd/example/main.go check: go test ./... diff --git a/README.md b/README.md index 2c41422..a4540ba 100644 --- a/README.md +++ b/README.md @@ -78,11 +78,7 @@ If you'd like to contribute to genqlient, welcome! The library is still in a so ### Tests -`go test ./...` tests code generation. (This is run by GitHub Actions.) Most of the tests are snapshot-based; see `generate/generate_test.go`. - -`make example` rebuilds the example, and tests that everything wires up to a real API correctly. This is not currently included in `go test`, since it requires a token. - -TODO(benkraft): Figure out how to get GitHub Actions a token to run the example. +`go test ./...` tests code generation. (This is run by GitHub Actions.) Most of the tests are snapshot-based; see `generate/generate_test.go`. If `GITHUB_TOKEN` is available in the environment, it also checks that the example returns the expected output when run against the real API. This is configured automatically in GitHub Actions, but you can also use a [personal access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) with no scopes. ### Design diff --git a/example/README.md b/example/README.md index ce8f6c5..46dd08e 100644 --- a/example/README.md +++ b/example/README.md @@ -1,9 +1,7 @@ # example of genqlient -## Getting a token -Get a token from [GitHub](https://github.com/settings/tokens/new) (no scopes needed). - ## Invoking the example +Create a [personal access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) with no scopes. To run the example: @@ -15,7 +13,7 @@ csilvers is Craig Silverstein ## Running genqlient -It's already checked in to github, but to generate `generated.go`: +It's already checked in to github, but to regenerate `generated.go`: ```sh go generate ./... ``` diff --git a/example/caller.go b/example/caller.go index 5be7c70..9cdd8b6 100644 --- a/example/caller.go +++ b/example/caller.go @@ -34,12 +34,6 @@ func Main() { return } - if len(os.Args) != 2 { - err = fmt.Errorf("usage: %v ", os.Args[0]) - return - } - username := os.Args[1] - httpClient := http.Client{ Transport: &authedTransport{ key: key, @@ -48,17 +42,25 @@ func Main() { } graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient) - viewerResp, err := getViewer(context.Background(), graphqlClient) - if err != nil { - return - } - fmt.Println("you are", viewerResp.Viewer.MyName) + switch len(os.Args) { + case 1: + viewerResp, err := getViewer(context.Background(), graphqlClient) + if err != nil { + return + } + fmt.Println("you are", viewerResp.Viewer.MyName) - userResp, err := getUser(context.Background(), graphqlClient, username) - if err != nil { - return + case 2: + username := os.Args[1] + userResp, err := getUser(context.Background(), graphqlClient, username) + if err != nil { + return + } + fmt.Println(username, "is", userResp.User.TheirName) + + default: + err = fmt.Errorf("usage: %v [username]", os.Args[0]) } - fmt.Println(username, "is", userResp.User.TheirName) } //go:generate go run github.com/Khan/genqlient genqlient.yaml diff --git a/generate/example_test.go b/generate/example_test.go index af18669..3837387 100644 --- a/generate/example_test.go +++ b/generate/example_test.go @@ -3,19 +3,25 @@ package generate import ( "bytes" "io/ioutil" + "os" + "os/exec" "path/filepath" "runtime" + "strings" "testing" ) -func TestGenerateExample(t *testing.T) { +func getRepoRoot(t *testing.T) string { _, thisFile, _, ok := runtime.Caller(0) if !ok { t.Fatal("runtime.Caller non-ok") } - repoRoot := filepath.Dir(filepath.Dir(thisFile)) - configFilename := filepath.Join(repoRoot, "example/genqlient.yaml") + return filepath.Dir(filepath.Dir(thisFile)) +} + +func TestGenerateExample(t *testing.T) { + configFilename := filepath.Join(getRepoRoot(t), "example/genqlient.yaml") config, err := ReadAndValidateConfig(configFilename) if err != nil { t.Fatal(err) @@ -39,3 +45,22 @@ func TestGenerateExample(t *testing.T) { } } } + +func TestRunExample(t *testing.T) { + if _, ok := os.LookupEnv("GITHUB_TOKEN"); !ok { + t.Skip("requires GITHUB_TOKEN to be set") + } + + cmd := exec.Command("go", "run", "./example/cmd/example", "benjaminjkraft") + cmd.Dir = getRepoRoot(t) + out, err := cmd.CombinedOutput() + if err != nil { + t.Error(err) + } + + got := strings.TrimSpace(string(out)) + want := "benjaminjkraft is Ben Kraft" + if got != want { + t.Errorf("output incorrect\ngot:\n%s\nwant:\n%s", got, want) + } +}