Skip to content

Commit

Permalink
Actually build the generated code in tests
Browse files Browse the repository at this point in the history
This revealed one bug already (a redux of #43)!  And will hopefully help
more as unmarshalers get more complicated (thanks to
interfaces/fragments).
  • Loading branch information
benjaminjkraft committed Jun 3, 2021
1 parent 1f442da commit dfc9f02
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generate/testdata/tmp
37 changes: 37 additions & 0 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package generate

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -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))
}
})
})
}
}
Expand Down
Empty file added generate/testdata/tmp/.gitkeep
Empty file.

0 comments on commit dfc9f02

Please sign in to comment.