github.com/switchupcb/yaegi@v0.10.2/interp/src_test.go (about)

     1  package interp
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func Test_effectivePkg(t *testing.T) {
    11  	testCases := []struct {
    12  		desc     string
    13  		root     string
    14  		path     string
    15  		expected string
    16  	}{
    17  		{
    18  			desc:     "path is a subpackage",
    19  			root:     "github.com/foo/plugin/vendor/guthib.com/traefik/fromage",
    20  			path:     "guthib.com/traefik/fromage/couteau/lol",
    21  			expected: "github.com/foo/plugin/vendor/guthib.com/traefik/fromage/couteau/lol",
    22  		},
    23  		{
    24  			desc:     "path is a vendored package",
    25  			root:     "github.com/foo/plugin/vendor/guthib.com/traefik/fromage",
    26  			path:     "vendor/guthib.com/traefik/vin",
    27  			expected: "github.com/foo/plugin/vendor/guthib.com/traefik/fromage/vendor/guthib.com/traefik/vin",
    28  		},
    29  		{
    30  			desc:     "path is non-existent",
    31  			root:     "foo",
    32  			path:     "githib.com/foo/app",
    33  			expected: "foo/githib.com/foo/app",
    34  		},
    35  	}
    36  
    37  	for _, test := range testCases {
    38  		test := test
    39  		t.Run(test.desc, func(t *testing.T) {
    40  			t.Parallel()
    41  
    42  			pkg := effectivePkg(test.root, test.path)
    43  
    44  			if pkg != test.expected {
    45  				t.Errorf("Got %s, want %s", pkg, test.expected)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func Test_pkgDir(t *testing.T) {
    52  	// create GOPATH
    53  	goPath, err := ioutil.TempDir("", "pkdir")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	defer func() {
    58  		_ = os.RemoveAll(goPath)
    59  	}()
    60  
    61  	// Create project
    62  	project := filepath.Join(goPath, "src", "guthib.com", "foo", "root")
    63  	if err := os.MkdirAll(project, 0o700); err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	type expected struct {
    68  		dir   string
    69  		rpath string
    70  	}
    71  
    72  	testCases := []struct {
    73  		desc     string
    74  		path     string
    75  		root     string
    76  		setup    func() error
    77  		expected expected
    78  	}{
    79  		{
    80  			desc: "GOPATH only",
    81  			path: "guthib.com/foo/bar",
    82  			root: "",
    83  			setup: func() error {
    84  				return os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700)
    85  			},
    86  			expected: expected{
    87  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
    88  				rpath: "",
    89  			},
    90  		},
    91  		{
    92  			desc: "vendor",
    93  			path: "guthib.com/foo/bar",
    94  			root: filepath.Join("guthib.com", "foo", "root"),
    95  			setup: func() error {
    96  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700)
    97  			},
    98  			expected: expected{
    99  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bar"),
   100  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   101  			},
   102  		},
   103  		{
   104  			desc: "GOPATH flat",
   105  			path: "guthib.com/foo/bar",
   106  			root: filepath.Join("guthib.com", "foo", "root"),
   107  			setup: func() error {
   108  				return os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700)
   109  			},
   110  			expected: expected{
   111  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
   112  				rpath: "",
   113  			},
   114  		},
   115  		{
   116  			desc: "vendor flat",
   117  			path: "guthib.com/foo/bar",
   118  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir"),
   119  			setup: func() error {
   120  				if err := os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700); err != nil {
   121  					return err
   122  				}
   123  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bir"), 0o700)
   124  			},
   125  			expected: expected{
   126  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bar"),
   127  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   128  			},
   129  		},
   130  		{
   131  			desc: "fallback to GOPATH",
   132  			path: "guthib.com/foo/bar",
   133  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir"),
   134  			setup: func() error {
   135  				if err := os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700); err != nil {
   136  					return err
   137  				}
   138  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bir"), 0o700)
   139  			},
   140  			expected: expected{
   141  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
   142  				rpath: "",
   143  			},
   144  		},
   145  		{
   146  			desc: "vendor recursive",
   147  			path: "guthib.com/foo/bar",
   148  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir", "vendor", "guthib.com", "foo", "bur"),
   149  			setup: func() error {
   150  				if err := os.MkdirAll(
   151  					filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir", "vendor", "guthib.com", "foo", "bur"),
   152  					0o700); err != nil {
   153  					return err
   154  				}
   155  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700)
   156  			},
   157  			expected: expected{
   158  				dir:   filepath.Join(project, "vendor", "guthib.com", "foo", "bar"),
   159  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   160  			},
   161  		},
   162  	}
   163  
   164  	interp := &Interpreter{
   165  		opt: opt{
   166  			filesystem: &realFS{},
   167  		},
   168  	}
   169  
   170  	for _, test := range testCases {
   171  		test := test
   172  		t.Run(test.desc, func(t *testing.T) {
   173  			if err := os.RemoveAll(goPath); err != nil {
   174  				t.Fatal(err)
   175  			}
   176  			if err := os.MkdirAll(goPath, 0o700); err != nil {
   177  				t.Fatal(err)
   178  			}
   179  
   180  			if test.setup != nil {
   181  				err := test.setup()
   182  				if err != nil {
   183  					t.Fatal(err)
   184  				}
   185  			}
   186  
   187  			dir, err := interp.getPackageDir(test.path)
   188  			if err != nil {
   189  				t.Fatal(err)
   190  			}
   191  
   192  			if dir != test.expected.dir {
   193  				t.Errorf("[dir] got: %s, want: %s", dir, test.expected.dir)
   194  			}
   195  
   196  			if test.root != test.expected.rpath {
   197  				t.Errorf(" [rpath] got: %s, want: %s", test.root, test.expected.rpath)
   198  			}
   199  		})
   200  	}
   201  }