4
4
"go/token"
5
5
"io"
6
6
"os"
7
+ "os/exec"
7
8
"path/filepath"
8
9
"testing"
9
10
@@ -14,13 +15,6 @@ import (
14
15
)
15
16
16
17
func TestAnalyzer (t * testing.T ) {
17
- // NOTE: analysistest does not yet support modules;
18
- // see https://github.com/golang/go/issues/37054 for details.
19
- // To be able to run tests with external dependencies,
20
- // we first need to write a GOPATH-like tree of stubs.
21
- prepareTestFiles (t )
22
- testPackages = []string {"tests" , "builtins" }
23
-
24
18
testdata := analysistest .TestData ()
25
19
26
20
t .Run ("tests" , func (t * testing.T ) {
@@ -31,11 +25,15 @@ func TestAnalyzer(t *testing.T) {
31
25
pass .Reportf (st .Pos , fn .shortName ())
32
26
}
33
27
28
+ setupTestData (t , testdata , "tests" )
29
+
34
30
analyzer := New ()
35
31
analysistest .Run (t , testdata , analyzer , "tests" )
36
32
})
37
33
38
34
t .Run ("builtins" , func (t * testing.T ) {
35
+ setupTestData (t , testdata , "builtins" )
36
+
39
37
analyzer := New (
40
38
Func {Name : "example.com/custom.Marshal" , Tag : "custom" , ArgPos : 0 },
41
39
Func {Name : "example.com/custom.Unmarshal" , Tag : "custom" , ArgPos : 1 },
@@ -44,6 +42,8 @@ func TestAnalyzer(t *testing.T) {
44
42
})
45
43
46
44
t .Run ("bad Func.ArgPos" , func (t * testing.T ) {
45
+ setupTestData (t , testdata , "tests" )
46
+
47
47
analyzer := New (
48
48
Func {Name : "encoding/json.Marshal" , Tag : "json" , ArgPos : 10 },
49
49
)
@@ -77,111 +77,19 @@ type nopT struct{}
77
77
78
78
func (nopT ) Errorf (string , ... any ) {}
79
79
80
- func prepareTestFiles (t * testing.T ) {
81
- testdata := analysistest .TestData ()
82
-
83
- t .Cleanup (func () {
84
- err := os .RemoveAll (filepath .Join (testdata , "src" ))
85
- assert .NoErr [F ](t , err )
86
- })
87
-
88
- hardlink := func (dir , file string ) {
89
- target := filepath .Join (testdata , "src" , dir , file )
90
-
91
- err := os .MkdirAll (filepath .Dir (target ), 0o777 )
92
- assert .NoErr [F ](t , err )
80
+ // NOTE: analysistest does not yet support modules;
81
+ // see https://github.com/golang/go/issues/37054 for details.
82
+ func setupTestData (t * testing.T , testDataDir , dir string ) {
83
+ t .Helper ()
93
84
94
- err = os .Link (filepath .Join (testdata , file ), target )
95
- assert .NoErr [F ](t , err )
85
+ err := os .Chdir (filepath .Join (testDataDir , "src" , dir ))
86
+ if err != nil {
87
+ t .Fatal (err )
96
88
}
97
89
98
- hardlink ("tests" , "tests.go" )
99
- hardlink ("builtins" , "builtins.go" )
100
-
101
- for file , data := range stubs {
102
- target := filepath .Join (testdata , "src" , file )
103
-
104
- err := os .MkdirAll (filepath .Dir (target ), 0o777 )
105
- assert .NoErr [F ](t , err )
106
-
107
- err = os .WriteFile (target , []byte (data ), 0o666 )
108
- assert .NoErr [F ](t , err )
90
+ output , err := exec .Command ("go" , "mod" , "vendor" ).CombinedOutput ()
91
+ if err != nil {
92
+ t .Log (string (output ))
93
+ t .Fatal (err )
109
94
}
110
95
}
111
-
112
- var stubs = map [string ]string {
113
- "gopkg.in/yaml.v3/yaml.go" : `package yaml
114
- import "io"
115
- func Marshal(_ any) ([]byte, error) { return nil, nil }
116
- func Unmarshal(_ []byte, _ any) error { return nil }
117
- type Encoder struct{}
118
- func NewEncoder(_ io.Writer) *Encoder { return nil }
119
- func (*Encoder) Encode(_ any) error { return nil }
120
- type Decoder struct{}
121
- func NewDecoder(_ io.Reader) *Decoder { return nil }
122
- func (*Decoder) Decode(_ any) error { return nil }` ,
123
-
124
- "github.com/BurntSushi/toml/toml.go" : `package toml
125
- import "io"
126
- import "io/fs"
127
- func Unmarshal(_ []byte, _ any) error { return nil }
128
- type MetaData struct{}
129
- func Decode(_ string, _ any) (MetaData, error) { return MetaData{}, nil }
130
- func DecodeFS(_ fs.FS, _ string, _ any) (MetaData, error) { return MetaData{}, nil }
131
- func DecodeFile(_ string, _ any) (MetaData, error) { return MetaData{}, nil }
132
- type Encoder struct{}
133
- func NewEncoder(_ io.Writer) *Encoder { return nil }
134
- func (*Encoder) Encode(_ any) error { return nil }
135
- type Decoder struct{}
136
- func NewDecoder(_ io.Reader) *Decoder { return nil }
137
- func (*Decoder) Decode(_ any) error { return nil }` ,
138
-
139
- "github.com/mitchellh/mapstructure/mapstructure.go" : `package mapstructure
140
- type Metadata struct{}
141
- func Decode(_, _ any) error { return nil }
142
- func DecodeMetadata(_, _ any, _ *Metadata) error { return nil }
143
- func WeakDecode(_, _ any) error { return nil }
144
- func WeakDecodeMetadata(_, _ any, _ *Metadata) error { return nil }` ,
145
-
146
- "github.com/jmoiron/sqlx/sqlx.go" : `package sqlx
147
- import "context"
148
- type Queryer interface{}
149
- type QueryerContext interface{}
150
- type rowsi interface{}
151
- func Get(Queryer, any, string, ...any) error { return nil }
152
- func GetContext(context.Context, QueryerContext, any, string, ...any) error { return nil }
153
- func Select(Queryer, any, string, ...any) error { return nil }
154
- func SelectContext(context.Context, QueryerContext, any, string, ...any) error { return nil }
155
- func StructScan(rowsi, any) error { return nil }
156
- type Conn struct{}
157
- func (*Conn) GetContext(context.Context, any, string, ...any) error { return nil }
158
- func (*Conn) SelectContext(context.Context, any, string, ...any) error { return nil }
159
- type DB struct{}
160
- func (*DB) Get(any, string, ...any) error { return nil }
161
- func (*DB) GetContext(context.Context, any, string, ...any) error { return nil }
162
- func (*DB) Select(any, string, ...any) error { return nil }
163
- func (*DB) SelectContext(context.Context, any, string, ...any) error { return nil }
164
- type NamedStmt struct{}
165
- func (n *NamedStmt) Get(any, any) error { return nil }
166
- func (n *NamedStmt) GetContext(context.Context, any, any) error { return nil }
167
- func (n *NamedStmt) Select(any, any) error { return nil }
168
- func (n *NamedStmt) SelectContext(context.Context, any, any) error { return nil }
169
- type Row struct{}
170
- func (*Row) StructScan(any) error { return nil }
171
- type Rows struct{}
172
- func (*Rows) StructScan(any) error { return nil }
173
- type Stmt struct{}
174
- func (*Stmt) Get(any, ...any) error { return nil }
175
- func (*Stmt) GetContext(context.Context, any, ...any) error { return nil }
176
- func (*Stmt) Select(any, ...any) error { return nil }
177
- func (*Stmt) SelectContext(context.Context, any, ...any) error { return nil }
178
- type Tx struct{}
179
- func (*Tx) Get(any, string, ...any) error { return nil }
180
- func (*Tx) GetContext(context.Context, any, string, ...any) error { return nil }
181
- func (*Tx) Select(any, string, ...any) error { return nil }
182
- func (*Tx) SelectContext(context.Context, any, string, ...any) error { return nil }` ,
183
-
184
- "example.com/custom/custom.go" : `package custom
185
- func Marshal(_ any) ([]byte, error) { return nil, nil }
186
- func Unmarshal(_ []byte, _ any) error { return nil }` ,
187
- }
0 commit comments