| 1 | package interp_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "go/build" |
| 6 | "go/parser" |
| 7 | "go/token" |
| 8 | "os" |
| 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 | // The following tests sometimes (not always) crash with go1.21 but not with go1.20 or go1.22. |
| 20 | // The reason of failure is not obvious, maybe due to the runtime itself, and will be investigated separately. |
| 21 | // Also, the closure tests depend on an incompatible language change in go1.22, where `for` variables are now |
| 22 | // defined in body (thus reallocated at each loop). This is now the behavior in yaegi, so 1.21 produces |
| 23 | // different results. |
| 24 | var testsToSkipGo121 = map[string]bool{"cli6.go": true, "cli7.go": true, "issue-1276.go": true, "issue-1330.go": true, "struct11.go": true, "closure9.go": true, "closure10.go": true, "closure11.go": true, "closure12.go": true, "closure15.go": true, "closure16.go": true, "closure17.go": true, "closure18.go": true, "closure20.go": true, "for17.go": true, "for18.go": true, "for19.go": true} |
| 25 | |
| 26 | var go121 = strings.HasPrefix(runtime.Version(), "go1.21") |
| 27 | |
| 28 | func TestFile(t *testing.T) { |
| 29 | filePath := "../_test/str.go" |
| 30 | runCheck(t, filePath) |
| 31 | |
| 32 | t.Setenv("YAEGI_SPECIAL_STDIO", "1") |
| 33 | |
| 34 | baseDir := filepath.Join("..", "_test") |
| 35 | files, err := os.ReadDir(baseDir) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | |
| 40 | for _, file := range files { |
| 41 | if filepath.Ext(file.Name()) != ".go" { |
| 42 | continue |
| 43 | } |
| 44 | // Skip some tests which are problematic in go1.21 only. |
| 45 | if go121 && testsToSkipGo121[file.Name()] { |
| 46 | continue |
| 47 | } |
| 48 | file := file |
| 49 | t.Run(file.Name(), func(t *testing.T) { |
| 50 | runCheck(t, filepath.Join(baseDir, file.Name())) |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func runCheck(t *testing.T, p string) { |
| 56 | t.Helper() |
| 57 | |
| 58 | wanted, goPath, errWanted := wantedFromComment(p) |
| 59 | if wanted == "" { |
| 60 | t.Skip(p, "has no comment 'Output:' or 'Error:'") |
| 61 | } |
| 62 | wanted = strings.TrimSpace(wanted) |
| 63 | |
| 64 | if goPath == "" { |
| 65 | goPath = build.Default.GOPATH |
| 66 | } |
| 67 | var stdout, stderr bytes.Buffer |
| 68 | i := interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr}) |
| 69 | if err := i.Use(interp.Symbols); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err := i.Use(stdlib.Symbols); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := i.Use(unsafe.Symbols); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | |
| 79 | _, err := i.EvalPath(p) |
| 80 | if errWanted { |
| 81 | if err == nil { |
| 82 | t.Fatalf("got nil error, want: %q", wanted) |
| 83 | } |
| 84 | if res := strings.TrimSpace(err.Error()); !strings.Contains(res, wanted) { |
| 85 | t.Errorf("got %q, want: %q", res, wanted) |
| 86 | } |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | |
| 94 | res := strings.TrimSpace(stdout.String()) |
| 95 | // Remove path in output, to have results independent of location. |
| 96 | res = strings.ReplaceAll(res, filepath.ToSlash(p)+":", "") |
| 97 | if res != wanted { |
| 98 | t.Errorf("\ngot: %q,\nwant: %q", res, wanted) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func wantedFromComment(p string) (res string, goPath string, err bool) { |
| 103 | fset := token.NewFileSet() |
| 104 | f, _ := parser.ParseFile(fset, p, nil, parser.ParseComments) |
| 105 | if len(f.Comments) == 0 { |
| 106 | return |
| 107 | } |
| 108 | text := f.Comments[len(f.Comments)-1].Text() |
| 109 | if strings.HasPrefix(text, "GOPATH:") { |
| 110 | parts := strings.SplitN(text, "\n", 2) |
| 111 | text = parts[1] |
| 112 | wd, err := os.Getwd() |
| 113 | if err != nil { |
| 114 | panic(err) |
| 115 | } |
| 116 | goPath = filepath.Join(wd, "..", "_test", strings.TrimPrefix(parts[0], "GOPATH:")) |
| 117 | } |
| 118 | if strings.HasPrefix(text, "Output:\n") { |
| 119 | return strings.TrimPrefix(text, "Output:\n"), goPath, false |
| 120 | } |
| 121 | if strings.HasPrefix(text, "Error:\n") { |
| 122 | return strings.TrimPrefix(text, "Error:\n"), goPath, true |
| 123 | } |
| 124 | return |
| 125 | } |