github.com/shish-dev/gqlgen@v0.99.0/api/generate_test.go (about) 1 package api 2 3 import ( 4 "os" 5 "path" 6 "testing" 7 8 "github.com/99designs/gqlgen/codegen/config" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func cleanup(workDir string) { 13 _ = os.Remove(path.Join(workDir, "server.go")) 14 _ = os.RemoveAll(path.Join(workDir, "graph", "generated")) 15 _ = os.Remove(path.Join(workDir, "graph", "resolver.go")) 16 _ = os.Remove(path.Join(workDir, "graph", "schema.resolvers.go")) 17 _ = os.Remove(path.Join(workDir, "graph", "model", "models_gen.go")) 18 } 19 20 func TestGenerate(t *testing.T) { 21 wd, _ := os.Getwd() 22 type args struct { 23 workDir string 24 } 25 tests := []struct { 26 name string 27 args args 28 wantErr bool 29 }{ 30 { 31 name: "default", 32 args: args{ 33 workDir: path.Join(wd, "testdata", "default"), 34 }, 35 wantErr: false, 36 }, 37 } 38 for _, tt := range tests { 39 t.Run(tt.name, func(t *testing.T) { 40 defer func() { 41 cleanup(tt.args.workDir) 42 _ = os.Chdir(wd) 43 }() 44 _ = os.Chdir(tt.args.workDir) 45 cfg, err := config.LoadConfigFromDefaultLocations() 46 require.Nil(t, err, "failed to load config") 47 if err := Generate(cfg); (err != nil) != tt.wantErr { 48 t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr) 49 } 50 }) 51 } 52 }