-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Walk parent directories to find config file (#141)
Code is mostly borrowed from [gqlgen](https://github.com/99designs/gqlgen). The idea here is that I want to be able to store `genqlient.yaml` at the top-level, but my client code lives down in `graph/client/`. I put the `//go:generate` line in `graph/client/client.go`.
- Loading branch information
1 parent
9ecc62e
commit 10dc388
Showing
14 changed files
with
193 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package generate | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFindCfg(t *testing.T) { | ||
cwd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
cases := map[string]struct { | ||
startDir string | ||
expectedCfg string | ||
expectedErr error | ||
}{ | ||
"yaml in parent directory": { | ||
startDir: cwd + "/testdata/find-config/parent/child", | ||
expectedCfg: cwd + "/testdata/find-config/parent/genqlient.yaml", | ||
}, | ||
"yaml in current directory": { | ||
startDir: cwd + "/testdata/find-config/current", | ||
expectedCfg: cwd + "/testdata/find-config/current/genqlient.yaml", | ||
}, | ||
"no yaml": { | ||
startDir: cwd + "/testdata/find-config/none/child", | ||
expectedErr: os.ErrNotExist, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
defer func() { | ||
require.NoError(t, os.Chdir(cwd), "Test cleanup failed") | ||
}() | ||
|
||
err = os.Chdir(tc.startDir) | ||
require.NoError(t, err) | ||
|
||
path, err := findCfg() | ||
assert.Equal(t, tc.expectedCfg, path) | ||
assert.Equal(t, tc.expectedErr, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestFindCfgInDir(t *testing.T) { | ||
cwd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
cases := map[string]struct { | ||
startDir string | ||
found bool | ||
}{ | ||
"yaml": { | ||
startDir: cwd + "/testdata/find-config/filenames/yaml", | ||
found: true, | ||
}, | ||
"yml": { | ||
startDir: cwd + "/testdata/find-config/filenames/yml", | ||
found: true, | ||
}, | ||
".yaml": { | ||
startDir: cwd + "/testdata/find-config/filenames/dotyaml", | ||
found: true, | ||
}, | ||
".yml": { | ||
startDir: cwd + "/testdata/find-config/filenames/dotyml", | ||
found: true, | ||
}, | ||
"none": { | ||
startDir: cwd + "/testdata/find-config/filenames/none", | ||
found: false, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
path := findCfgInDir(tc.startDir) | ||
if tc.found { | ||
assert.NotEmpty(t, path) | ||
} else { | ||
assert.Empty(t, path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Default genqlient config; for full documentation see: | ||
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml | ||
schema: schema.graphql | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go |
6 changes: 6 additions & 0 deletions
6
generate/testdata/find-config/filenames/dotyaml/.genqlient.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Default genqlient config; for full documentation see: | ||
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml | ||
schema: schema.graphql | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go |
6 changes: 6 additions & 0 deletions
6
generate/testdata/find-config/filenames/dotyml/.genqlient.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Default genqlient config; for full documentation see: | ||
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml | ||
schema: schema.graphql | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Default genqlient config; for full documentation see: | ||
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml | ||
schema: schema.graphql | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Default genqlient config; for full documentation see: | ||
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml | ||
schema: schema.graphql | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Default genqlient config; for full documentation see: | ||
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml | ||
schema: schema.graphql | ||
operations: | ||
- genqlient.graphql | ||
generated: generated.go |