forked from Khan/genqlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
108 lines (95 loc) · 2.35 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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)
}
})
}
}
func TestAbsoluteAndRelativePathsInConfigFiles(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(t, err)
config, err := ReadAndValidateConfig(
cwd + "/testdata/find-config/current/genqlient.yaml")
require.NoError(t, err)
require.Equal(t, 1, len(config.Schema))
require.Equal(
t,
cwd+"/testdata/find-config/current/schema.graphql",
config.Schema[0],
)
require.Equal(t, 1, len(config.Operations))
require.Equal(t, "/tmp/genqlient.graphql", config.Operations[0])
}