Skip to content

Commit

Permalink
Read gqlgen.yml from io.Reader. (#2607)
Browse files Browse the repository at this point in the history
* Update config.go

Add ReadConfig

* Add tests

* Update config_test.go

remove extra space to fix lint checks

* Update config.go

Need to return the config
  • Loading branch information
smac89 authored Apr 9, 2023
1 parent 50c2829 commit 7bc1f62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
11 changes: 8 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -100,14 +101,18 @@ var path2regex = strings.NewReplacer(

// LoadConfig reads the gqlgen.yml config file
func LoadConfig(filename string) (*Config, error) {
config := DefaultConfig()

b, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to read config: %w", err)
}

dec := yaml.NewDecoder(bytes.NewReader(b))
return ReadConfig(bytes.NewReader(b))
}

func ReadConfig(cfgFile io.Reader) (*Config, error) {
config := DefaultConfig()

dec := yaml.NewDecoder(cfgFile)
dec.KnownFields(true)

if err := dec.Decode(config); err != nil {
Expand Down
20 changes: 16 additions & 4 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,19 +22,29 @@ func TestLoadConfig(t *testing.T) {
_, err := LoadConfig("doesnotexist.yml")
require.Error(t, err)
})
}

func TestReadConfig(t *testing.T) {
t.Run("empty config", func(t *testing.T) {
_, err := ReadConfig(strings.NewReader(""))
require.EqualError(t, err, "unable to parse config: EOF")
})

t.Run("malformed config", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/malformedconfig.yml")
cfgFile, _ := os.Open("testdata/cfg/malformedconfig.yml")
_, err := ReadConfig(cfgFile)
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into config.Config")
})

t.Run("unknown keys", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unknownkeys.yml")
cfgFile, _ := os.Open("testdata/cfg/unknownkeys.yml")
_, err := ReadConfig(cfgFile)
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type config.Config")
})

t.Run("globbed filenames", func(t *testing.T) {
c, err := LoadConfig("testdata/cfg/glob.yml")
cfgFile, _ := os.Open("testdata/cfg/glob.yml")
c, err := ReadConfig(cfgFile)
require.NoError(t, err)

if runtime.GOOS == "windows" {
Expand All @@ -46,7 +57,8 @@ func TestLoadConfig(t *testing.T) {
})

t.Run("unwalkable path", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unwalkable.yml")
cfgFile, _ := os.Open("testdata/cfg/unwalkable.yml")
_, err := ReadConfig(cfgFile)
if runtime.GOOS == "windows" {
require.EqualError(t, err, "failed to walk schema at root not_walkable/: CreateFile not_walkable/: The system cannot find the file specified.")
} else {
Expand Down

0 comments on commit 7bc1f62

Please sign in to comment.