Skip to content

Commit

Permalink
Merge pull request #755 from 99designs/fix-globbing-windows
Browse files Browse the repository at this point in the history
fix globbing on windows
  • Loading branch information
vektah committed Jun 24, 2019
2 parents ba176e2 + a4480fb commit 56f3f92
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
20 changes: 13 additions & 7 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func LoadConfigFromDefaultLocations() (*Config, error) {
return LoadConfig(cfgFile)
}

var path2regex = strings.NewReplacer(
`.`, `\.`,
`*`, `.+`,
`\`, `[\\/]`,
`/`, `[\\/]`,
)

// LoadConfig reads the gqlgen.yml config file
func LoadConfig(filename string) (*Config, error) {
config := DefaultConfig()
Expand All @@ -74,18 +81,17 @@ func LoadConfig(filename string) (*Config, error) {
// subdirectories to match schema files.
if strings.Contains(f, "**") {
pathParts := strings.SplitN(f, "**", 2)
rest := strings.TrimPrefix(strings.TrimPrefix(pathParts[1], `\`), `/`)
// turn the rest of the glob into a regex, anchored only at the end because ** allows
// for any number of dirs in between and walk will let us match against the full path name
globRe := regexp.MustCompile(path2regex.Replace(rest) + `$`)

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) {
if globRe.MatchString(strings.TrimPrefix(path, pathParts[0])) {
matches = append(matches, path)
}

Expand Down
16 changes: 13 additions & 3 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -29,13 +30,22 @@ func TestLoadConfig(t *testing.T) {
c, err := LoadConfig("testdata/cfg/glob.yml")
require.NoError(t, err)

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")
if runtime.GOOS == "windows" {
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`)
} else {
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")
if runtime.GOOS == "windows" {
require.EqualError(t, err, "failed to walk schema at root not_walkable/: FindFirstFile not_walkable/: The parameter is incorrect.")
} else {
require.EqualError(t, err, "failed to walk schema at root not_walkable/: lstat not_walkable/: no such file or directory")
}
})
}

Expand Down
4 changes: 2 additions & 2 deletions plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestPlugin(t *testing.T) {
_ = syscall.Unlink("out/resolver.go")
_ = syscall.Unlink("testdata/out/resolver.go")

cfg, err := config.LoadConfig("testdata/gqlgen.yml")
require.NoError(t, err)
Expand All @@ -24,7 +24,7 @@ func TestPlugin(t *testing.T) {
}

require.NoError(t, p.GenerateCode(data))
assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/out")
assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/testdata/out")
}

func assertNoErrors(t *testing.T, pkg string) {
Expand Down
8 changes: 4 additions & 4 deletions plugin/resolvergen/testdata/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ schema:
- "testdata/schema.graphql"

exec:
filename: out/ignored.go
filename: testdata/out/ignored.go
model:
filename: out/generated.go
filename: testdata/out/generated.go
resolver:
filename: out/resolver.go
filename: testdata/out/resolver.go
type: CustomResolverType

models:
Resolver:
model: github.com/99designs/gqlgen/plugin/resolvergen/out.Resolver
model: github.com/99designs/gqlgen/plugin/resolvergen/testdata/out.Resolver
File renamed without changes.
File renamed without changes.

0 comments on commit 56f3f92

Please sign in to comment.