github.com/gmemcc/yaegi@v0.12.1-0.20221128122509-aa99124c5d16/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, err := os.MkdirTemp("", "pkdir")
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	defer func() {
    57  		_ = os.RemoveAll(goPath)
    58  	}()
    59  
    60  	// Create project
    61  	project := filepath.Join(goPath, "src", "guthib.com", "foo", "root")
    62  	if err := os.MkdirAll(project, 0o700); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	type expected struct {
    67  		dir   string
    68  		rpath string
    69  	}
    70  
    71  	testCases := []struct {
    72  		desc     string
    73  		path     string
    74  		root     string
    75  		setup    func() error
    76  		expected expected
    77  	}{
    78  		{
    79  			desc: "GOPATH only",
    80  			path: "guthib.com/foo/bar",
    81  			root: "",
    82  			setup: func() error {
    83  				return os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700)
    84  			},
    85  			expected: expected{
    86  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
    87  				rpath: "",
    88  			},
    89  		},
    90  		{
    91  			desc: "vendor",
    92  			path: "guthib.com/foo/bar",
    93  			root: filepath.Join("guthib.com", "foo", "root"),
    94  			setup: func() error {
    95  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700)
    96  			},
    97  			expected: expected{
    98  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bar"),
    99  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   100  			},
   101  		},
   102  		{
   103  			desc: "GOPATH flat",
   104  			path: "guthib.com/foo/bar",
   105  			root: filepath.Join("guthib.com", "foo", "root"),
   106  			setup: func() error {
   107  				return os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700)
   108  			},
   109  			expected: expected{
   110  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
   111  				rpath: "",
   112  			},
   113  		},
   114  		{
   115  			desc: "vendor flat",
   116  			path: "guthib.com/foo/bar",
   117  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir"),
   118  			setup: func() error {
   119  				if err := os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700); err != nil {
   120  					return err
   121  				}
   122  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bir"), 0o700)
   123  			},
   124  			expected: expected{
   125  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bar"),
   126  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   127  			},
   128  		},
   129  		{
   130  			desc: "fallback to GOPATH",
   131  			path: "guthib.com/foo/bar",
   132  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir"),
   133  			setup: func() error {
   134  				if err := os.MkdirAll(filepath.Join(goPath, "src", "guthib.com", "foo", "bar"), 0o700); err != nil {
   135  					return err
   136  				}
   137  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bir"), 0o700)
   138  			},
   139  			expected: expected{
   140  				dir:   filepath.Join(goPath, "src", "guthib.com", "foo", "bar"),
   141  				rpath: "",
   142  			},
   143  		},
   144  		{
   145  			desc: "vendor recursive",
   146  			path: "guthib.com/foo/bar",
   147  			root: filepath.Join("guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir", "vendor", "guthib.com", "foo", "bur"),
   148  			setup: func() error {
   149  				if err := os.MkdirAll(
   150  					filepath.Join(goPath, "src", "guthib.com", "foo", "root", "vendor", "guthib.com", "foo", "bir", "vendor", "guthib.com", "foo", "bur"),
   151  					0o700); err != nil {
   152  					return err
   153  				}
   154  				return os.MkdirAll(filepath.Join(project, "vendor", "guthib.com", "foo", "bar"), 0o700)
   155  			},
   156  			expected: expected{
   157  				dir:   filepath.Join(project, "vendor", "guthib.com", "foo", "bar"),
   158  				rpath: filepath.Join("guthib.com", "foo", "root", "vendor"),
   159  			},
   160  		},
   161  	}
   162  
   163  	interp := &Interpreter{
   164  		opt: opt{
   165  			filesystem: &realFS{},
   166  		},
   167  	}
   168  
   169  	for _, test := range testCases {
   170  		test := test
   171  		t.Run(test.desc, func(t *testing.T) {
   172  			if err := os.RemoveAll(goPath); err != nil {
   173  				t.Fatal(err)
   174  			}
   175  			if err := os.MkdirAll(goPath, 0o700); err != nil {
   176  				t.Fatal(err)
   177  			}
   178  
   179  			if test.setup != nil {
   180  				err := test.setup()
   181  				if err != nil {
   182  					t.Fatal(err)
   183  				}
   184  			}
   185  
   186  			dir, rPath, err := interp.pkgDir(goPath, test.root, test.path)
   187  			if err != nil {
   188  				t.Fatal(err)
   189  			}
   190  
   191  			if dir != test.expected.dir {
   192  				t.Errorf("[dir] got: %s, want: %s", dir, test.expected.dir)
   193  			}
   194  
   195  			if rPath != test.expected.rpath {
   196  				t.Errorf(" [rpath] got: %s, want: %s", rPath, test.expected.rpath)
   197  			}
   198  		})
   199  	}
   200  }
   201  
   202  func Test_previousRoot(t *testing.T) {
   203  	testCases := []struct {
   204  		desc           string
   205  		root           string
   206  		rootPathSuffix string
   207  		expected       string
   208  	}{
   209  		{
   210  			desc:     "GOPATH",
   211  			root:     "github.com/foo/pkg/",
   212  			expected: "",
   213  		},
   214  		{
   215  			desc:     "vendor level 1",
   216  			root:     "github.com/foo/pkg/vendor/guthib.com/traefik/fromage",
   217  			expected: "github.com/foo/pkg",
   218  		},
   219  		{
   220  			desc:     "vendor level 2",
   221  			root:     "github.com/foo/pkg/vendor/guthib.com/traefik/fromage/vendor/guthib.com/traefik/fuu",
   222  			expected: "github.com/foo/pkg/vendor/guthib.com/traefik/fromage",
   223  		},
   224  		{
   225  			desc:           "vendor is sibling",
   226  			root:           "github.com/foo/bar",
   227  			rootPathSuffix: "testdata/src/github.com/foo/bar",
   228  			expected:       "github.com/foo",
   229  		},
   230  		{
   231  			desc:           "vendor is uncle",
   232  			root:           "github.com/foo/bar/baz",
   233  			rootPathSuffix: "testdata/src/github.com/foo/bar/baz",
   234  			expected:       "github.com/foo",
   235  		},
   236  	}
   237  
   238  	for _, test := range testCases {
   239  		test := test
   240  		t.Run(test.desc, func(t *testing.T) {
   241  			t.Parallel()
   242  
   243  			var rootPath string
   244  			if test.rootPathSuffix != "" {
   245  				wd, err := os.Getwd()
   246  				if err != nil {
   247  					t.Fatal(err)
   248  				}
   249  				rootPath = filepath.Join(wd, test.rootPathSuffix)
   250  			} else {
   251  				rootPath = vendor
   252  			}
   253  			p, err := previousRoot(&realFS{}, rootPath, test.root)
   254  			if err != nil {
   255  				t.Error(err)
   256  			}
   257  
   258  			if p != test.expected {
   259  				t.Errorf("got: %s, want: %s", p, test.expected)
   260  			}
   261  		})
   262  	}
   263  }