github.com/HaswinVidanage/gqlgen@v0.8.1-0.20220609041233-69528c1bf712/codegen/config/config_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestLoadConfig(t *testing.T) { 13 t.Run("config does not exist", func(t *testing.T) { 14 _, err := LoadConfig("doesnotexist.yml") 15 require.Error(t, err) 16 }) 17 18 t.Run("malformed config", func(t *testing.T) { 19 _, err := LoadConfig("testdata/cfg/malformedconfig.yml") 20 require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into config.Config") 21 }) 22 23 t.Run("unknown keys", func(t *testing.T) { 24 _, err := LoadConfig("testdata/cfg/unknownkeys.yml") 25 require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type config.Config") 26 }) 27 } 28 29 func TestLoadDefaultConfig(t *testing.T) { 30 testDir, err := os.Getwd() 31 require.NoError(t, err) 32 var cfg *Config 33 34 t.Run("will find closest match", func(t *testing.T) { 35 err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "subdir")) 36 require.NoError(t, err) 37 38 cfg, err = LoadConfigFromDefaultLocations() 39 require.NoError(t, err) 40 require.Equal(t, StringList{"inner"}, cfg.SchemaFilename) 41 }) 42 43 t.Run("will find config in parent dirs", func(t *testing.T) { 44 err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "otherdir")) 45 require.NoError(t, err) 46 47 cfg, err = LoadConfigFromDefaultLocations() 48 require.NoError(t, err) 49 require.Equal(t, StringList{"outer"}, cfg.SchemaFilename) 50 }) 51 52 t.Run("will return error if config doesn't exist", func(t *testing.T) { 53 err = os.Chdir(testDir) 54 require.NoError(t, err) 55 56 cfg, err = LoadConfigFromDefaultLocations() 57 require.True(t, os.IsNotExist(err)) 58 }) 59 } 60 61 func TestReferencedPackages(t *testing.T) { 62 t.Run("valid", func(t *testing.T) { 63 tm := TypeMap{ 64 "Foo": {Model: StringList{"github.com/test.Foo"}}, 65 "Bar": {Model: StringList{"github.com/test.Bar"}}, 66 "Baz": {Model: StringList{"github.com/otherpkg.Baz"}}, 67 "Map": {Model: StringList{"map[string]interface{}"}}, 68 "SkipResolver": { 69 Fields: map[string]TypeMapField{ 70 "field": {Resolver: false}, 71 }, 72 }, 73 } 74 75 pkgs := tm.ReferencedPackages() 76 77 assert.Equal(t, []string{"github.com/test", "github.com/otherpkg"}, pkgs) 78 }) 79 80 } 81 82 func TestConfigCheck(t *testing.T) { 83 t.Run("invalid config format due to conflicting package names", func(t *testing.T) { 84 config, err := LoadConfig("testdata/cfg/conflictedPackages.yml") 85 require.NoError(t, err) 86 87 err = config.normalize() 88 require.NoError(t, err) 89 90 err = config.Check() 91 require.EqualError(t, err, "filenames exec.go and models.go are in the same directory but have different package definitions") 92 }) 93 }