| 1 |
package interp_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"go/build" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"runtime" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/traefik/yaegi/interp" |
| 15 |
"github.com/traefik/yaegi/stdlib" |
| 16 |
"github.com/traefik/yaegi/stdlib/unsafe" |
| 17 |
) |
| 18 |
|
| 19 |
var testsToSkipGo122 = map[string]bool{} |
| 20 |
|
| 21 |
var go122 = strings.HasPrefix(runtime.Version(), "go1.22") |
| 22 |
|
| 23 |
var go123 = strings.HasPrefix(runtime.Version(), "go1.23") |
| 24 |
|
| 25 |
func TestInterpConsistencyBuild(t *testing.T) { |
| •26 |
if testing.Short() { |
| 27 |
t.Skip("short mode") |
| 28 |
} |
| 29 |
dir := filepath.Join("..", "_test", "tmp") |
| 30 |
if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 31 |
if err := os.Mkdir(dir, 0o700); err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
baseDir := filepath.Join("..", "_test") |
| 37 |
files, err := os.ReadDir(baseDir) |
| 38 |
if err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} |
| 41 |
|
| 42 |
for _, file := range files { |
| 43 |
if filepath.Ext(file.Name()) != ".go" || |
| 44 |
file.Name() == "assign11.go" || // expect error |
| 45 |
file.Name() == "assign12.go" || // expect error |
| 46 |
file.Name() == "assign15.go" || // expect error |
| 47 |
file.Name() == "assign19.go" || // expect error |
| 48 |
file.Name() == "bad0.go" || // expect error |
| 49 |
file.Name() == "break0.go" || // expect error |
| 50 |
file.Name() == "cont3.go" || // expect error |
| 51 |
file.Name() == "const9.go" || // expect error |
| 52 |
file.Name() == "export1.go" || // non-main package |
| 53 |
file.Name() == "export0.go" || // non-main package |
| 54 |
file.Name() == "for7.go" || // expect error |
| 55 |
file.Name() == "fun21.go" || // expect error |
| 56 |
file.Name() == "fun22.go" || // expect error |
| 57 |
file.Name() == "fun23.go" || // expect error |
| 58 |
file.Name() == "fun24.go" || // expect error |
| 59 |
file.Name() == "fun25.go" || // expect error |
| 60 |
file.Name() == "gen7.go" || // expect error |
| 61 |
file.Name() == "gen8.go" || // expect error |
| 62 |
file.Name() == "gen9.go" || // expect error |
| 63 |
file.Name() == "if2.go" || // expect error |
| 64 |
file.Name() == "import6.go" || // expect error |
| 65 |
file.Name() == "init1.go" || // expect error |
| 66 |
file.Name() == "io0.go" || // use random number |
| 67 |
file.Name() == "issue-1093.go" || // expect error |
| 68 |
file.Name() == "issue-1276.go" || // expect error |
| 69 |
file.Name() == "issue-1330.go" || // expect error |
| 70 |
file.Name() == "op1.go" || // expect error |
| 71 |
file.Name() == "op7.go" || // expect error |
| 72 |
file.Name() == "op9.go" || // expect error |
| 73 |
file.Name() == "bltn0.go" || // expect error |
| 74 |
file.Name() == "method16.go" || // private struct field |
| 75 |
file.Name() == "method39.go" || // expect error |
| 76 |
file.Name() == "switch8.go" || // expect error |
| 77 |
file.Name() == "switch9.go" || // expect error |
| 78 |
file.Name() == "switch13.go" || // expect error |
| 79 |
file.Name() == "switch19.go" || // expect error |
| 80 |
file.Name() == "time0.go" || // display time (similar to random number) |
| 81 |
file.Name() == "factor.go" || // bench |
| 82 |
file.Name() == "fib.go" || // bench |
| 83 |
file.Name() == "issue-1618.go" || // bench (infinite running) |
| 84 |
|
| 85 |
file.Name() == "type5.go" || // used to illustrate a limitation with no workaround, related to the fact that the reflect package does not allow the creation of named types |
| 86 |
file.Name() == "type6.go" || // used to illustrate a limitation with no workaround, related to the fact that the reflect package does not allow the creation of named types |
| 87 |
|
| 88 |
file.Name() == "redeclaration0.go" || // expect error |
| 89 |
file.Name() == "redeclaration1.go" || // expect error |
| 90 |
file.Name() == "redeclaration2.go" || // expect error |
| 91 |
file.Name() == "redeclaration3.go" || // expect error |
| 92 |
file.Name() == "redeclaration4.go" || // expect error |
| 93 |
file.Name() == "redeclaration5.go" || // expect error |
| 94 |
file.Name() == "redeclaration-global0.go" || // expect error |
| 95 |
file.Name() == "redeclaration-global1.go" || // expect error |
| 96 |
file.Name() == "redeclaration-global2.go" || // expect error |
| 97 |
file.Name() == "redeclaration-global3.go" || // expect error |
| 98 |
file.Name() == "redeclaration-global4.go" || // expect error |
| 99 |
file.Name() == "redeclaration-global5.go" || // expect error |
| 100 |
file.Name() == "redeclaration-global6.go" || // expect error |
| 101 |
file.Name() == "redeclaration-global7.go" || // expect error |
| 102 |
file.Name() == "panic0.go" || // expect error |
| 103 |
file.Name() == "pkgname0.go" || // has deps |
| 104 |
file.Name() == "pkgname1.go" || // expect error |
| 105 |
file.Name() == "pkgname2.go" || // has deps |
| 106 |
file.Name() == "ipp_as_key.go" || // has deps |
| 107 |
file.Name() == "restricted0.go" || // expect error |
| 108 |
file.Name() == "restricted1.go" || // expect error |
| 109 |
file.Name() == "restricted2.go" || // expect error |
| 110 |
file.Name() == "restricted3.go" || // expect error |
| 111 |
file.Name() == "server6.go" || // syntax parsing |
| 112 |
file.Name() == "server5.go" || // syntax parsing |
| 113 |
file.Name() == "server4.go" || // syntax parsing |
| 114 |
file.Name() == "server3.go" || // syntax parsing |
| 115 |
file.Name() == "server2.go" || // syntax parsing |
| 116 |
file.Name() == "server1a.go" || // syntax parsing |
| 117 |
file.Name() == "server1.go" || // syntax parsing |
| 118 |
file.Name() == "server0.go" || // syntax parsing |
| 119 |
file.Name() == "server.go" || // syntax parsing |
| 120 |
file.Name() == "range9.go" || // expect error |
| 121 |
file.Name() == "unsafe6.go" || // needs go.mod to be 1.17 |
| 122 |
file.Name() == "unsafe7.go" || // needs go.mod to be 1.17 |
| 123 |
file.Name() == "type24.go" || // expect error |
| 124 |
file.Name() == "type27.go" || // expect error |
| 125 |
file.Name() == "type28.go" || // expect error |
| 126 |
file.Name() == "type29.go" || // expect error |
| 127 |
file.Name() == "type30.go" || // expect error |
| 128 |
file.Name() == "type31.go" || // expect error |
| 129 |
file.Name() == "type32.go" || // expect error |
| 130 |
file.Name() == "type33.go" { // expect error |
| 131 |
continue |
| 132 |
} |
| 133 |
// Skip some tests which are problematic in go1.21 only. |
| 134 |
if go121 && testsToSkipGo121[file.Name()] { |
| 135 |
continue |
| 136 |
} |
| 137 |
if (go122 || go123) && testsToSkipGo122[file.Name()] { |
| 138 |
continue |
| •139 |
} |
| 140 |
|
| 141 |
file := file |
| 142 |
t.Run(file.Name(), func(t *testing.T) { |
| 143 |
filePath := filepath.Join(baseDir, file.Name()) |
| 144 |
|
| 145 |
// catch stdout |
| 146 |
backupStdout := os.Stdout |
| 147 |
defer func() { |
| 148 |
os.Stdout = backupStdout |
| 149 |
}() |
| 150 |
r, w, _ := os.Pipe() |
| 151 |
os.Stdout = w |
| 152 |
|
| 153 |
i := interp.New(interp.Options{GoPath: build.Default.GOPATH}) |
| 154 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 155 |
t.Fatal(err) |
| 156 |
} |
| 157 |
if err := i.Use(interp.Symbols); err != nil { |
| 158 |
t.Fatal(err) |
| 159 |
} |
| 160 |
if err := i.Use(unsafe.Symbols); err != nil { |
| 161 |
t.Fatal(err) |
| 162 |
} |
| 163 |
|
| 164 |
_, err = i.EvalPath(filePath) |
| 165 |
if err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
|
| 169 |
// read stdout |
| 170 |
if err = w.Close(); err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
outInterp, err := io.ReadAll(r) |
| 174 |
if err != nil { |
| 175 |
t.Fatal(err) |
| 176 |
} |
| 177 |
|
| 178 |
// restore Stdout |
| 179 |
os.Stdout = backupStdout |
| 180 |
|
| 181 |
bin := filepath.Join(dir, strings.TrimSuffix(file.Name(), ".go")) |
| 182 |
if runtime.GOOS == "windows" { |
| 183 |
bin += ".exe" |
| 184 |
} |
| 185 |
|
| 186 |
cmdBuild := exec.Command("go", "build", "-tags=dummy", "-o", bin, filePath) |
| 187 |
outBuild, err := cmdBuild.CombinedOutput() |
| 188 |
if err != nil { |
| 189 |
t.Log(string(outBuild)) |
| 190 |
t.Fatal(err) |
| 191 |
} |
| 192 |
|
| 193 |
cmd := exec.Command(bin) |
| 194 |
outRun, err := cmd.CombinedOutput() |
| 195 |
if err != nil { |
| 196 |
t.Log(string(outRun)) |
| 197 |
t.Fatal(err) |
| 198 |
} |
| 199 |
|
| 200 |
if !bytes.Equal(outInterp, outRun) { |
| 201 |
t.Errorf("\nGot: %q,\n want: %q", string(outInterp), string(outRun)) |
| 202 |
} |
| 203 |
}) |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func TestInterpErrorConsistency(t *testing.T) { |
| 208 |
testCases := []struct { |
| 209 |
fileName string |
| 210 |
expectedInterp string |
| 211 |
expectedStderr string |
| 212 |
expectedExec string |
| 213 |
}{ |
| 214 |
{ |
| 215 |
fileName: "assign11.go", |
| 216 |
expectedInterp: "6:2: assignment mismatch: 3 variables but fmt.Println returns 2 values", |
| 217 |
expectedExec: "6:12: assignment mismatch: 3 variables but fmt.Println returns 2 values", |
| 218 |
}, |
| 219 |
{ |
| 220 |
fileName: "assign12.go", |
| 221 |
expectedInterp: "6:2: assignment mismatch: 3 variables but fmt.Println returns 2 values", |
| 222 |
expectedExec: "6:13: assignment mismatch: 3 variables but fmt.Println returns 2 values", |
| 223 |
}, |
| 224 |
{ |
| 225 |
fileName: "bad0.go", |
| 226 |
expectedInterp: "1:1: expected 'package', found println", |
| 227 |
expectedExec: "1:1: expected 'package', found println", |
| 228 |
}, |
| 229 |
{ |
| 230 |
fileName: "break0.go", |
| 231 |
expectedInterp: "15:5: invalid break label OuterLoop", |
| 232 |
expectedExec: "15:11: invalid break label OuterLoop", |
| 233 |
}, |
| 234 |
{ |
| 235 |
fileName: "cont3.go", |
| 236 |
expectedInterp: "15:5: invalid continue label OuterLoop", |
| 237 |
expectedExec: "15:14: invalid continue label OuterLoop", |
| 238 |
}, |
| 239 |
{ |
| 240 |
fileName: "const9.go", |
| 241 |
expectedInterp: "5:2: constant definition loop", |
| 242 |
expectedExec: "5:2: initialization", |
| 243 |
}, |
| 244 |
{ |
| 245 |
fileName: "if2.go", |
| 246 |
expectedInterp: "7:5: non-bool used as if condition", |
| 247 |
expectedExec: "7:5: non-boolean condition in if statement", |
| 248 |
}, |
| 249 |
{ |
| 250 |
fileName: "for7.go", |
| 251 |
expectedInterp: "4:14: non-bool used as for condition", |
| 252 |
expectedExec: "4:14: non-boolean condition in for statement", |
| 253 |
}, |
| 254 |
{ |
| 255 |
fileName: "fun21.go", |
| 256 |
expectedInterp: "4:2: not enough arguments to return", |
| 257 |
expectedExec: "4:2: not enough return values", |
| 258 |
}, |
| 259 |
{ |
| 260 |
fileName: "fun22.go", |
| 261 |
expectedInterp: "6:2: not enough arguments in call to time.Date", |
| 262 |
expectedExec: "6:2: not enough arguments in call to time.Date", |
| 263 |
}, |
| 264 |
{ |
| 265 |
fileName: "fun23.go", |
| 266 |
expectedInterp: "3:17: too many arguments to return", |
| 267 |
expectedExec: "3:24: too many return values", |
| 268 |
}, |
| 269 |
{ |
| 270 |
fileName: "issue-1093.go", |
| 271 |
expectedInterp: "9:6: cannot use type untyped string as type int in assignment", |
| 272 |
expectedExec: `9:6: cannot use "a" + b() (value of type string)`, |
| 273 |
}, |
| 274 |
{ |
| 275 |
fileName: "op1.go", |
| 276 |
expectedInterp: "5:2: invalid operation: mismatched types int and untyped float", |
| 277 |
expectedExec: "5:7: 1.3 (untyped float constant) truncated to int", |
| 278 |
}, |
| 279 |
{ |
| 280 |
fileName: "bltn0.go", |
| 281 |
expectedInterp: "4:7: use of builtin println not in function call", |
| 282 |
expectedExec: "4:7: println (built-in) must be called", |
| 283 |
}, |
| 284 |
{ |
| 285 |
fileName: "import6.go", |
| 286 |
expectedInterp: "import cycle not allowed", |
| 287 |
expectedExec: "import cycle not allowed", |
| 288 |
}, |
| 289 |
{ |
| 290 |
fileName: "switch8.go", |
| 291 |
expectedInterp: "5:2: fallthrough statement out of place", |
| 292 |
expectedExec: "5:2: fallthrough statement out of place", |
| 293 |
}, |
| 294 |
{ |
| 295 |
fileName: "switch9.go", |
| 296 |
expectedInterp: "9:3: cannot fallthrough in type switch", |
| 297 |
expectedExec: "fallthrough", |
| 298 |
}, |
| 299 |
{ |
| 300 |
fileName: "switch13.go", |
| 301 |
expectedInterp: "is not a type", |
| 302 |
expectedExec: "is not a type", |
| 303 |
}, |
| 304 |
{ |
| 305 |
fileName: "switch19.go", |
| 306 |
expectedInterp: "37:2: duplicate case Bir in type switch", |
| 307 |
expectedExec: "37:7: duplicate case Bir in type switch", |
| 308 |
}, |
| 309 |
{ |
| 310 |
fileName: "panic0.go", |
| 311 |
expectedInterp: "stop!", |
| 312 |
expectedStderr: ` |
| 313 |
../_test/panic0.go:16:2: panic: main.baz(...) |
| 314 |
../_test/panic0.go:12:2: panic: main.bar(...) |
| 315 |
../_test/panic0.go:8:2: panic: main.foo(...) |
| 316 |
../_test/panic0.go:4:2: panic: main.main(...) |
| 317 |
`, |
| 318 |
}, |
| 319 |
} |
| 320 |
|
| 321 |
for _, test := range testCases { |
| •322 |
t.Run(test.fileName, func(t *testing.T) { |
| 323 |
if go123 && test.fileName == "switch13.go" { |
| 324 |
t.Skip() |
| 325 |
} |
| 326 |
|
| 327 |
if test.expectedInterp == "" && test.expectedExec == "" { |
| 328 |
t.Fatal("at least expectedInterp must be defined") |
| 329 |
} |
| 330 |
|
| 331 |
filePath := filepath.Join("..", "_test", test.fileName) |
| 332 |
|
| 333 |
var stderr bytes.Buffer |
| 334 |
i := interp.New(interp.Options{GoPath: build.Default.GOPATH, Stderr: &stderr}) |
| 335 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 336 |
t.Fatal(err) |
| 337 |
} |
| 338 |
|
| 339 |
_, errEval := i.EvalPath(filePath) |
| 340 |
if errEval == nil { |
| 341 |
t.Fatal("An error is expected but got none.") |
| 342 |
} |
| 343 |
|
| 344 |
if !strings.Contains(errEval.Error(), test.expectedInterp) { |
| 345 |
t.Errorf("got %q, want: %q", errEval.Error(), test.expectedInterp) |
| 346 |
} |
| 347 |
if test.expectedStderr != "" { |
| 348 |
exp, got := strings.TrimSpace(test.expectedStderr), strings.TrimSpace(stderr.String()) |
| 349 |
if exp != got { |
| 350 |
t.Errorf("got %q, want: %q", got, exp) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
cmd := exec.Command("go", "run", filePath) |
| 355 |
outRun, errExec := cmd.CombinedOutput() |
| 356 |
if errExec == nil { |
| 357 |
t.Log(string(outRun)) |
| 358 |
t.Fatal("An error is expected but got none.") |
| 359 |
} |
| 360 |
|
| 361 |
if test.expectedExec == "" && !strings.Contains(string(outRun), test.expectedInterp) { |
| 362 |
t.Errorf("got %q, want: %q", string(outRun), test.expectedInterp) |
| 363 |
} else if !strings.Contains(string(outRun), test.expectedExec) { |
| 364 |
t.Errorf("got %q, want: %q", string(outRun), test.expectedExec) |
| 365 |
} |
| 366 |
}) |
| 367 |
} |
| 368 |
} |