-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate_test.go
168 lines (149 loc) · 3.69 KB
/
translate_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
package trygo_test
import (
"bytes"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/rhysd/trygo"
)
func replaceLast(s, old, new string) string {
idx := strings.LastIndex(s, old)
if idx == -1 {
return s
}
return s[:idx] + new + s[idx+len(old):]
}
func collectPackagesUnder(dirpath string, t *testing.T) []*trygo.Package {
fset := token.NewFileSet()
saw := map[string]*trygo.Package{}
if err := filepath.Walk(dirpath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(p, ".go") {
return nil
}
dir := filepath.Dir(p)
if _, ok := saw[dir]; ok {
return nil
}
pkgs, err := parser.ParseDir(fset, dir, nil, 0)
if err != nil {
t.Fatal(dirpath, err)
}
for p, pkg := range pkgs {
dest := replaceLast(dir, filepath.FromSlash("/src"), filepath.FromSlash("/want/src"))
saw[p] = trygo.NewPackage(pkg, dir, dest, fset)
}
return nil
}); err != nil {
t.Fatal(dirpath, err)
}
if len(saw) == 0 {
t.Fatal("0 package found under directory " + dirpath)
}
ret := make([]*trygo.Package, 0, len(saw))
for _, pkg := range saw {
ret = append(ret, pkg)
}
return ret
}
func TestTranslationOK(t *testing.T) {
base := filepath.Join(cwd, "testdata", "trans", "ok")
entries, err := ioutil.ReadDir(base)
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
dir := filepath.Join(base, entry.Name())
t.Run(entry.Name(), func(t *testing.T) {
pkgs := collectPackagesUnder(filepath.Join(dir, "src"), t)
if err := trygo.Translate(pkgs); err != nil {
t.Fatal(err)
}
for _, pkg := range pkgs {
shouldModified := ""
fs, err := ioutil.ReadDir(pkg.Path)
if err != nil {
t.Fatal("Want dir does not exist", err, pkg.Path, pkg.Node.Name)
}
// Check all wanted files were translated and their contents are as expected
for _, f := range fs {
name := f.Name()
if f.IsDir() || !strings.HasSuffix(name, ".go") {
continue
}
path := filepath.Join(pkg.Path, name)
if !strings.HasPrefix(name, "skip") {
shouldModified = path
}
var buf bytes.Buffer
if err := pkg.WriteFileTo(&buf, path); err != nil {
t.Fatal(err, pkg.Node.Files)
}
have := buf.String()
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
want := string(b)
if want != have {
t.Fatalf("Translated source is unexpected.\nWanted:\n%s\n\nHave:\n%s\n", want, have)
}
}
if shouldModified == "" {
if pkg.Modified() {
t.Fatal("No file was modified but pkg.Modified() returned true", pkg)
}
} else {
if !pkg.Modified() {
t.Fatal(shouldModified, "was modified but pkg.Modified() returned false", pkg)
}
}
}
})
}
}
func TestTranslationError(t *testing.T) {
base := filepath.Join(cwd, "testdata", "trans", "error")
entries, err := ioutil.ReadDir(base)
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
dir := filepath.Join(base, entry.Name())
t.Run(entry.Name(), func(t *testing.T) {
pkgs := collectPackagesUnder(dir, t)
err := trygo.Translate(pkgs)
if err == nil {
t.Fatal("Error did not occur:", pkgs)
}
have := err.Error()
b, err := ioutil.ReadFile(filepath.Join(dir, "message.txt"))
if err != nil {
t.Fatal("message.txt cannot be read:", err)
}
for _, want := range strings.Split(string(b), "\n") {
if want == "" {
continue
}
if !strings.Contains(have, want) {
t.Fatalf("Error is unexpected. Wanted %q to be included in %q", want, have)
}
}
})
}
}