-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_test.go
182 lines (165 loc) · 4.12 KB
/
generate_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package trygo_test
import (
"github.com/rhysd/go-fakeio"
"github.com/rhysd/go-tmpenv"
"github.com/rhysd/trygo"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGenerateOK(t *testing.T) {
base := filepath.Join("testdata", "gen", "ok")
es, err := ioutil.ReadDir(base)
if err != nil {
t.Fatal(err)
}
outDir := filepath.Join(base, "HAVE")
for _, e := range es {
if !e.IsDir() {
continue
}
name := e.Name()
if name == "WANT" || name == "HAVE" {
continue
}
t.Run(name, func(t *testing.T) {
fake := fakeio.Stdout()
defer fake.Restore()
os.RemoveAll(filepath.Join(base, "HAVE", name))
dir := filepath.Join(base, name)
gen, err := trygo.NewGen(outDir)
if err != nil {
t.Fatal(err)
}
if err := gen.Generate([]string{dir}, true); err != nil {
t.Fatal(err)
}
if s, err := os.Stat(outDir); err != nil || !s.IsDir() {
t.Fatal(err, s)
}
paths := []string{}
if err := filepath.Walk(filepath.Join(base, "WANT", name), func(wantPath string, info os.FileInfo, err error) error {
if err != nil {
t.Fatal(wantPath, err)
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(wantPath, ".go") {
t.Fatal("Not Go source", wantPath)
}
havePath := strings.Replace(wantPath, filepath.FromSlash("/WANT/"), filepath.FromSlash("/HAVE/"), 1)
b, err := ioutil.ReadFile(wantPath)
if err != nil {
t.Fatal(err)
}
want := string(b)
b, err = ioutil.ReadFile(havePath)
if err != nil {
t.Fatal(err)
}
have := string(b)
if want != have {
t.Fatalf("Translation result does not match at %s\nwanted:\n%s\nbut have:\n%s\n", havePath, want, have)
}
paths = append(paths, havePath)
return nil
}); err != nil {
t.Fatal(err)
}
stdout, err := fake.String()
if err != nil {
t.Fatal(err)
}
for _, path := range paths {
want := filepath.Dir(path)
if !strings.Contains(stdout, want) {
t.Error(want, "is not contained in stdout:", stdout)
}
}
})
}
}
func TestGenerateGoGenerateOK(t *testing.T) {
defer os.Chdir(cwd)
base := filepath.FromSlash("testdata/gen/ok/simple/")
if err := os.Chdir(base); err != nil {
t.Fatal(err)
}
tmp := tmpenv.New("GOFILE")
defer tmp.Restore()
os.Setenv("GOFILE", "foo.go")
gen, err := trygo.NewGen(".")
if err != nil {
t.Fatal(err)
}
dirs, err := gen.PackageDirs([]string{})
if err != nil {
t.Fatal(err)
}
if len(dirs) != 1 || dirs[0] != cwd {
t.Fatal("Current working dir must be set as package dir:", dirs)
}
}
func TestGenerateFindPackageDirsError(t *testing.T) {
for _, tc := range []struct {
what string
paths []string
want string
}{
{
what: "no path",
paths: []string{},
want: "not run from `go generate` and no path is given",
},
{
what: "directory not exist",
paths: []string{"/path/to/unknown"},
want: "Cannot read directory",
},
{
what: "not a go package",
paths: []string{filepath.FromSlash("testdata/gen/error/empty")},
want: "No Go package is included in given paths",
},
} {
t.Run(tc.what, func(t *testing.T) {
gen, err := trygo.NewGen(".")
if err != nil {
t.Fatal(err)
}
if _, err := gen.PackageDirs(tc.paths); err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatal("Unexpected error:", err)
}
if err := gen.Generate(tc.paths, false); err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatal("Unexpected error:", err)
}
})
}
}
func TestGenerateNewGenError(t *testing.T) {
_, err := trygo.NewGen("")
if err == nil || !strings.Contains(err.Error(), "Output directory must be given") {
t.Fatal("Unexpected error:", err)
}
}
func TestGenPackageDirsOutdirIsInInpath(t *testing.T) {
inpath := filepath.Join(cwd, "testdata", "gen", "pkgdirs", "outpath-is-in-inpath")
outdir := filepath.Join(inpath, "out") // Out path is subdirectory of input path
gen, err := trygo.NewGen(outdir)
if err != nil {
t.Fatal(err)
}
dirs, err := gen.PackageDirs([]string{inpath})
if err != nil {
t.Fatal(err)
}
if len(dirs) != 1 {
t.Fatal(dirs)
}
if dirs[0] == outdir {
t.Fatal("output directory must not be translated")
}
}