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