github.com/fortexxx/gqlgen@v0.10.3-0.20191216030626-ca5ea8b21ead/plugin/resolvergen/resolver_test.go (about)

     1  package resolvergen
     2  
     3  import (
     4  	"fmt"
     5  	"syscall"
     6  	"testing"
     7  
     8  	"github.com/99designs/gqlgen/codegen"
     9  	"github.com/99designs/gqlgen/codegen/config"
    10  	"github.com/stretchr/testify/require"
    11  	"golang.org/x/tools/go/packages"
    12  )
    13  
    14  func TestPlugin(t *testing.T) {
    15  	_ = syscall.Unlink("testdata/out/resolver.go")
    16  
    17  	cfg, err := config.LoadConfig("testdata/gqlgen.yml")
    18  	require.NoError(t, err)
    19  	p := Plugin{}
    20  
    21  	data, err := codegen.BuildData(cfg, nil)
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  
    26  	require.NoError(t, p.GenerateCode(data))
    27  	assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/testdata/out")
    28  }
    29  
    30  func assertNoErrors(t *testing.T, pkg string) {
    31  	pkgs, err := packages.Load(&packages.Config{
    32  		Mode: packages.LoadTypes,
    33  	}, pkg)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	hasErrors := false
    39  	for _, pkg := range pkgs {
    40  		for _, err := range pkg.Errors {
    41  			hasErrors = true
    42  			fmt.Println(err.Pos + ":" + err.Msg)
    43  		}
    44  	}
    45  	if hasErrors {
    46  		t.Fatal("see compilation errors above")
    47  	}
    48  }