github.com/traefik/yaegi@v0.15.1/interp/src_test.go (about)

     1  package interp
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func Test_effectivePkg(t *testing.T) {
    10  	testCases := []struct {
    11  		desc     string
    12  		root     string
    13  		path     string
    14  		expected string
    15  	}{
    16  		{
    17  			desc:     "path is a subpackage",
    18  			root:     "github.com/foo/plugin/vendor/guthib.com/traefik/fromage",
    19  			path:     "guthib.com/traefik/fromage/couteau/lol",
    20  			expected: "github.com/foo/plugin/vendor/guthib.com/traefik/fromage/couteau/lol",
    21  		},
    22  		{
    23  			desc:     "path is a vendored package",
    24  			root:     "github.com/foo/plugin/vendor/guthib.com/traefik/fromage",
    25  			path:     "vendor/guthib.com/traefik/vin",
    26  			expected: "github.com/foo/plugin/vendor/guthib.com/traefik/fromage/vendor/guthib.com/traefik/vin",
    27  		},
    28  		{
    29  			desc:     "path is non-existent",
    30  			root:     "foo",
    31  			path:     "githib.com/foo/app",
    32  			expected: "foo/githib.com/foo/app",
    33  		},
    34  	}
    35  
    36  	for _, test := range testCases {
    37  		test := test
    38  		t.Run(test.desc, func(t *testing.T) {
    39  			t.Parallel()
    40  
    41  			pkg := effectivePkg(test.root, test.path)
    42  
    43  			if pkg != test.expected {
    44  				t.Errorf("Got %s, want %s", pkg, test.expected)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func Test_pkgDir(t *testing.T) {
    51  	// create GOPATH
    52  	goPath := t.TempDir()
    53  
    54  	// Create project
    55  	project := filepath.Join(goPath, "src", "guthib.com", "foo", "root")
    56  	if err := os.MkdirAll(project, 0o700); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	type expected struct {
    61  		dir   string
    62  		rpath string
    63  	}
    64  
    65  	testCases := []struct {
    66  		desc     string
    67  		path     string
    68  		root     string
    69  		setup    func() error
    70  		expected expected
    71  	}{
    72  		{
    73  			desc: "GOPATH only",
    74  			path: "guthib.com/foo/bar",
    75  			root: "",
    76  			setup: func() error {
    77  				return os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700)
    78  			},
    79  			expected: expected{
    80  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
    81  				rpath: "",
    82  			},
    83  		},
    84  		{
    85  			desc: "vendor",
    86  			path: "guthib.com/foo/bar",
    87  			root: filepath.Join("guthib.com", "foo", "root"),
    88  			setup: func() error {
    89  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700)
    90  			},
    91  			expected: expected{
    92  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bar"),
    93  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
    94  			},
    95  		},
    96  		{
    97  			desc: "GOPATH flat",
    98  			path: "guthib.com/foo/bar",
    99  			root: filepath.Join("guthib.com", "foo", "root"),
   100  			setup: func() error {
   101  				return os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700)
   102  			},
   103  			expected: expected{
   104  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
   105  				rpath: "",
   106  			},
   107  		},
   108  		{
   109  			desc: "vendor flat",
   110  			path: "guthib.com/foo/bar",
   111  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir"),
   112  			setup: func() error {
   113  				if err := os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700); err != nil {
   114  					return err
   115  				}
   116  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bir"), 0o700)
   117  			},
   118  			expected: expected{
   119  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bar"),
   120  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   121  			},
   122  		},
   123  		{
   124  			desc: "fallback to GOPATH",
   125  			path: "guthib.com/foo/bar",
   126  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir"),
   127  			setup: func() error {
   128  				if err := os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700); err != nil {
   129  					return err
   130  				}
   131  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bir"), 0o700)
   132  			},
   133  			expected: expected{
   134  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
   135  				rpath: "",
   136  			},
   137  		},
   138  		{
   139  			desc: "vendor recursive",
   140  			path: "guthib.com/foo/bar",
   141  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir", "vendor", "guthib.com", "foo", "bur"),
   142  			setup: func() error {
   143  				if err := os.MkdirAll(
   144  					filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir", "vendor", "guthib.com", "foo", "bur"),
   145  					0o700); err != nil {
   146  					return err
   147  				}
   148  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700)
   149  			},
   150  			expected: expected{
   151  				dir:   filepath.Join(project, "vendor", "guthib.com", "foo", "bar"),
   152  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   153  			},
   154  		},
   155  	}
   156  
   157  	interp := &Interpreter{
   158  		opt: opt{
   159  			filesystem: &realFS{},
   160  		},
   161  	}
   162  
   163  	for _, test := range testCases {
   164  		test := test
   165  		t.Run(test.desc, func(t *testing.T) {
   166  			if err := os.RemoveAll(goPath); err != nil {
   167  				t.Fatal(err)
   168  			}
   169  			if err := os.MkdirAll(goPath, 0o700); err != nil {
   170  				t.Fatal(err)
   171  			}
   172  
   173  			if test.setup != nil {
   174  				err := test.setup()
   175  				if err != nil {
   176  					t.Fatal(err)
   177  				}
   178  			}
   179  
   180  			dir, rPath, err := interp.pkgDir(goPath, test.root, test.path)
   181  			if err != nil {
   182  				t.Fatal(err)
   183  			}
   184  
   185  			if dir != test.expected.dir {
   186  				t.Errorf("[dir] got: %s, want: %s", dir, test.expected.dir)
   187  			}
   188  
   189  			if rPath != test.expected.rpath {
   190  				t.Errorf(" [rpath] got: %s, want: %s", rPath, test.expected.rpath)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func Test_previousRoot(t *testing.T) {
   197  	testCases := []struct {
   198  		desc           string
   199  		root           string
   200  		rootPathSuffix string
   201  		expected       string
   202  	}{
   203  		{
   204  			desc:     "GOPATH",
   205  			root:     "github.com/foo/pkg/",
   206  			expected: "",
   207  		},
   208  		{
   209  			desc:     "vendor level 1",
   210  			root:     "github.com/foo/pkg/vendor/guthib.com/traefik/fromage",
   211  			expected: "github.com/foo/pkg",
   212  		},
   213  		{
   214  			desc:     "vendor level 2",
   215  			root:     "github.com/foo/pkg/vendor/guthib.com/traefik/fromage/vendor/guthib.com/traefik/fuu",
   216  			expected: "github.com/foo/pkg/vendor/guthib.com/traefik/fromage",
   217  		},
   218  		{
   219  			desc:           "vendor is sibling",
   220  			root:           "github.com/foo/bar",
   221  			rootPathSuffix: "testdata/src/github.com/foo/bar",
   222  			expected:       "github.com/foo",
   223  		},
   224  		{
   225  			desc:           "vendor is uncle",
   226  			root:           "github.com/foo/bar/baz",
   227  			rootPathSuffix: "testdata/src/github.com/foo/bar/baz",
   228  			expected:       "github.com/foo",
   229  		},
   230  	}
   231  
   232  	for _, test := range testCases {
   233  		test := test
   234  		t.Run(test.desc, func(t *testing.T) {
   235  			t.Parallel()
   236  
   237  			var rootPath string
   238  			if test.rootPathSuffix != "" {
   239  				wd, err := os.Getwd()
   240  				if err != nil {
   241  					t.Fatal(err)
   242  				}
   243  				rootPath = filepath.Join(wd, test.rootPathSuffix)
   244  			} else {
   245  				rootPath = vendor
   246  			}
   247  			p, err := previousRoot(&realFS{}, rootPath, test.root)
   248  			if err != nil {
   249  				t.Error(err)
   250  			}
   251  
   252  			if p != test.expected {
   253  				t.Errorf("got: %s, want: %s", p, test.expected)
   254  			}
   255  		})
   256  	}
   257  }