Files
genqlient/generate/example_test.go
T
Ben KraftandGitHub 700392315a Enable golangci-lint (#49)
## Summary:
It would be nice to have some linting beyond `go vet`!  Now we do.  I
started by copying the config from Khan/webapp.  I did remove a couple
of staticcheck checks that I didn't feel were useful.  (Note also that
exportloopref is the replacement for scopelint in newer golangci-lint.)

Included are all the needed lint fixes; most are stylistic but the
changes in the example are a (minor) bugfix.

Fixes #22.
Issue: https://github.com/Khan/genqlient/issues/22

## Test plan:
make check

Author: benjaminjkraft

Reviewers: aberkan, dnerdy, benjaminjkraft, csilvers, MiguelCastillo

Required Reviewers: 

Approved by: aberkan, dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint

Pull request URL: https://github.com/Khan/genqlient/pull/49
2021-08-20 10:39:12 -07:00

68 lines
1.4 KiB
Go

package generate
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func getRepoRoot(t *testing.T) string {
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller non-ok")
}
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)
}
generated, err := Generate(config)
if err != nil {
t.Fatal(err)
}
for filename, content := range generated {
expectedContent, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, expectedContent) {
t.Errorf("mismatch in %s", filename)
if testing.Verbose() {
t.Errorf("got:\n%s\nwant:\n%s\n", content, expectedContent)
}
}
}
}
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 created on 2009-08-03"
if got != want {
t.Errorf("output incorrect\ngot:\n%s\nwant:\n%s", got, want)
}
}