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
|
package pkg
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
func TestPackages(t *testing.T) {
testCases := []struct {
desc string
goPath string
expected string
topImport string
evalFile string
}{
{
desc: "vendor",
goPath: "./_pkg/",
expected: "root Fromage",
},
{
desc: "sub-subpackage",
goPath: "./_pkg0/",
expected: "root Fromage Cheese",
},
{
desc: "subpackage",
goPath: "./_pkg1/",
expected: "root Fromage!",
},
{
desc: "multiple vendor folders",
goPath: "./_pkg2/",
expected: "root Fromage Cheese!",
},
{
desc: "multiple vendor folders and subpackage in vendor",
goPath: "./_pkg3/",
expected: "root Fromage Couteau Cheese!",
},
{
desc: "multiple vendor folders and multiple subpackages in vendor",
goPath: "./_pkg4/",
expected: "root Fromage Cheese Vin! Couteau",
},
{
desc: "vendor flat",
goPath: "./_pkg5/",
expected: "root Fromage Cheese Vin! Couteau",
},
{
desc: "fallback to GOPATH",
goPath: "./_pkg6/",
expected: "root Fromage Cheese Vin! Couteau",
},
{
desc: "recursive vendor",
goPath: "./_pkg7/",
expected: "root vin cheese fromage",
},
{
desc: "named subpackage",
goPath: "./_pkg8/",
expected: "root Fromage!",
},
{
desc: "at the project root",
goPath: "./_pkg10/",
expected: "root Fromage",
topImport: "github.com/foo",
},
{
desc: "eval main that has vendored dep",
goPath: "./_pkg11/",
expected: "Fromage",
evalFile: "./_pkg11/src/foo/foo.go",
},
{
desc: "vendor dir is a sibling or an uncle",
goPath: "./_pkg12/",
expected: "Yo hello",
topImport: "guthib.com/foo/pkg",
},
{
desc: "eval main with vendor as a sibling",
goPath: "./_pkg12/",
expected: "Yo hello",
evalFile: "./_pkg12/src/guthib.com/foo/main.go",
},
{
desc: "eval main with vendor",
goPath: "./_pkg13/",
expected: "foobar",
evalFile: "./_pkg13/src/guthib.com/foo/bar/main.go",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
goPath, err := filepath.Abs(filepath.FromSlash(test.goPath))
if err != nil {
t.Fatal(err)
}
var stdout, stderr bytes.Buffer
i := interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr})
// Use binary standard library
if err := i.Use(stdlib.Symbols); err != nil {
t.Fatal(err)
}
var msg string
if test.evalFile != "" {
if _, err := i.EvalPath(filepath.FromSlash(test.evalFile)); err != nil {
fatalStderrf(t, "%v", err)
}
msg = stdout.String()
} else {
// Load pkg from sources
topImport := "github.com/foo/pkg"
if test.topImport != "" {
topImport = test.topImport
}
if _, err = i.Eval(fmt.Sprintf(`import %q`, topImport)); err != nil {
t.Fatal(err)
}
value, err := i.Eval(`pkg.NewSample()`)
if err != nil {
t.Fatal(err)
}
fn := value.Interface().(func() string)
msg = fn()
}
if msg != test.expected {
fatalStderrf(t, "Got %q, want %q", msg, test.expected)
}
})
}
}
func fatalStderrf(t *testing.T, format string, args ...interface{}) {
t.Helper()
fmt.Fprintf(os.Stderr, format+"\n", args...)
t.FailNow()
}
func TestPackagesError(t *testing.T) {
testCases := []struct {
desc string
goPath string
expected string
}{
{
desc: "different packages in the same directory",
goPath: "./_pkg9/",
expected: `1:21: import "github.com/foo/pkg" error: found packages pkg and pkgfalse in ` + filepath.FromSlash("_pkg9/src/github.com/foo/pkg"),
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
// Init go interpreter
i := interp.New(interp.Options{GoPath: test.goPath})
// Use binary standard library
if err := i.Use(stdlib.Symbols); err != nil {
t.Fatal(err)
}
// Load pkg from sources
_, err := i.Eval(`import "github.com/foo/pkg"`)
if err == nil {
t.Fatalf("got no error, want %q", test.expected)
}
if err.Error() != test.expected {
t.Errorf("got %q, want %q", err.Error(), test.expected)
}
})
}
}
|