Actually build the generated code in tests

This revealed one bug already (a redux of #43)!  And will hopefully help
more as unmarshalers get more complicated (thanks to
interfaces/fragments).
This commit is contained in:
Ben Kraft
2021-06-03 15:48:05 -07:00
parent 1f442da041
commit dfc9f02efc
3 changed files with 38 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
generate/testdata/tmp
+37
View File
@@ -1,8 +1,10 @@
package generate
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
@@ -68,6 +70,41 @@ func TestGenerate(t *testing.T) {
})
// TODO(benkraft): Also check that the code at least builds!
}
t.Run("Build", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping build due to -short")
} else if sourceFilename == "InterfaceNesting.graphql" ||
sourceFilename == "InterfaceNoFragments.graphql" ||
sourceFilename == "Omitempty.graphql" {
t.Skip("TODO: enable these once they build")
}
goContent := generated[goFilename]
// We need to put this within the current module, rather than in
// /tmp, so that it can access internal/testutil.
f, err := ioutil.TempFile("./testdata/tmp", sourceFilename+"_*.go")
if err != nil {
t.Fatal(err)
}
defer func() {
f.Close()
os.Remove(f.Name())
}()
_, err = f.Write(goContent)
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("go", "build", f.Name())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Fatal(fmt.Errorf("generated code does not compile: %w", err))
}
})
})
}
}
View File