github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/api/generate_test.go (about)

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