forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_test.go
219 lines (195 loc) · 4.74 KB
/
files_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package gnolang_test
import (
"bytes"
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/test"
"github.com/stretchr/testify/require"
)
var withSync = flag.Bool("update-golden-tests", false, "rewrite tests updating Realm: and Output: with new values where changed")
type nopReader struct{}
func (nopReader) Read(p []byte) (int, error) { return 0, io.EOF }
// TestFiles tests all the files in "gnovm/tests/files".
//
// Cheatsheet:
//
// fail on the first test:
// go test -run TestFiles -failfast
// run a specific test:
// go test -run TestFiles/addr0b
// fix a specific test:
// go test -run TestFiles/'^bin1.gno' -short -v -update-golden-tests .
func TestFiles(t *testing.T) {
t.Parallel()
rootDir, err := filepath.Abs("../../../")
require.NoError(t, err)
newOpts := func() *test.TestOptions {
o := &test.TestOptions{
RootDir: rootDir,
Output: io.Discard,
Error: io.Discard,
Sync: *withSync,
}
o.BaseStore, o.TestStore = test.StoreWithOptions(
rootDir, o.WriterForStore(),
test.StoreOptions{WithExtern: true},
)
return o
}
// sharedOpts is used for all "short" tests.
sharedOpts := newOpts()
dir := "../../tests/"
fsys := os.DirFS(dir)
err = fs.WalkDir(fsys, "files", func(path string, de fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case path == "files/extern":
return fs.SkipDir
case de.IsDir():
return nil
}
subTestName := path[len("files/"):]
isLong := strings.HasSuffix(path, "_long.gno")
if isLong && testing.Short() {
t.Run(subTestName, func(t *testing.T) {
t.Skip("skipping in -short")
})
return nil
}
content, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}
var criticalError error
t.Run(subTestName, func(t *testing.T) {
opts := sharedOpts
if isLong {
// Long tests are run in parallel, and with their own store.
t.Parallel()
opts = newOpts()
}
changed, err := opts.RunFiletest(path, content)
if err != nil {
t.Fatal(err.Error())
}
if changed != "" {
err = os.WriteFile(filepath.Join(dir, path), []byte(changed), de.Type())
if err != nil {
criticalError = fmt.Errorf("could not fix golden file: %w", err)
}
}
})
return criticalError
})
if err != nil {
t.Fatal(err)
}
}
// TestStdlibs tests all the standard library packages.
func TestStdlibs(t *testing.T) {
t.Parallel()
rootDir, err := filepath.Abs("../../../")
require.NoError(t, err)
newOpts := func() (capture *bytes.Buffer, opts *test.TestOptions) {
var out io.Writer
if testing.Verbose() {
out = os.Stdout
} else {
capture = new(bytes.Buffer)
out = capture
}
opts = test.NewTestOptions(rootDir, out, out)
opts.Verbose = true
return
}
sharedCapture, sharedOpts := newOpts()
dir := "../../stdlibs/"
fsys := os.DirFS(dir)
err = fs.WalkDir(fsys, ".", func(path string, de fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case !de.IsDir() || path == ".":
return nil
}
fp := filepath.Join(dir, path)
memPkg := gnolang.MustReadMemPackage(fp, path)
t.Run(strings.ReplaceAll(memPkg.Path, "/", "-"), func(t *testing.T) {
capture, opts := sharedCapture, sharedOpts
switch memPkg.Path {
// Excluded in short
case
"bufio",
"bytes",
"strconv":
if testing.Short() {
t.Skip("Skipped because of -short, and this stdlib is very long currently.")
}
fallthrough
// Run using separate store, as it's faster
case
"math/rand",
"regexp",
"regexp/syntax",
"sort":
t.Parallel()
capture, opts = newOpts()
}
if capture != nil {
capture.Reset()
}
err := test.Test(memPkg, "", opts)
if !testing.Verbose() {
t.Log(capture.String())
}
if err != nil {
t.Error(err)
}
})
return nil
})
if err != nil {
t.Fatal(err)
}
testDir := "../../tests/stdlibs/"
testFs := os.DirFS(testDir)
err = fs.WalkDir(testFs, ".", func(path string, de fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case !de.IsDir() || path == ".":
return nil
}
if _, err := os.Stat(filepath.Join(dir, path)); err == nil {
// skip; this dir exists already in the normal stdlibs and we
// currently don't support testing these "mixed stdlibs".
return nil
}
fp := filepath.Join(testDir, path)
memPkg := gnolang.MustReadMemPackage(fp, path)
t.Run("test-"+strings.ReplaceAll(memPkg.Path, "/", "-"), func(t *testing.T) {
if sharedCapture != nil {
sharedCapture.Reset()
}
err := test.Test(memPkg, "", sharedOpts)
if !testing.Verbose() {
t.Log(sharedCapture.String())
}
if err != nil {
t.Error(err)
}
})
return nil
})
if err != nil {
t.Fatal(err)
}
}