Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 18, 2019
2 parents 647c62a + 015d02e commit 0eb8b5c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
26 changes: 26 additions & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,35 @@ func (c *Config) Check() error {
}
}

// check packages names against conflict, if present in the same dir
// and check filenames for uniqueness
packageConfigList := []PackageConfig{
c.Model,
c.Exec,
c.Resolver,
}
filesMap := make(map[string]bool)
pkgConfigsByDir := make(map[string]PackageConfig)
for _, current := range packageConfigList {
_, fileFound := filesMap[current.Filename]
if fileFound {
return fmt.Errorf("filename %s defined more than once", current.Filename)
}
filesMap[current.Filename] = true
previous, inSameDir := pkgConfigsByDir[current.Dir()]
if inSameDir && current.Package != previous.Package {
return fmt.Errorf("filenames %s and %s are in the same directory but have different package definitions", stripPath(current.Filename), stripPath(previous.Filename))
}
pkgConfigsByDir[current.Dir()] = current
}

return c.normalize()
}

func stripPath(path string) string {
return filepath.Base(path)
}

type TypeMap map[string]TypeMapEntry

func (tm TypeMap) Exists(typeName string) bool {
Expand Down
13 changes: 13 additions & 0 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,16 @@ func TestReferencedPackages(t *testing.T) {
})

}

func TestConfigCheck(t *testing.T) {
t.Run("invalid config format due to conflicting package names", func(t *testing.T) {
config, err := LoadConfig("testdata/cfg/conflictedPackages.yml")
require.NoError(t, err)

err = config.normalize()
require.NoError(t, err)

err = config.check()
require.EqualError(t, err, "filenames exec.go and models.go are in the same directory but have different package definitions")
})
}
10 changes: 10 additions & 0 deletions codegen/testdata/cfg/conflictedPackages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
schema:
- schema.graphql
exec:
filename: generated/exec.go
package: graphql
model:
filename: generated/models.go
resolver:
filename: generated/resolver.go
type: Resolver
2 changes: 1 addition & 1 deletion plugin/modelgen/testdata/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ schema:
- "testdata/schema.graphql"

exec:
filename: out/generated.go
filename: out/ignored.go
model:
filename: out/generated.go

Expand Down

0 comments on commit 0eb8b5c

Please sign in to comment.