proper test wiring for example test in github actions
This commit is contained in:
@@ -25,11 +25,8 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
|
||||||
go test -v ./...
|
|
||||||
|
|
||||||
- name: Run example
|
|
||||||
env:
|
env:
|
||||||
|
# Needed for the example-test to run.
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
go run ./example/cmd/example/main.go csilvers
|
go test -v ./...
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
example:
|
example:
|
||||||
go generate ./...
|
go generate ./...
|
||||||
go run ./example/cmd/example/main.go csilvers
|
go run ./example/cmd/example/main.go
|
||||||
|
|
||||||
check:
|
check:
|
||||||
go test ./...
|
go test ./...
|
||||||
|
|||||||
@@ -78,11 +78,7 @@ If you'd like to contribute to genqlient, welcome! The library is still in a so
|
|||||||
|
|
||||||
### Tests
|
### Tests
|
||||||
|
|
||||||
`go test ./...` tests code generation. (This is run by GitHub Actions.) Most of the tests are snapshot-based; see `generate/generate_test.go`.
|
`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.
|
||||||
|
|
||||||
`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.
|
|
||||||
|
|
||||||
### Design
|
### Design
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -1,9 +1,7 @@
|
|||||||
# example of genqlient
|
# example of genqlient
|
||||||
|
|
||||||
## Getting a token
|
|
||||||
Get a token from [GitHub](https://github.com/settings/tokens/new) (no scopes needed).
|
|
||||||
|
|
||||||
## Invoking the example
|
## 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:
|
To run the example:
|
||||||
|
|
||||||
@@ -15,7 +13,7 @@ csilvers is Craig Silverstein
|
|||||||
|
|
||||||
## Running genqlient
|
## 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
|
```sh
|
||||||
go generate ./...
|
go generate ./...
|
||||||
```
|
```
|
||||||
|
|||||||
+17
-15
@@ -34,12 +34,6 @@ func Main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(os.Args) != 2 {
|
|
||||||
err = fmt.Errorf("usage: %v <username>", os.Args[0])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
username := os.Args[1]
|
|
||||||
|
|
||||||
httpClient := http.Client{
|
httpClient := http.Client{
|
||||||
Transport: &authedTransport{
|
Transport: &authedTransport{
|
||||||
key: key,
|
key: key,
|
||||||
@@ -48,17 +42,25 @@ func Main() {
|
|||||||
}
|
}
|
||||||
graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient)
|
graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient)
|
||||||
|
|
||||||
viewerResp, err := getViewer(context.Background(), graphqlClient)
|
switch len(os.Args) {
|
||||||
if err != nil {
|
case 1:
|
||||||
return
|
viewerResp, err := getViewer(context.Background(), graphqlClient)
|
||||||
}
|
if err != nil {
|
||||||
fmt.Println("you are", viewerResp.Viewer.MyName)
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("you are", viewerResp.Viewer.MyName)
|
||||||
|
|
||||||
userResp, err := getUser(context.Background(), graphqlClient, username)
|
case 2:
|
||||||
if err != nil {
|
username := os.Args[1]
|
||||||
return
|
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
|
//go:generate go run github.com/Khan/genqlient genqlient.yaml
|
||||||
|
|||||||
@@ -3,19 +3,25 @@ package generate
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenerateExample(t *testing.T) {
|
func getRepoRoot(t *testing.T) string {
|
||||||
_, thisFile, _, ok := runtime.Caller(0)
|
_, thisFile, _, ok := runtime.Caller(0)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("runtime.Caller non-ok")
|
t.Fatal("runtime.Caller non-ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
repoRoot := filepath.Dir(filepath.Dir(thisFile))
|
return filepath.Dir(filepath.Dir(thisFile))
|
||||||
configFilename := filepath.Join(repoRoot, "example/genqlient.yaml")
|
}
|
||||||
|
|
||||||
|
func TestGenerateExample(t *testing.T) {
|
||||||
|
configFilename := filepath.Join(getRepoRoot(t), "example/genqlient.yaml")
|
||||||
config, err := ReadAndValidateConfig(configFilename)
|
config, err := ReadAndValidateConfig(configFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user