Add some more tests for config validation, and fix some gaps (#274)
I set these tests up in #270, but I realized there's a lot more we could test unrelated to that PR. In this commit I add some more tests. They're not really exhaustive yet, but they did catch a few bugs, which I fixed: - we weren't validating the package-name if you do set it (only if we guess it) - If you omitted `generated`, you would try to write generated code to the directory containing `genqlient.yaml`, which makes no sense; now we default to `generated.go`. In the real world it's probably good to set explicitly, but it's actually very convenient in tests that we don't have to, and maybe in small projects too.
This commit is contained in:
+30
-7
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
const (
|
||||
findConfigDir = "testdata/find-config"
|
||||
validConfigDir = "testdata/valid-config"
|
||||
invalidConfigDir = "testdata/invalid-config"
|
||||
)
|
||||
|
||||
@@ -114,18 +115,40 @@ func TestAbsoluteAndRelativePathsInConfigFiles(t *testing.T) {
|
||||
require.Equal(t, "/tmp/genqlient.graphql", config.Operations[0])
|
||||
}
|
||||
|
||||
func TestInvalidConfigs(t *testing.T) {
|
||||
files, err := os.ReadDir(invalidConfigDir)
|
||||
func testAllSnapshots(
|
||||
t *testing.T,
|
||||
dir string,
|
||||
testfunc func(t *testing.T, filename string),
|
||||
) {
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
t.Run(file.Name(), func(t *testing.T) {
|
||||
filename := filepath.Join(invalidConfigDir, file.Name())
|
||||
_, err := ReadAndValidateConfig(filename)
|
||||
require.Error(t, err)
|
||||
testutil.Cupaloy.SnapshotT(t, err.Error())
|
||||
name := file.Name()
|
||||
if name[0] == '.' {
|
||||
continue // editor backup files, etc.
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
filename := filepath.Join(dir, file.Name())
|
||||
testfunc(t, filename)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidConfigs(t *testing.T) {
|
||||
testAllSnapshots(t, validConfigDir, func(t *testing.T, filename string) {
|
||||
config, err := ReadAndValidateConfig(filename)
|
||||
require.NoError(t, err)
|
||||
testutil.Cupaloy.SnapshotT(t, config)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInvalidConfigs(t *testing.T) {
|
||||
testAllSnapshots(t, invalidConfigDir, func(t *testing.T, filename string) {
|
||||
_, err := ReadAndValidateConfig(filename)
|
||||
require.Error(t, err)
|
||||
testutil.Cupaloy.SnapshotT(t, err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user