-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync_test.go
118 lines (100 loc) · 2.43 KB
/
sync_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
package goast_test
import (
"bytes"
"encoding/json"
"io"
"io/fs"
"strings"
"testing"
"github.com/m-mizutani/goast"
"github.com/m-mizutani/gt"
)
type buffer struct {
bytes.Buffer
}
type node struct {
Path string
Node map[string]any
Kind string
}
func (x *buffer) Close() error { return nil }
func TestSync(t *testing.T) {
code := strings.NewReader(
`package main
import "fmt"
func main() {
fmt.Println("test1") // goast.sync:test/sync/dir/dest1.json
fmt.Println("test2") // goast.sync: test/sync/dir/dest2.json
}
`,
)
buf := &buffer{}
var walkCount, mkdirCount, createCount int
g := goast.New(
goast.WithWalkFunc(func(src string, cb func(fpath string, r io.Reader) error) error {
walkCount++
return cb("testcode.go", code)
}),
goast.WithMkDirFunc(func(path string, perm fs.FileMode) error {
mkdirCount++
gt.V(t, path).Equal("test/sync/dir")
gt.V(t, perm).Equal(fs.FileMode(0755))
return nil
}),
goast.WithCreateFunc(func(path string) (io.WriteCloser, error) {
files := []string{
"test/sync/dir/dest1.json",
"test/sync/dir/dest2.json",
}
gt.V(t, path).Equal(files[createCount])
createCount++
return buf, nil
}),
)
gt.NoError(t, g.Sync("src_dir")).Must()
gt.V(t, walkCount).Equal(1)
gt.V(t, mkdirCount).Equal(2)
gt.V(t, createCount).Equal(2)
var out node
r := bytes.NewReader(buf.Bytes())
gt.NoError(t, json.NewDecoder(r).Decode(&out)).Must()
gt.V(t, out.Kind).Equal("ExprStmt")
}
func TestSyncFile(t *testing.T) {
code := strings.NewReader(
`// goast.sync: test/file.json
package main
import "fmt"
func main() {
fmt.Println("test")
}
`,
)
buf := &buffer{}
var walkCount, mkdirCount, createCount int
g := goast.New(
goast.WithWalkFunc(func(src string, cb func(fpath string, r io.Reader) error) error {
walkCount++
return cb("testcode.go", code)
}),
goast.WithMkDirFunc(func(path string, perm fs.FileMode) error {
mkdirCount++
gt.V(t, path).Equal("test")
gt.V(t, perm).Equal(fs.FileMode(0755))
return nil
}),
goast.WithCreateFunc(func(path string) (io.WriteCloser, error) {
createCount++
gt.V(t, path).Equal("test/file.json")
return buf, nil
}),
)
gt.NoError(t, g.Sync("src_dir")).Must()
gt.V(t, walkCount).Equal(1)
gt.V(t, mkdirCount).Equal(1)
gt.V(t, createCount).Equal(1)
var out node
r := bytes.NewReader(buf.Bytes())
gt.NoError(t, json.NewDecoder(r).Decode(&out)).Must()
gt.V(t, out.Kind).Equal("File")
}