Skip to content

Commit

Permalink
Merge pull request #1134 from seriousben/fix-default-config-no-ast-so…
Browse files Browse the repository at this point in the history
…urces

Add LoadDefaultConfig to load the schema by default
  • Loading branch information
vektah authored Jul 26, 2020
2 parents 1b23cf1 + 35a9048 commit 39a12e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
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.

0 comments on commit 39a12e0

Please sign in to comment.