Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoadDefaultConfig to load the schema by default #1134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ var genCmd = &cli.Command{
} else {
cfg, err = config.LoadConfigFromDefaultLocations()
if os.IsNotExist(errors.Cause(err)) {
cfg = config.DefaultConfig()
} else if err != nil {
cfg, err = config.LoadDefaultConfig()
}

if err != nil {
return err
}
}
Expand Down
19 changes: 19 additions & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ func DefaultConfig() *Config {
}
}

// LoadDefaultConfig loads the default config so that it is ready to be used
func LoadDefaultConfig() (*Config, error) {
config := DefaultConfig()

for _, filename := range config.SchemaFilename {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrap(err, "unable to open schema")
}

config.Sources = append(config.Sources, &ast.Source{Name: filename, Input: string(schemaRaw)})
}

return config, nil
}

// LoadConfigFromDefaultLocations looks for a config file in the current directory, and all parent directories
// walking up the tree. The closest config file will be returned.
func LoadConfigFromDefaultLocations() (*Config, error) {
Expand Down
26 changes: 25 additions & 1 deletion codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2"
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestLoadConfig(t *testing.T) {
})
}

func TestLoadDefaultConfig(t *testing.T) {
func TestLoadConfigFromDefaultLocation(t *testing.T) {
testDir, err := os.Getwd()
require.NoError(t, err)
var cfg *Config
Expand Down Expand Up @@ -85,6 +86,29 @@ func TestLoadDefaultConfig(t *testing.T) {
})
}

func TestLoadDefaultConfig(t *testing.T) {
testDir, err := os.Getwd()
require.NoError(t, err)
var cfg *Config

t.Run("will find the schema", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "defaultconfig"))
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
require.NoError(t, err)
require.NotEmpty(t, cfg.Sources)
})

t.Run("will return error if schema doesn't exist", func(t *testing.T) {
err = os.Chdir(testDir)
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
require.True(t, os.IsNotExist(errors.Cause(err)))
})
}

func TestReferencedPackages(t *testing.T) {
t.Run("valid", func(t *testing.T) {
tm := TypeMap{
Expand Down
Empty file.