proper test wiring for example test in github actions

This commit is contained in:
Ben Kraft
2021-04-02 18:04:05 -07:00
parent efc6549d7e
commit 9c8cf02181
6 changed files with 51 additions and 33 deletions
+28 -3
View File
@@ -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)
}
}