Skip to content

Commit

Permalink
added schema file globbing fixes 99designs#631
Browse files Browse the repository at this point in the history
  • Loading branch information
cbelsole committed May 18, 2019
1 parent edf5fac commit 32c00f3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
31 changes: 28 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -67,9 +68,33 @@ func LoadConfig(filename string) (*Config, error) {
preGlobbing := config.SchemaFilename
config.SchemaFilename = StringList{}
for _, f := range preGlobbing {
matches, err := filepath.Glob(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to glob schema filename %s", f)
var matches []string

// for ** we want to override default globbing patterns and walk all
// subdirectories to match schema files.
if strings.Contains(f, "**") {
pathParts := strings.SplitN(f, "**", 2)
if err := filepath.Walk(pathParts[0], func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// make sure paths match files
// <root>?.*<filename|.+>\.<ext>
fileRegex := regexp.MustCompile(pathParts[0] + "?.*" + strings.Replace(strings.Replace(pathParts[1], ".", "\\.", -1), "*", ".+", -1))
if fileRegex.MatchString(path) {
matches = append(matches, path)
}

return nil
}); err != nil {
return nil, errors.Wrapf(err, "failed to walk schema at root %s", pathParts[0])
}
} else {
matches, err = filepath.Glob(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to glob schema filename %s", f)
}
}

for _, m := range matches {
Expand Down
14 changes: 14 additions & 0 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -24,6 +25,19 @@ func TestLoadConfig(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unknownkeys.yml")
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")
require.NoError(t, err, "")
fmt.Printf("%+v\n", c)
require.Equal(t, c.SchemaFilename[0], "testdata/cfg/glob/bar/bar with spaces.graphql")
require.Equal(t, c.SchemaFilename[1], "testdata/cfg/glob/foo/foo.graphql")
})

t.Run("unwalkable path", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unwalkable.yml")
require.EqualError(t, err, "failed to walk schema at root not_walkable/: lstat not_walkable/: no such file or directory")
})
}

func TestLoadDefaultConfig(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions codegen/config/testdata/cfg/glob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema:
- testdata/cfg/glob/**/*.graphql
exec:
filename: generated.go
model:
filename: models_gen.go
resolver:
filename: resolver.go
type: Resolver
12 changes: 12 additions & 0 deletions codegen/config/testdata/cfg/glob/bar/bar with spaces.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Query {
todos: [Todo!]!
}

input NewTodo {
text: String!
userId: String!
}

type Mutation {
createTodo(input: NewTodo!): Todo!
}
11 changes: 11 additions & 0 deletions codegen/config/testdata/cfg/glob/foo/foo.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Todo {
id: ID!
text: String!
done: Boolean!
user: User!
}

type User {
id: ID!
name: String!
}
9 changes: 9 additions & 0 deletions codegen/config/testdata/cfg/unwalkable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema:
- not_walkable/**/*.graphql
exec:
filename: generated.go
model:
filename: models_gen.go
resolver:
filename: resolver.go
type: Resolver

0 comments on commit 32c00f3

Please sign in to comment.