GitLabgitlab-org

cli

main
ply7 git calls303 ms105 KiB4 cachedprotocol v2
1107 lines49 KiBRaw
1//go:build !integration
3package pm
5import (
6 "os"
7 "os/user"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
16 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
19func TestMain(m *testing.M) {
20 dir, err := os.MkdirTemp("", "pm-system-root")
21 if err != nil {
22 panic(err)
23 }
24 defer func() { _ = os.RemoveAll(dir) }()
25 systemRoot = dir
26 currentUser = func() (*user.User, error) { return &user.User{HomeDir: dir}, nil }
27 m.Run()
30// configCase is one precedence case: files to write under a temp root,
31// symlinks to make there (link to target), the environment, and the directory
32// the manager runs in, relative to the root. HOME is root/home and the
33// default directory is root/proj. "$ROOT" in file contents, env values and
34// args is replaced with the root. plain resolves the plain-HTTP proxy instead
35// of the HTTPS one.
36type configCase struct {
37 name string
38 files map[string]string
39 links map[string]string
40 env []string
41 dir string
42 args []string
43 plain bool
44 want upstreamProxy
45 wantOK bool
46 wantErr string
49// writeConfigFiles writes each file under root, executable when it is a bin/
50// entry, after passing its content through expand.
51func writeConfigFiles(t *testing.T, root string, files map[string]string, expand func(string) string) {
52 t.Helper()
53 for rel, content := range files {
54 path := filepath.Join(root, filepath.FromSlash(rel))
55 require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
56 mode := os.FileMode(0o600)
57 if filepath.Base(filepath.Dir(path)) == "bin" {
58 mode = 0o755
59 }
60 require.NoError(t, os.WriteFile(path, []byte(expand(content)), mode))
61 }
64func runConfigCases(t *testing.T, manager PackageManager, cases []configCase) {
65 t.Helper()
66 resolver, ok := manager.(upstreamProxyResolver)
67 require.True(t, ok)
68 for _, tt := range cases {
69 t.Run(tt.name, func(t *testing.T) {
70 t.Parallel()
71 // Resolved, because the node lookup reports a symlink-free path.
72 root, err := filepath.EvalSymlinks(t.TempDir())
73 require.NoError(t, err)
74 expand := func(s string) string { return strings.ReplaceAll(s, "$ROOT", root) }
75 writeConfigFiles(t, root, tt.files, expand)
76 for link, target := range tt.links {
77 require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(root, link)), 0o755))
78 require.NoError(t, os.Symlink(filepath.Join(root, target), filepath.Join(root, link)))
79 }
80 env := []string{"HOME=" + filepath.Join(root, "home")}
81 for _, e := range tt.env {
82 env = append(env, expand(e))
83 }
84 dir := filepath.Join(root, "proj")
85 if tt.dir != "" {
86 dir = filepath.Join(root, filepath.FromSlash(tt.dir))
87 }
88 require.NoError(t, os.MkdirAll(dir, 0o755))
90 var args []string
91 for _, a := range tt.args {
92 args = append(args, expand(a))
93 }
94 var got upstreamProxy
95 var gotOK bool
96 if tt.plain {
97 plain, ok := manager.(plainHTTPProxyResolver)
98 require.True(t, ok)
99 got, gotOK, err = plain.resolvePlainHTTPProxy(env, dir)
100 } else {
101 got, gotOK, err = resolver.resolveUpstreamProxy(env, dir, args)
102 }
103 if tt.wantErr != "" {
104 require.ErrorContains(t, err, tt.wantErr)
105 return
106 }
107 require.NoError(t, err)
108 assert.Equal(t, tt.wantOK, gotOK)
109 expandSource := func(u upstreamProxy) upstreamProxy {
110 u.Source = expand(filepath.FromSlash(u.Source))
111 return u
112 }
113 tt.want = expandSource(tt.want)
114 if tt.want.Fallbacks != nil {
115 fallbacks := make([]upstreamProxy, len(tt.want.Fallbacks))
116 for i, f := range tt.want.Fallbacks {
117 fallbacks[i] = expandSource(f)
118 }
119 tt.want.Fallbacks = fallbacks
120 }
121 assert.Equal(t, tt.want, got)
122 })
123 }
126func TestNpmUpstreamProxyFromConfigFiles(t *testing.T) {
127 t.Parallel()
128 runConfigCases(t, NPM(), []configCase{
129 {
130 name: "the user .npmrc sets https-proxy",
131 files: map[string]string{"home/.npmrc": "https-proxy=http://user:1\n"},
132 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://user:1"},
133 wantOK: true,
134 },
135 {
136 name: "npm_config_https_proxy wins over the user .npmrc",
137 files: map[string]string{"home/.npmrc": "https-proxy=http://user:1\n"},
138 env: []string{"npm_config_https_proxy=http://env:2"},
139 want: upstreamProxy{Source: "npm_config_https_proxy", URL: "http://env:2"},
140 wantOK: true,
141 },
142 {
143 name: "https-proxy in a file wins over proxy in the environment",
144 files: map[string]string{"home/.npmrc": "https-proxy=http://user:1\n"},
145 env: []string{"npm_config_proxy=http://env:2"},
146 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://user:1"},
147 wantOK: true,
148 },
149 {
150 name: "proxy in a file wins over HTTPS_PROXY",
151 files: map[string]string{"home/.npmrc": "proxy=http://user:1\n"},
152 env: []string{"HTTPS_PROXY=http://env:2"},
153 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://user:1"},
154 wantOK: true,
155 },
156 {
157 name: "the user .npmrc wins over the global npmrc",
158 files: map[string]string{"home/.npmrc": "https-proxy=http://user:1\n", "prefix/etc/npmrc": "https-proxy=http://global:2\n"},
159 env: []string{"PREFIX=$ROOT/prefix"},
160 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://user:1"},
161 wantOK: true,
162 },
163 {
164 name: "NPM_CONFIG_USERCONFIG names the user file",
165 files: map[string]string{"other/npmrc": "https-proxy=http://other:1\n", "home/.npmrc": "https-proxy=http://user:2\n"},
166 env: []string{"NPM_CONFIG_USERCONFIG=$ROOT/other/npmrc"},
167 want: upstreamProxy{Source: "$ROOT/other/npmrc (https-proxy)", URL: "http://other:1"},
168 wantOK: true,
169 },
170 {
171 name: "global https-proxy under PREFIX wins over a user proxy",
172 files: map[string]string{"prefix/etc/npmrc": "https-proxy=http://global:1\n", "home/.npmrc": "proxy=http://user:2\n"},
173 env: []string{"PREFIX=$ROOT/prefix"},
174 want: upstreamProxy{Source: "$ROOT/prefix/etc/npmrc (https-proxy)", URL: "http://global:1"},
175 wantOK: true,
176 },
177 {
178 name: "prefix in the user .npmrc moves the global file",
179 files: map[string]string{"custom/etc/npmrc": "https-proxy=http://global:1\n", "home/.npmrc": "prefix=$ROOT/custom\n"},
180 want: upstreamProxy{Source: "$ROOT/custom/etc/npmrc (https-proxy)", URL: "http://global:1"},
181 wantOK: true,
182 },
183 {
184 name: "global prefix defaults to the node binary's install prefix",
185 files: map[string]string{"node/bin/node": "", "node/etc/npmrc": "https-proxy=http://global:1\n"},
186 env: []string{"PATH=$ROOT/node/bin"},
187 want: upstreamProxy{Source: "$ROOT/node/etc/npmrc (https-proxy)", URL: "http://global:1"},
188 wantOK: true,
189 },
190 {
191 name: "an empty https-proxy hides lower layers and falls back to proxy",
192 files: map[string]string{"home/.npmrc": "https-proxy=\n", "prefix/etc/npmrc": "https-proxy=http://global:1\nproxy=http://global:2\n"},
193 env: []string{"PREFIX=$ROOT/prefix"},
194 want: upstreamProxy{Source: "$ROOT/prefix/etc/npmrc (proxy)", URL: "http://global:2"},
195 wantOK: true,
196 },
197 {
198 name: "https-proxy=null in a file falls back to proxy",
199 files: map[string]string{"home/.npmrc": "https-proxy=null\nproxy=http://user:2\n"},
200 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://user:2"},
201 wantOK: true,
202 },
203 {
204 name: "proxy=false in a file falls back to HTTPS_PROXY",
205 files: map[string]string{"home/.npmrc": "proxy=false\n"},
206 env: []string{"HTTPS_PROXY=http://env:1"},
207 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:1"},
208 wantOK: true,
209 },
210 {
211 name: "values expand environment variables",
212 files: map[string]string{"home/.npmrc": "https-proxy=${CORP_PROXY}\n"},
213 env: []string{"CORP_PROXY=http://corp:1"},
214 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://corp:1"},
215 wantOK: true,
216 },
217 {
218 name: "quoted values and comments",
219 files: map[string]string{"home/.npmrc": "; comment\nhttps-proxy = \"http://corp:1\"\nnoproxy=internal.example ; trailing\n"},
220 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://corp:1", NoProxy: "internal.example"},
221 wantOK: true,
222 },
223 {
224 name: "noproxy from a file and NO_PROXY both apply",
225 files: map[string]string{"home/.npmrc": "https-proxy=http://corp:1\nnoproxy[]=a.example\nnoproxy[]=b.example\n"},
226 env: []string{"NO_PROXY=c.example"},
227 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://corp:1", NoProxy: "a.example,b.example,c.example"},
228 wantOK: true,
229 },
230 {
231 name: "keys in a section are not top-level settings",
232 files: map[string]string{"home/.npmrc": "[section]\nhttps-proxy=http://corp:1\n"},
233 },
234 {
235 name: "the project .npmrc is not read",
236 files: map[string]string{"proj/package.json": "{}", "proj/.npmrc": "https-proxy=http://project:1\n"},
237 env: []string{"HTTPS_PROXY=http://env:2"},
238 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:2"},
239 wantOK: true,
240 },
241 {
242 name: "the project .npmrc does not override the user .npmrc",
243 files: map[string]string{"proj/package.json": "{}", "proj/.npmrc": "https-proxy=http://project:1\n", "home/.npmrc": "https-proxy=http://user:2\n"},
244 dir: "proj/sub",
245 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://user:2"},
246 wantOK: true,
247 },
248 {
249 name: "the project .npmrc cannot move the user or global file",
250 files: map[string]string{"proj/package.json": "{}", "proj/.npmrc": "userconfig=$ROOT/evil/npmrc\nglobalconfig=$ROOT/evil/npmrc\nprefix=$ROOT/evil\n", "evil/npmrc": "https-proxy=http://evil:1\n", "evil/etc/npmrc": "https-proxy=http://evil:2\n"},
251 },
252 {
253 name: "NPM_CONFIG_USERCONFIG inside the project is not read",
254 files: map[string]string{"proj/.npmrc-ci": "https-proxy=http://project:1\n", "prefix/etc/npmrc": "proxy=http://global:2\n"},
255 env: []string{"NPM_CONFIG_USERCONFIG=$ROOT/proj/.npmrc-ci", "PREFIX=$ROOT/prefix"},
256 want: upstreamProxy{Source: "$ROOT/prefix/etc/npmrc (proxy)", URL: "http://global:2"},
257 wantOK: true,
258 },
259 {
260 name: "a PREFIX inside the project is not read",
261 files: map[string]string{"proj/tools/etc/npmrc": "https-proxy=http://project:1\n", "home/.npmrc": "proxy=http://user:2\n"},
262 env: []string{"PREFIX=$ROOT/proj/tools"},
263 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://user:2"},
264 wantOK: true,
265 },
266 {
267 name: "a HOME inside the project is not read",
268 files: map[string]string{"proj/home/.npmrc": "https-proxy=http://project:1\n"},
269 env: []string{"HOME=$ROOT/proj/home", "HTTPS_PROXY=http://env:2"},
270 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:2"},
271 wantOK: true,
272 },
273 {
274 name: "a user file that links into the project is not read",
275 files: map[string]string{"proj/.npmrc": "https-proxy=http://project:1\n"},
276 links: map[string]string{"home/.npmrc": "proj/.npmrc"},
277 env: []string{"HTTPS_PROXY=http://env:2"},
278 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:2"},
279 wantOK: true,
280 },
281 {
282 name: "the project is the git work tree around the working directory",
283 files: map[string]string{"proj/.git/HEAD": "", "proj/ci/npmrc": "https-proxy=http://project:1\n"},
284 dir: "proj/sub",
285 env: []string{"NPM_CONFIG_USERCONFIG=$ROOT/proj/ci/npmrc", "HTTPS_PROXY=http://env:2"},
286 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:2"},
287 wantOK: true,
288 },
289 {
290 name: "outside a git work tree the project is the working directory",
291 files: map[string]string{"proj/ci/npmrc": "https-proxy=http://ci:1\n"},
292 dir: "proj/sub",
293 env: []string{"NPM_CONFIG_USERCONFIG=$ROOT/proj/ci/npmrc"},
294 want: upstreamProxy{Source: "$ROOT/proj/ci/npmrc (https-proxy)", URL: "http://ci:1"},
295 wantOK: true,
296 },
297 })
300func TestPnpmReadsOnlyNpmConfigEnvironment(t *testing.T) {
301 t.Parallel()
302 runConfigCases(t, Pnpm(), []configCase{
303 {
304 name: "npm config files are ignored",
305 files: map[string]string{"proj/.npmrc": "https-proxy=http://project:1\n", "home/.npmrc": "proxy=http://user:2\n"},
306 },
307 {
308 name: "npm_config_https_proxy still applies",
309 files: map[string]string{"home/.npmrc": "https-proxy=http://user:1\n"},
310 env: []string{"npm_config_https_proxy=http://env:2"},
311 want: upstreamProxy{Source: "npm_config_https_proxy", URL: "http://env:2"},
312 wantOK: true,
313 },
314 })
317func TestYarnClassicUpstreamProxyFromConfigFiles(t *testing.T) {
318 t.Parallel()
319 runConfigCases(t, Yarn(), []configCase{
320 {
321 name: "the user .yarnrc https-proxy wins over the user .npmrc https-proxy",
322 files: map[string]string{"home/.yarnrc": "https-proxy \"http://yarnrc:1\"\n", "home/.npmrc": "https-proxy=http://npmrc:2\n"},
323 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (https-proxy)", URL: "http://yarnrc:1"},
324 wantOK: true,
325 },
326 {
327 name: "the user .yarnrc https-proxy wins over npm_config_https_proxy",
328 files: map[string]string{"home/.yarnrc": "https-proxy \"http://yarnrc:1\"\n"},
329 env: []string{"npm_config_https_proxy=http://env:2"},
330 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (https-proxy)", URL: "http://yarnrc:1"},
331 wantOK: true,
332 },
333 {
334 name: ".npmrc https-proxy wins over .yarnrc proxy",
335 files: map[string]string{"home/.yarnrc": "proxy \"http://yarnrc:1\"\n", "home/.npmrc": "https-proxy=http://npmrc:2\n"},
336 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://npmrc:2"},
337 wantOK: true,
338 },
339 {
340 name: "YARN_HTTPS_PROXY wins over files",
341 files: map[string]string{"home/.yarnrc": "https-proxy \"http://yarnrc:1\"\n"},
342 env: []string{"YARN_HTTPS_PROXY=http://env:2"},
343 want: upstreamProxy{Source: "YARN_HTTPS_PROXY", URL: "http://env:2"},
344 wantOK: true,
345 },
346 {
347 name: "YARN_PROXY wins over the user .yarnrc proxy and HTTPS_PROXY",
348 files: map[string]string{"home/.yarnrc": "proxy \"http://yarnrc:1\"\n"},
349 env: []string{"YARN_PROXY=http://env:2", "HTTPS_PROXY=http://https:3"},
350 want: upstreamProxy{Source: "YARN_PROXY", URL: "http://env:2"},
351 wantOK: true,
352 },
353 {
354 name: "YARN_HTTPS_PROXY wins over YARN_PROXY",
355 env: []string{"YARN_PROXY=http://env:2", "YARN_HTTPS_PROXY=http://env:3"},
356 want: upstreamProxy{Source: "YARN_HTTPS_PROXY", URL: "http://env:3"},
357 wantOK: true,
358 },
359 {
360 name: "YARN_PROXY in any case wins over npm_config_proxy for plain HTTP",
361 env: []string{"npm_config_proxy=http://npm:2", "yarn_proxy=http://env:3"},
362 plain: true,
363 want: upstreamProxy{Source: "yarn_proxy", URL: "http://env:3"},
364 wantOK: true,
365 },
366 {
367 name: "the user .yarnrc wins over the global yarnrc",
368 files: map[string]string{"home/.yarnrc": "https-proxy: \"http://user:1\"\n", "prefix/etc/yarnrc": "https-proxy \"http://global:2\"\n"},
369 env: []string{"PREFIX=$ROOT/prefix"},
370 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (https-proxy)", URL: "http://user:1"},
371 wantOK: true,
372 },
373 {
374 name: "the user .npmrc wins over the global npmrc",
375 files: map[string]string{"home/.npmrc": "https-proxy=http://user:1\n", "prefix/etc/npmrc": "https-proxy=http://global:2\n"},
376 env: []string{"PREFIX=$ROOT/prefix"},
377 want: upstreamProxy{Source: "$ROOT/home/.npmrc (https-proxy)", URL: "http://user:1"},
378 wantOK: true,
379 },
380 {
381 name: "YARN_USERCONFIG names the user .yarnrc",
382 files: map[string]string{"other/yarnrc": "https-proxy \"http://other:1\"\n", "home/.yarnrc": "https-proxy \"http://user:2\"\n"},
383 env: []string{"YARN_USERCONFIG=$ROOT/other/yarnrc"},
384 want: upstreamProxy{Source: "$ROOT/other/yarnrc (https-proxy)", URL: "http://other:1"},
385 wantOK: true,
386 },
387 {
388 name: "https-proxy false connects directly",
389 files: map[string]string{"home/.yarnrc": "https-proxy false\nproxy \"http://yarnrc:1\"\n"},
390 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (https-proxy)"},
391 wantOK: true,
392 },
393 {
394 name: "https-proxy null in .npmrc falls back to proxy",
395 files: map[string]string{"home/.npmrc": "https-proxy=null\nproxy=http://npmrc:1\n"},
396 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://npmrc:1"},
397 wantOK: true,
398 },
399 {
400 name: "proxy false in .npmrc leaves HTTPS_PROXY to the default",
401 files: map[string]string{"home/.npmrc": "proxy=false\n"},
402 env: []string{"HTTPS_PROXY=http://env:1"},
403 },
404 {
405 name: "NO_PROXY does not apply and HTTPS_PROXY is left to the default",
406 files: map[string]string{"home/.yarnrc": "network-timeout 600000\n"},
407 env: []string{"HTTPS_PROXY=http://env:1"},
408 },
409 {
410 name: "an unset variable in the user .npmrc fails as in yarn",
411 files: map[string]string{"home/.npmrc": "https-proxy=${MISSING}\n"},
412 wantErr: "unset environment variable",
413 },
414 {
415 name: "an unquoted URL leaves .yarnrc without settings, as in yarn",
416 files: map[string]string{"home/.yarnrc": "https-proxy http://alice:s3cret@yarnrc:1\n", "home/.npmrc": "proxy=http://npmrc:2\n"},
417 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://npmrc:2"},
418 wantOK: true,
419 },
420 {
421 name: "an unterminated quote leaves .yarnrc without settings, as in yarn",
422 files: map[string]string{"home/.yarnrc": "https-proxy \"http://alice:s3cret@yarnrc:1\nproxy \"http://yarnrc:2\"\n"},
423 env: []string{"HTTPS_PROXY=http://env:3"},
424 },
425 {
426 name: "a .yarnrc written as YAML is read, as in yarn",
427 files: map[string]string{"home/.yarnrc": "https-proxy: http://yarnrc:1\n"},
428 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (https-proxy)", URL: "http://yarnrc:1"},
429 wantOK: true,
430 },
431 {
432 name: "a .yarnrc that is neither lockfile syntax nor YAML fails as in yarn",
433 files: map[string]string{"home/.yarnrc": "[ \"http://alice:s3cret@yarnrc:1\"\n"},
434 wantErr: ".yarnrc line 1",
435 },
436 {
437 name: "comments, a quoted key and shared keys in .yarnrc",
438 files: map[string]string{"home/.yarnrc": "# comment\n\"proxy\", https-proxy \"http://yarnrc:1\" # trailing\n"},
439 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (https-proxy)", URL: "http://yarnrc:1"},
440 wantOK: true,
441 },
442 {
443 name: "an indented line ends the settings of .yarnrc, as in yarn",
444 files: map[string]string{"home/.yarnrc": "network-timeout 600000\n nested \"x\"\nproxy \"http://yarnrc:1\"\n"},
445 plain: true,
446 },
447 {
448 name: "duplicated keys in a YAML .yarnrc fail as in yarn",
449 files: map[string]string{"home/.yarnrc": "https-proxy: http://yarnrc:1\nhttps-proxy: http://yarnrc:2\n"},
450 wantErr: ".yarnrc line 1",
451 },
452 {
453 name: "NPM_CONFIG_USERCONFIG inside the project is not read",
454 files: map[string]string{"proj/.npmrc-ci": "https-proxy=http://project:1\n", "prefix/etc/npmrc": "proxy=http://global:2\n"},
455 env: []string{"NPM_CONFIG_USERCONFIG=$ROOT/proj/.npmrc-ci", "PREFIX=$ROOT/prefix"},
456 want: upstreamProxy{Source: "$ROOT/prefix/etc/npmrc (proxy)", URL: "http://global:2"},
457 wantOK: true,
458 },
459 {
460 name: "a HOME inside the project is not read",
461 files: map[string]string{"proj/home/.yarnrc": "https-proxy \"http://project:1\"\n", "proj/home/.npmrc": "proxy=http://project:2\n"},
462 env: []string{"HOME=$ROOT/proj/home"},
463 },
464 {
465 name: "YARN_USERCONFIG inside the project is not read",
466 files: map[string]string{"proj/.yarnrc-ci": "https-proxy \"http://project:1\"\n", "home/.npmrc": "proxy=http://user:2\n"},
467 env: []string{"YARN_USERCONFIG=$ROOT/proj/.yarnrc-ci", "NPM_CONFIG_USERCONFIG=$ROOT/home/.npmrc"},
468 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://user:2"},
469 wantOK: true,
470 },
471 {
472 name: "yarn_userconfig is read in any case",
473 files: map[string]string{"other/yarnrc": "https-proxy \"http://other:1\"\n"},
474 env: []string{"yarn_userconfig=$ROOT/other/yarnrc"},
475 want: upstreamProxy{Source: "$ROOT/other/yarnrc (https-proxy)", URL: "http://other:1"},
476 wantOK: true,
477 },
478 {
479 name: "npm_config_userconfig is read in any case",
480 files: map[string]string{"other/npmrc": "https-proxy=http://other:1\n", "home/.npmrc": "https-proxy=http://user:2\n"},
481 env: []string{"npm_config_userconfig=$ROOT/other/npmrc"},
482 want: upstreamProxy{Source: "$ROOT/other/npmrc (https-proxy)", URL: "http://other:1"},
483 wantOK: true,
484 },
485 {
486 name: "the project .yarnrc and .npmrc are not read",
487 files: map[string]string{"proj/package.json": "{}", "proj/.yarnrc": "https-proxy \"http://project:1\"\n", "proj/.npmrc": "proxy=http://project:2\n"},
488 env: []string{"HTTPS_PROXY=http://env:3"},
489 },
490 {
491 name: "ancestor .yarnrc and .npmrc files are not read",
492 files: map[string]string{"proj/package.json": "{}", ".yarnrc": "https-proxy \"http://ancestor:1\"\n", ".npmrc": "https-proxy=http://ancestor:2\n", "home/.npmrc": "proxy=http://user:3\n"},
493 dir: "proj/sub",
494 want: upstreamProxy{Source: "$ROOT/home/.npmrc (proxy)", URL: "http://user:3"},
495 wantOK: true,
496 },
497 {
498 name: "a malformed project .yarnrc does not fail the run",
499 files: map[string]string{"proj/.yarnrc": "https-proxy http://project:1\n", "proj/.npmrc": "https-proxy=${MISSING}\n"},
500 },
501 {
502 name: "plain HTTP reads proxy, not https-proxy",
503 files: map[string]string{"home/.yarnrc": "https-proxy \"http://yarnrc:1\"\nproxy \"http://yarnrc:2\"\n"},
504 plain: true,
505 want: upstreamProxy{Source: "$ROOT/home/.yarnrc (proxy)", URL: "http://yarnrc:2"},
506 wantOK: true,
507 },
508 {
509 name: "plain HTTP ignores https-proxy in files",
510 files: map[string]string{"home/.npmrc": "https-proxy=http://npmrc:1\n"},
511 plain: true,
512 },
513 {
514 name: "plain HTTP reads proxy from the global npmrc",
515 files: map[string]string{"prefix/etc/npmrc": "proxy=http://global:1\n"},
516 env: []string{"PREFIX=$ROOT/prefix"},
517 plain: true,
518 want: upstreamProxy{Source: "$ROOT/prefix/etc/npmrc (proxy)", URL: "http://global:1"},
519 wantOK: true,
520 },
521 {
522 name: "plain HTTP ignores the project's proxy",
523 files: map[string]string{"proj/package.json": "{}", "proj/.yarnrc": "proxy \"http://project:1\"\n", "proj/.npmrc": "proxy=http://project:2\n"},
524 plain: true,
525 },
526 })
529func TestYarnBerryUpstreamProxyFromConfigFiles(t *testing.T) {
530 t.Parallel()
531 berry := `{"packageManager": "yarn@4.9.2"}`
532 runConfigCases(t, Yarn(), []configCase{
533 {
534 name: "~/.yarnrc.yml sets httpsProxy",
535 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: http://home:1\n"},
536 want: upstreamProxy{Source: "$ROOT/home/.yarnrc.yml (httpsProxy)", URL: "http://home:1"},
537 wantOK: true,
538 },
539 {
540 name: "YARN_HTTPS_PROXY wins over ~/.yarnrc.yml and ignores NO_PROXY",
541 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: http://home:1\n"},
542 env: []string{"YARN_HTTPS_PROXY=http://env:2", "NO_PROXY=registry.yarnpkg.com"},
543 want: upstreamProxy{Source: "YARN_HTTPS_PROXY", URL: "http://env:2"},
544 wantOK: true,
545 },
546 {
547 name: "an empty YARN_HTTPS_PROXY connects directly over ~/.yarnrc.yml",
548 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: http://home:1\n"},
549 env: []string{"YARN_HTTPS_PROXY="},
550 want: upstreamProxy{Source: "YARN_HTTPS_PROXY"},
551 wantOK: true,
552 },
553 {
554 name: "values expand environment variables with defaults",
555 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: ${CORP_PROXY:-http://fallback:1}\n"},
556 want: upstreamProxy{Source: "$ROOT/home/.yarnrc.yml (httpsProxy)", URL: "http://fallback:1"},
557 wantOK: true,
558 },
559 {
560 name: "an unset variable without a default fails as in yarn",
561 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: ${MISSING}\n"},
562 wantErr: "a value names an unset environment variable",
563 },
564 {
565 name: "an empty httpsProxy connects directly",
566 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: \"\"\n"},
567 want: upstreamProxy{Source: "$ROOT/home/.yarnrc.yml (httpsProxy)"},
568 wantOK: true,
569 },
570 {
571 name: "Berry ignores .npmrc and .yarnrc",
572 files: map[string]string{"proj/package.json": berry, "home/.npmrc": "https-proxy=http://npmrc:1\n", "home/.yarnrc": "https-proxy \"http://yarnrc:2\"\n"},
573 },
574 {
575 name: "yarnPath in the project's .yarnrc.yml selects Berry, without its httpsProxy",
576 files: map[string]string{"proj/.yarnrc.yml": "yarnPath: .yarn/releases/yarn-4.9.2.cjs\nhttpsProxy: http://project:1\n", "home/.yarnrc.yml": "httpsProxy: http://home:2\n", "home/.npmrc": "https-proxy=http://npmrc:3\n"},
577 want: upstreamProxy{Source: "$ROOT/home/.yarnrc.yml (httpsProxy)", URL: "http://home:2"},
578 wantOK: true,
579 },
580 {
581 name: "the project's .yarnrc.yml is not read",
582 files: map[string]string{"proj/package.json": berry, "proj/.yarnrc.yml": "httpsProxy: http://project:1\n", "home/.yarnrc.yml": "httpsProxy: http://home:2\n"},
583 want: upstreamProxy{Source: "$ROOT/home/.yarnrc.yml (httpsProxy)", URL: "http://home:2"},
584 wantOK: true,
585 },
586 {
587 name: "an ancestor's .yarnrc.yml is not read",
588 files: map[string]string{"proj/package.json": berry, ".yarnrc.yml": "httpsProxy: http://ancestor:1\n"},
589 },
590 {
591 name: "a HOME inside the project is not read",
592 files: map[string]string{"proj/package.json": berry, "proj/home/.yarnrc.yml": "httpsProxy: http://project:1\n"},
593 env: []string{"HOME=$ROOT/proj/home"},
594 },
595 {
596 name: "YARN_RC_FILENAME does not make a project file readable",
597 files: map[string]string{"proj/package.json": berry, "proj/.custom.yml": "httpsProxy: http://custom:1\n"},
598 env: []string{"YARN_RC_FILENAME=.custom.yml"},
599 },
600 {
601 name: "plain HTTP takes the HTTPS path",
602 files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: http://home:1\n", "home/.npmrc": "proxy=http://npmrc:2\n"},
603 env: []string{"npm_config_proxy=http://env:3"},
604 plain: true,
605 },
606 })
609func TestPipUpstreamProxyFromConfigFiles(t *testing.T) {
610 t.Parallel()
611 if runtime.GOOS == "windows" {
612 t.Skip("pip's user config lives under %APPDATA% on Windows")
613 }
614 install := []string{"install", "six"}
615 runConfigCases(t, Pip(), []configCase{
616 {
617 name: "the XDG user file wins over the legacy one",
618 files: map[string]string{"home/.pip/pip.conf": "[global]\nproxy = http://legacy:1\n", "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
619 args: install,
620 want: upstreamProxy{Source: "$ROOT/home/.config/pip/pip.conf (global.proxy)", URL: "http://xdg:2"},
621 wantOK: true,
622 },
623 {
624 name: "the command's section wins over [global] in any file",
625 files: map[string]string{"home/.pip/pip.conf": "[install]\nproxy = http://legacy:1\n", "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
626 args: install,
627 want: upstreamProxy{Source: "$ROOT/home/.pip/pip.conf (install.proxy)", URL: "http://legacy:1"},
628 wantOK: true,
629 },
630 {
631 name: "another command's section does not apply",
632 files: map[string]string{"home/.config/pip/pip.conf": "[download]\nproxy = http://download:1\n[global]\nproxy = http://global:2\n"},
633 args: []string{"--no-cache-dir", "install", "six"},
634 want: upstreamProxy{Source: "$ROOT/home/.config/pip/pip.conf (global.proxy)", URL: "http://global:2"},
635 wantOK: true,
636 },
637 {
638 name: "a global option's value is not the command",
639 files: map[string]string{"home/.config/pip/pip.conf": "[global]\nproxy = http://global:1\n[install]\nproxy = http://install:2\n"},
640 args: []string{"--cache-dir", "/tmp/c", "install", "six"},
641 want: upstreamProxy{Source: "$ROOT/home/.config/pip/pip.conf (install.proxy)", URL: "http://install:2"},
642 wantOK: true,
643 },
644 {
645 name: "PIP_PROXY wins over config files",
646 files: map[string]string{"home/.config/pip/pip.conf": "[install]\nproxy = http://xdg:2\n"},
647 env: []string{"PIP_PROXY=http://env:3"},
648 args: install,
649 want: upstreamProxy{Source: "PIP_PROXY", URL: "http://env:3"},
650 wantOK: true,
651 },
652 {
653 name: "--proxy wins over PIP_PROXY and config files, and ignores NO_PROXY",
654 files: map[string]string{"home/.config/pip/pip.conf": "[install]\nproxy = http://xdg:2\n[global]\nproxy = http://global:4\n"},
655 env: []string{"PIP_PROXY=http://env:3", "NO_PROXY=pypi.org"},
656 args: []string{"install", "--proxy", "http://cli:5", "six"},
657 want: upstreamProxy{Source: "--proxy", URL: "http://cli:5"},
658 wantOK: true,
659 },
660 {
661 name: "an empty --proxy clears PIP_PROXY and config files",
662 files: map[string]string{"home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
663 env: []string{"PIP_PROXY=http://env:3", "HTTPS_PROXY=http://env:1"},
664 args: []string{"install", "--proxy=", "six"},
665 },
666 {
667 name: "--isolated leaves --proxy in effect",
668 env: []string{"PIP_PROXY=http://env:3"},
669 args: []string{"--isolated", "install", "--prox", "http://cli:5", "six"},
670 want: upstreamProxy{Source: "--proxy", URL: "http://cli:5"},
671 wantOK: true,
672 },
673 {
674 name: "PIP_CONFIG_FILE wins and replaces the user files",
675 files: map[string]string{"home/.config/pip/pip.conf": "[install]\nproxy = http://xdg:2\n", "custom.conf": "[global]\nproxy = http://custom:4\n"},
676 env: []string{"PIP_CONFIG_FILE=$ROOT/custom.conf"},
677 args: install,
678 want: upstreamProxy{Source: "$ROOT/custom.conf (global.proxy)", URL: "http://custom:4"},
679 wantOK: true,
680 },
681 {
682 name: "PIP_CONFIG_FILE set to the null device disables every file",
683 files: map[string]string{"home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
684 env: []string{"PIP_CONFIG_FILE=" + os.DevNull, "HTTPS_PROXY=http://env:1"},
685 args: install,
686 },
687 {
688 name: "an empty proxy in a later file clears an earlier one",
689 files: map[string]string{"home/.pip/pip.conf": "[global]\nproxy = http://legacy:1\n", "home/.config/pip/pip.conf": "[global]\nproxy =\n"},
690 args: install,
691 },
692 {
693 name: "--isolated skips the user files and PIP_PROXY",
694 files: map[string]string{"home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
695 env: []string{"PIP_PROXY=http://env:3"},
696 args: []string{"--isolated", "install", "six"},
697 },
698 {
699 name: "the site file of a virtualenv outside the working directory wins over user files",
700 files: map[string]string{"venv/bin/pip": "", "venv/pyvenv.cfg": "home = /usr/bin\n", "venv/pip.conf": "[global]\nproxy = http://site:5\n", "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
701 env: []string{"PATH=$ROOT/venv/bin"},
702 args: install,
703 want: upstreamProxy{Source: "$ROOT/venv/pip.conf (global.proxy)", URL: "http://site:5"},
704 wantOK: true,
705 },
706 {
707 name: "the site file of a virtualenv inside the working directory is not read",
708 files: map[string]string{"proj/.venv/bin/pip": "", "proj/.venv/pyvenv.cfg": "home = /usr/bin\n", "proj/.venv/pip.conf": "[global]\nproxy = http://project:5\n", "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
709 env: []string{"PATH=$ROOT/proj/.venv/bin"},
710 args: install,
711 want: upstreamProxy{Source: "$ROOT/home/.config/pip/pip.conf (global.proxy)", URL: "http://xdg:2"},
712 wantOK: true,
713 },
714 {
715 name: "PIP_CONFIG_FILE inside the project is not read",
716 files: map[string]string{"proj/pip.conf": "[global]\nproxy = http://project:1\n", "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
717 env: []string{"PIP_CONFIG_FILE=$ROOT/proj/pip.conf"},
718 args: install,
719 },
720 {
721 name: "the site file of a virtualenv in the git work tree is not read from a subdirectory",
722 files: map[string]string{"proj/.git/HEAD": "", "proj/.venv/bin/pip": "", "proj/.venv/pyvenv.cfg": "home = /usr/bin\n", "proj/.venv/pip.conf": "[global]\nproxy = http://project:5\n", "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n"},
723 dir: "proj/sub",
724 env: []string{"PATH=$ROOT/proj/.venv/bin"},
725 args: install,
726 want: upstreamProxy{Source: "$ROOT/home/.config/pip/pip.conf (global.proxy)", URL: "http://xdg:2"},
727 wantOK: true,
728 },
729 {
730 name: "a duplicate key fails as in pip",
731 files: map[string]string{"home/.config/pip/pip.conf": "[global]\nproxy = http://a:1\nPROXY = http://b:2\n"},
732 args: install,
733 wantErr: "pip.conf line 3",
734 },
735 })
736 runConfigCases(t, Pipenv(), []configCase{
737 {
738 name: "pipenv reads only [global]",
739 files: map[string]string{"home/.config/pip/pip.conf": "[install]\nproxy = http://install:1\n[global]\nproxy = http://global:2\n"},
740 args: install,
741 want: upstreamProxy{Source: "$ROOT/home/.config/pip/pip.conf (global.proxy)", URL: "http://global:2"},
742 wantOK: true,
743 },
744 {
745 name: "pipenv has no --proxy option",
746 env: []string{"PIP_PROXY=http://env:3"},
747 args: []string{"install", "--proxy", "http://cli:5", "six"},
748 want: upstreamProxy{Source: "PIP_PROXY", URL: "http://env:3"},
749 wantOK: true,
750 },
751 })
754func TestPipIgnoresVirtualenvInsideTheProjectThroughSymlink(t *testing.T) {
755 t.Parallel()
756 if runtime.GOOS == "windows" {
757 t.Skip("pip's user config lives under %APPDATA% on Windows")
758 }
759 root := t.TempDir()
760 writeConfigFiles(t, root, map[string]string{
761 "proj/.venv/bin/pip": "",
762 "proj/.venv/pyvenv.cfg": "home = /usr/bin\n",
763 "proj/.venv/pip.conf": "[global]\nproxy = http://project:1\n",
764 }, func(s string) string { return s })
765 link := filepath.Join(root, "venv-link")
766 require.NoError(t, os.Symlink(filepath.Join(root, "proj", ".venv"), link))
767 resolver, ok := Pip().(upstreamProxyResolver)
768 require.True(t, ok)
770 got, gotOK, err := resolver.resolveUpstreamProxy([]string{"PATH=" + filepath.Join(link, "bin")}, filepath.Join(root, "proj"), []string{"install"})
771 require.NoError(t, err)
772 assert.False(t, gotOK, "the virtualenv is inside the project, whatever path reaches it")
773 assert.Equal(t, upstreamProxy{}, got)
776func TestConfigFilesSkipFilesInsideTheProject(t *testing.T) {
777 t.Parallel()
778 root := t.TempDir()
779 writeConfigFiles(t, root, map[string]string{
780 "proj/.git/HEAD": "",
781 "proj/sub/x": "",
782 "proj/ci/npmrc": "",
783 "outside/npmrc": "",
784 "outside/sub/rc": "",
785 "plain/sub/x": "",
786 "plain/ci/npmrc": "",
787 "plain/sub/npmrc": "",
788 }, func(s string) string { return s })
789 require.NoError(t, os.Symlink(filepath.Join(root, "proj", "ci", "npmrc"), filepath.Join(root, "outside", "link")))
790 require.NoError(t, os.Symlink(filepath.Join(root, "proj"), filepath.Join(root, "outside", "dirlink")))
792 inRepo := newConfigFiles(filepath.Join(root, "proj", "sub"))
793 assert.True(t, inRepo.inProject(filepath.Join(root, "proj", "ci", "npmrc")), "the git work tree is the project")
794 assert.True(t, inRepo.inProject(filepath.Join(root, "outside", "link")), "a symlink into the project")
795 assert.True(t, inRepo.inProject(filepath.Join(root, "outside", "dirlink", "ci", "npmrc")), "a path through a symlinked directory")
796 assert.False(t, inRepo.inProject(filepath.Join(root, "outside", "npmrc")))
797 assert.False(t, inRepo.inProject(filepath.Join(root, "outside", "sub", "rc")))
799 noRepo := newConfigFiles(filepath.Join(root, "plain", "sub"))
800 assert.True(t, noRepo.inProject(filepath.Join(root, "plain", "sub", "npmrc")), "outside a git work tree the working directory is the project")
801 assert.False(t, noRepo.inProject(filepath.Join(root, "plain", "ci", "npmrc")))
804func TestConfigFilesFailClosed(t *testing.T) {
805 t.Parallel()
806 root := t.TempDir()
807 writeConfigFiles(t, root, map[string]string{"home/npmrc": "https-proxy=http://user:1\n"}, func(s string) string { return s })
808 file := filepath.Join(root, "home", "npmrc")
810 unknown := newConfigFiles(filepath.Join(root, "missing"))
811 assert.True(t, unknown.inProject(file), "with no project root, every file may be in the project")
812 _, ok, err := unknown.read(file)
813 require.NoError(t, err)
814 assert.False(t, ok)
816 require.NoError(t, os.MkdirAll(filepath.Join(root, "proj"), 0o755))
817 files := newConfigFiles(filepath.Join(root, "proj"))
818 require.NoError(t, os.Symlink(filepath.Join(root, "nowhere"), filepath.Join(root, "home", "dangling")))
819 assert.True(t, files.inProject(filepath.Join(root, "home", "dangling")), "a path that cannot be resolved may be in the project")
820 assert.False(t, files.inProject(file))
823func TestConfigFilesMatchTheProjectInAnyCase(t *testing.T) {
824 t.Parallel()
825 root, err := filepath.EvalSymlinks(t.TempDir())
826 require.NoError(t, err)
827 writeConfigFiles(t, root, map[string]string{"proj/npmrc": ""}, func(s string) string { return s })
828 if _, err := os.Stat(filepath.Join(root, "PROJ")); err != nil {
829 t.Skip("the file system is case-sensitive")
830 }
832 assert.True(t, newConfigFiles(filepath.Join(root, "proj")).inProject(filepath.Join(root, "PROJ", "npmrc")))
833 assert.True(t, newConfigFiles(filepath.Join(root, "PROJ")).inProject(filepath.Join(root, "proj", "npmrc")))
836func TestConfigPathsThatResolveInsideTheProjectAreNotRead(t *testing.T) {
837 root, err := filepath.EvalSymlinks(t.TempDir())
838 require.NoError(t, err)
839 writeConfigFiles(t, root, map[string]string{
840 "proj/.git/HEAD": "",
841 "proj/sub/pfx/etc/npmrc": "https-proxy=http://project:1\n",
842 "proj/sub/home/.npmrc": "https-proxy=http://project:1\n",
843 "proj/sub/.yarnrc": "https-proxy \"http://project:1\"\n",
844 "proj/sub/.npmrc-ci": "https-proxy=http://project:1\n",
845 "proj/sub/xdg/git/config": "[http]\n\tproxy = http://project:1\n",
846 "proj/sub/.cargo/config": "[http]\nproxy = \"http://project:1\"\n",
847 "proj/x/etc/npmrc": "https-proxy=http://project:1\n",
848 "proj/d/e/.keep": "",
849 "proj/decoy.yarnrc": "https-proxy \"http://project:1\"\n",
850 "decoy.yarnrc": "https-proxy \"http://outside:2\"\n",
851 "outside/x/etc/npmrc": "https-proxy=http://outside:2\n",
852 "global/etc/npmrc": "https-proxy=http://global:2\n",
853 }, func(s string) string { return s })
854 require.NoError(t, os.Symlink(filepath.Join("d", "e"), filepath.Join(root, "proj", "l")))
855 require.NoError(t, os.Symlink(filepath.Join(root, "proj", "sub"), filepath.Join(root, "outside", "link")))
856 home := "HOME=" + filepath.Join(root, "home")
857 // Not filepath.Join, which would drop the ".." that the OS applies after the symlink.
858 throughLink := filepath.Join(root, "proj", "l") + filepath.FromSlash("/../../decoy.yarnrc")
860 tests := []struct {
861 name string
862 manager PackageManager
863 cwd string
864 env []string
865 want upstreamProxy
866 wantOK bool
867 }{
868 {name: "npm PREFIX", manager: NPM(), env: []string{home, "PREFIX=pfx"}},
869 {name: "npm HOME", manager: NPM(), env: []string{"HOME=home"}},
870 {name: "npm NPM_CONFIG_USERCONFIG", manager: NPM(), env: []string{home, "NPM_CONFIG_USERCONFIG=.npmrc-ci"}},
871 {name: "Yarn YARN_USERCONFIG", manager: Yarn(), env: []string{home, "YARN_USERCONFIG=.yarnrc"}},
872 {name: "Yarn NPM_CONFIG_USERCONFIG", manager: Yarn(), env: []string{home, "NPM_CONFIG_USERCONFIG=.npmrc-ci"}},
873 {name: "cargo XDG_CONFIG_HOME", manager: Cargo(), env: []string{home, "XDG_CONFIG_HOME=xdg"}},
874 {name: "cargo CARGO_HOME", manager: Cargo(), env: []string{home, "CARGO_HOME=.cargo"}},
875 {name: "Yarn YARN_USERCONFIG with .. after a symlink", manager: Yarn(), env: []string{home, "YARN_USERCONFIG=" + throughLink}},
876 {name: "npm PREFIX with .. from a symlinked working directory", manager: NPM(), cwd: "outside/link", env: []string{home, "PREFIX=../x"}},
877 {
878 name: "npm PREFIX outside the project",
879 manager: NPM(),
880 env: []string{home, "PREFIX=../../global"},
881 want: upstreamProxy{Source: filepath.FromSlash("../../global/etc/npmrc") + " (https-proxy)", URL: "http://global:2"},
882 wantOK: true,
883 },
884 }
885 for _, tt := range tests {
886 t.Run(tt.name, func(t *testing.T) {
887 dir := filepath.Join(root, "proj", "sub")
888 if tt.cwd != "" {
889 dir = filepath.Join(root, filepath.FromSlash(tt.cwd))
890 }
891 t.Chdir(dir)
892 resolver, ok := tt.manager.(upstreamProxyResolver)
893 require.True(t, ok)
895 got, gotOK, err := resolver.resolveUpstreamProxy(tt.env, dir, []string{"install"})
896 require.NoError(t, err)
897 assert.Equal(t, tt.wantOK, gotOK)
898 assert.Equal(t, tt.want, got)
899 })
900 }
903func TestPipUserConfigOnMacOS(t *testing.T) {
904 t.Parallel()
905 if runtime.GOOS != "darwin" {
906 t.Skip("macOS only")
907 }
908 runConfigCases(t, Pip(), []configCase{{
909 name: "an Application Support pip directory replaces ~/.config",
910 files: map[string]string{
911 "home/Library/Application Support/pip/pip.conf": "[global]\nproxy = http://appsupport:1\n",
912 "home/.config/pip/pip.conf": "[global]\nproxy = http://xdg:2\n",
913 },
914 args: []string{"install"},
915 want: upstreamProxy{Source: "$ROOT/home/Library/Application Support/pip/pip.conf (global.proxy)", URL: "http://appsupport:1"},
916 wantOK: true,
917 }})
920func TestCargoUpstreamProxyFromConfigFiles(t *testing.T) {
921 t.Parallel()
922 runConfigCases(t, Cargo(), []configCase{
923 {
924 name: "a git config comment ending in a backslash doesn't continue",
925 files: map[string]string{"home/.gitconfig": "[http]\n# note \\\n\tsslVerify = true ; c \\\n\tproxy = http://git:2\n"},
926 want: upstreamProxy{Source: "$ROOT/home/.gitconfig (http.proxy)", URL: "http://git:2"},
927 wantOK: true,
928 },
929 {
930 name: "CARGO_HOME's config sets http.proxy",
931 files: map[string]string{"cargo/config.toml": "[http]\nproxy = \"http://home:2\"\n"},
932 env: []string{"CARGO_HOME=$ROOT/cargo"},
933 want: upstreamProxy{Source: "$ROOT/cargo/config.toml (http.proxy)", URL: "http://home:2"},
934 wantOK: true,
935 },
936 {
937 name: "CARGO_HOME defaults to ~/.cargo and keeps NO_PROXY",
938 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"home:2\"\n"},
939 env: []string{"NO_PROXY=crates.io"},
940 want: upstreamProxy{Source: "$ROOT/home/.cargo/config.toml (http.proxy)", URL: "home:2", NoProxy: "crates.io"},
941 wantOK: true,
942 },
943 {
944 name: "config without an extension wins over config.toml",
945 files: map[string]string{"home/.cargo/config": "[http]\nproxy = \"http://legacy:1\"\n", "home/.cargo/config.toml": "[http]\nproxy = \"http://toml:2\"\n"},
946 want: upstreamProxy{Source: "$ROOT/home/.cargo/config (http.proxy)", URL: "http://legacy:1"},
947 wantOK: true,
948 },
949 {
950 name: "CARGO_HTTP_PROXY wins over files",
951 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"http://home:1\"\n"},
952 env: []string{"CARGO_HTTP_PROXY=http://env:3"},
953 want: upstreamProxy{Source: "CARGO_HTTP_PROXY", URL: "http://env:3"},
954 wantOK: true,
955 },
956 {
957 name: "an empty CARGO_HTTP_PROXY connects directly",
958 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"http://home:1\"\n"},
959 env: []string{"CARGO_HTTP_PROXY=", "HTTPS_PROXY=http://env:4"},
960 want: upstreamProxy{Source: "CARGO_HTTP_PROXY"},
961 wantOK: true,
962 },
963 {
964 name: "an empty http.proxy connects directly",
965 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"\"\n", "home/.gitconfig": "[http]\n\tproxy = http://git:2\n"},
966 env: []string{"HTTPS_PROXY=http://env:4"},
967 want: upstreamProxy{Source: "$ROOT/home/.cargo/config.toml (http.proxy)"},
968 wantOK: true,
969 },
970 {
971 name: "git's http.proxy applies when cargo has none",
972 files: map[string]string{"home/.gitconfig": "[user]\n\tname = x\n[http \"https://example.com\"]\n\tproxy = http://scoped:1\n[HTTP]\n\tProxy = \"http://git:2\" ; comment\n", "home/.config/git/config": "[http]\n\tproxy = http://xdg:3\n"},
973 env: []string{"HTTPS_PROXY=http://env:4"},
974 want: upstreamProxy{Source: "$ROOT/home/.gitconfig (http.proxy)", URL: "http://git:2"},
975 wantOK: true,
976 },
977 {
978 name: "XDG git config applies without ~/.gitconfig",
979 files: map[string]string{"home/.config/git/config": "[http]\n\tproxy = http://xdg:\\\n3\n"},
980 want: upstreamProxy{Source: "$ROOT/home/.config/git/config (http.proxy)", URL: "http://xdg:3"},
981 wantOK: true,
982 },
983 {
984 name: "cargo config wins over git config",
985 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"http://cargo:1\"\n", "home/.gitconfig": "[http]\n\tproxy = http://git:2\n"},
986 want: upstreamProxy{Source: "$ROOT/home/.cargo/config.toml (http.proxy)", URL: "http://cargo:1"},
987 wantOK: true,
988 },
989 {
990 name: "malformed TOML fails as in cargo",
991 files: map[string]string{"home/.cargo/config.toml": "[http\nproxy = 1\n"},
992 wantErr: "failed to parse",
993 },
994 {
995 name: "the working directory's .cargo/config.toml is not read",
996 files: map[string]string{"proj/.cargo/config.toml": "[http]\nproxy = \"http://project:1\"\n", "home/.cargo/config.toml": "[http]\nproxy = \"http://home:2\"\n"},
997 want: upstreamProxy{Source: "$ROOT/home/.cargo/config.toml (http.proxy)", URL: "http://home:2"},
998 wantOK: true,
999 },
1001 name: "an ancestor's .cargo/config is not read",
1002 files: map[string]string{".cargo/config": "[http]\nproxy = \"http://ancestor:1\"\n", "home/.gitconfig": "[http]\n\tproxy = http://git:2\n"},
1003 dir: "proj/sub",
1004 want: upstreamProxy{Source: "$ROOT/home/.gitconfig (http.proxy)", URL: "http://git:2"},
1005 wantOK: true,
1006 },
1008 name: "a malformed project .cargo/config.toml does not fail the run",
1009 files: map[string]string{"proj/.cargo/config.toml": "[http\nproxy = 1\n"},
1010 },
1012 name: "a CARGO_HOME inside the project is not read",
1013 files: map[string]string{"proj/.cargo/config.toml": "[http]\nproxy = \"http://project:1\"\n", "home/.gitconfig": "[http]\n\tproxy = http://git:2\n"},
1014 env: []string{"CARGO_HOME=$ROOT/proj/.cargo"},
1015 want: upstreamProxy{Source: "$ROOT/home/.gitconfig (http.proxy)", URL: "http://git:2"},
1016 wantOK: true,
1017 },
1019 name: "an XDG git config inside the project is not read",
1020 files: map[string]string{"proj/xdg/git/config": "[http]\n\tproxy = http://project:1\n"},
1021 env: []string{"XDG_CONFIG_HOME=$ROOT/proj/xdg"},
1022 },
1023 })
1026func TestUpstreamProxyFuncConnectsDirectlyForEmptyURL(t *testing.T) {
1027 t.Parallel()
1028 pick, err := upstreamProxy{Source: "CARGO_HTTP_PROXY"}.proxyFunc()
1029 require.NoError(t, err)
1030 got, err := pick(nil)
1031 require.NoError(t, err)
1032 assert.Nil(t, got)
1035func TestRunRejectsMalformedManagerConfig(t *testing.T) {
1036 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
1037 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
1038 home := t.TempDir()
1039 isolateUserConfig(t, home)
1040 clearProxyEnv(t)
1041 require.NoError(t, os.MkdirAll(filepath.Join(home, ".cargo"), 0o755))
1042 require.NoError(t, os.WriteFile(filepath.Join(home, ".cargo", "config.toml"), []byte("[http\n"), 0o600))
1043 ios, _, _, _ := cmdtest.TestIOStreams()
1044 exec := &fakeExecutor{}
1046 err := Run(t.Context(), Cargo(), RunOptions{
1047 IO: ios,
1048 Executor: exec,
1049 BaseDir: t.TempDir(),
1050 ProjectID: "group/proj",
1051 Args: []string{"fetch"},
1052 })
1053 require.ErrorContains(t, err, "failed to read the cargo proxy settings")
1054 assert.Nil(t, exec.env, "the package manager must not run")
1057func TestConfigErrorsNameTheFileWithoutQuotingIt(t *testing.T) {
1058 t.Parallel()
1059 berry := `{"packageManager": "yarn@4.9.2"}`
1060 tests := []struct {
1061 name string
1062 manager PackageManager
1063 files map[string]string
1064 // want is the whole error, with $ROOT for the temp root.
1065 want string
1066 }{
1067 {name: "malformed .yarnrc", manager: Yarn(), files: map[string]string{"home/.yarnrc": "[ \"http://alice:s3cret@yarnrc:1\"\n"}, want: "failed to parse $ROOT/home/.yarnrc line 1"},
1068 {name: "unset variable in .npmrc for yarn", manager: Yarn(), files: map[string]string{"home/.npmrc": "//registry.example/:_authToken=${S3CRET_TOKEN}\n"}, want: "failed to read $ROOT/home/.npmrc: a value names an unset environment variable"},
1069 {name: "wrong type in ~/.yarnrc.yml", manager: Yarn(), files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: [\"http://alice:s3cret@yarnrc:1\"]\n"}, want: "failed to parse $ROOT/home/.yarnrc.yml line 1"},
1070 {name: "unset variable in ~/.yarnrc.yml", manager: Yarn(), files: map[string]string{"proj/package.json": berry, "home/.yarnrc.yml": "httpsProxy: ${S3CRET_VAR}\n"}, want: "httpsProxy in $ROOT/home/.yarnrc.yml: a value names an unset environment variable"},
1071 {name: "malformed package.json", manager: Yarn(), files: map[string]string{"proj/package.json": "{\n\"packageManager\": s3cret}\n"}, want: "failed to parse $ROOT/proj/package.json line 2"},
1072 {name: "duplicate pip section", manager: Pip(), files: map[string]string{"home/.config/pip/pip.conf": "[s3cret]\nproxy = http://a:1\n[s3cret]\n"}, want: "failed to parse $ROOT/home/.config/pip/pip.conf line 3"},
1073 {name: "malformed cargo config", manager: Cargo(), files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = 'http://alice:s3cret@cargo:1' s3cret\n"}, want: "failed to parse $ROOT/home/.cargo/config.toml line 2"},
1074 {name: "bad escape in git config", manager: Cargo(), files: map[string]string{"home/.gitconfig": "[http]\n\tproxy = \"http://alice:s3cret@git:1\\q\"\n"}, want: "failed to parse $ROOT/home/.gitconfig line 2"},
1075 {name: "bad escape in git config after a continued line", manager: Cargo(), files: map[string]string{"home/.gitconfig": "[http]\n\tcookieFile = a\\\n b\n\tproxy = \"http://alice:s3cret@git:1\\q\"\n"}, want: "failed to parse $ROOT/home/.gitconfig line 4"},
1077 for _, tt := range tests {
1078 t.Run(tt.name, func(t *testing.T) {
1079 t.Parallel()
1080 if tt.manager.Name() == "pip" && runtime.GOOS == "windows" {
1081 t.Skip("pip's user config lives under %APPDATA% on Windows")
1083 root, err := filepath.EvalSymlinks(t.TempDir())
1084 require.NoError(t, err)
1085 writeConfigFiles(t, root, tt.files, func(s string) string { return s })
1086 dir := filepath.Join(root, "proj")
1087 require.NoError(t, os.MkdirAll(dir, 0o755))
1088 resolver, ok := tt.manager.(upstreamProxyResolver)
1089 require.True(t, ok)
1091 _, _, err = resolver.resolveUpstreamProxy([]string{"HOME=" + filepath.Join(root, "home")}, dir, []string{"install"})
1092 require.EqualError(t, err, strings.ReplaceAll(tt.want, "$ROOT", root))
1093 })
1097func TestInvalidProxyFromAFileIsNotQuoted(t *testing.T) {
1098 t.Parallel()
1099 root := t.TempDir()
1100 writeConfigFiles(t, root, map[string]string{"home/.npmrc": "https-proxy=s3cret://alice:pw@corp:1\n"}, func(s string) string { return s })
1101 require.NoError(t, os.MkdirAll(filepath.Join(root, "proj"), 0o755))
1103 _, err := upstreamProxyOptions(NPM(), []string{"HOME=" + filepath.Join(root, "home")}, filepath.Join(root, "proj"), nil)
1104 require.ErrorContains(t, err, "invalid outbound proxy in "+filepath.Join(root, "home", ".npmrc")+" (https-proxy)")
1105 assert.NotContains(t, err.Error(), "s3cret")
1106 assert.NotContains(t, err.Error(), "alice")
Diagnostics7 git calls · 303 ms
CallStatusSizeTime0303 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0200361 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=361
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=361
git.pkt.payload.max=334
git.sideband.band1.bytes=334
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=334
git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=1327
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1327
git.pkt.payload.max=1300
git.sideband.band1.bytes=1300
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1300
git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4
Open pack map
fetch2008.1 KiB290 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=290
git.response.bytes=8282
git.response.cache=MISS
git.response.duration_ms=290
git.fetch.wants=a2df02d11aa0f40d0d7bec9887da462e9b48fe95
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=8282
git.pkt.payload.max=8255
git.sideband.band1.bytes=8255
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e211535a45c6f8
Request
1ad19eb4…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format