GitLabgitlab-org

cli

main
ply8 git calls32 ms102 KiB7 cachedprotocol v2
566 lines21 KiBRaw
1//go:build !integration
3package login
5import (
6 "net/url"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
16func sbtCredentialsPath(home string) string {
17 return filepath.Join(home, ".sbt", "1.0", "credentials.sbt")
20// sbt2CredentialsPath is the sbt 2.x file for a home where neither
21// SBT_CONFIG_HOME nor XDG_CONFIG_HOME is set, as setHome leaves it.
22func sbt2CredentialsPath(home string) string {
23 return filepath.Join(home, ".config", "sbt", "2", "credentials.sbt")
26func sbtCredentialsLine(host, token string) string {
27 return `credentials += Credentials("artifact-registry", "` + host + `", "__token__", "` + token + `")`
30func requireLoginSbt(t *testing.T, registry *url.URL, token string) {
31 t.Helper()
33 _, _, err := loginSbt("", registry, token)
34 require.NoError(t, err)
37// TestLoginSbt_WritesBothGlobalBases pins that one login covers a project on
38// either sbt version. Which base a build reads follows the sbt.version the
39// project declares, so a login that wrote only one would leave every project on
40// the other version sending no credential.
41func TestLoginSbt_WritesBothGlobalBases(t *testing.T) {
42 home := t.TempDir()
43 setHome(t, home)
45 written, skipped, err := loginSbt("", mustURL(t, "https://ar.example.com"), "tok1")
46 require.NoError(t, err)
48 assert.Equal(t, []sbtCredentialsFile{
49 {readBy: "sbt 1.x", path: sbtCredentialsPath(home)},
50 {readBy: "sbt 2.x", path: sbt2CredentialsPath(home)},
51 }, written)
52 assert.Empty(t, skipped)
54 for _, path := range []string{sbtCredentialsPath(home), sbt2CredentialsPath(home)} {
55 got, err := os.ReadFile(path)
56 require.NoError(t, err)
57 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1")+"\n", string(got), path)
59 info, err := os.Stat(path)
60 require.NoError(t, err)
61 assert.Equal(t, os.FileMode(0o600), info.Mode().Perm(), path)
62 }
65// TestSbtCredentialsFilesFor_SbtTwoBaseOrder pins the order sbt 2.x resolves
66// its global base in, as SysProp.defaultGlobalBaseDirectory does in sbt 2.0.8.
67// Getting it wrong writes a file sbt 2.x never opens on a machine that sets
68// one of these variables.
69func TestSbtCredentialsFilesFor_SbtTwoBaseOrder(t *testing.T) {
70 home := t.TempDir()
71 sbtConfigHome := filepath.Join(home, "sbt-config")
72 configHome := filepath.Join(home, "config-home")
74 launcherFile := sbtCredentialsFile{
75 readBy: "sbt 1.x and 2.x builds started by the sbt.bat of sbt 2.x, which read no other file, even with SBT_CONFIG_HOME set",
76 path: filepath.Join(configHome, "sbt", "credentials.sbt"),
77 }
79 tests := []struct {
80 name string
81 goos string
82 env map[string]string
83 want string
84 wantExtra []sbtCredentialsFile
85 wantSkipped string
86 }{
87 {
88 name: "SBT_CONFIG_HOME wins over XDG_CONFIG_HOME",
89 goos: "linux",
90 env: map[string]string{"SBT_CONFIG_HOME": sbtConfigHome, "XDG_CONFIG_HOME": configHome},
91 want: filepath.Join(sbtConfigHome, "2", "credentials.sbt"),
92 },
93 {
94 name: "XDG_CONFIG_HOME when SBT_CONFIG_HOME is unset",
95 goos: "linux",
96 env: map[string]string{"XDG_CONFIG_HOME": configHome},
97 want: filepath.Join(configHome, "sbt", "2", "credentials.sbt"),
98 },
99 {
100 name: "~/.config when neither is set",
101 goos: "linux",
102 want: sbt2CredentialsPath(home),
103 },
104 {
105 name: "surrounding whitespace is trimmed, as sbt trims it",
106 goos: "linux",
107 env: map[string]string{"XDG_CONFIG_HOME": " " + configHome + "\t"},
108 want: filepath.Join(configHome, "sbt", "2", "credentials.sbt"),
109 },
110 {
111 name: "a relative XDG_CONFIG_HOME is ignored, as the XDG spec requires",
112 goos: "linux",
113 env: map[string]string{"XDG_CONFIG_HOME": filepath.Join("relative", "config")},
114 want: sbt2CredentialsPath(home),
115 wantSkipped: "XDG_CONFIG_HOME",
116 },
117 {
118 name: "a relative SBT_CONFIG_HOME is ignored the same way",
119 goos: "linux",
120 env: map[string]string{"SBT_CONFIG_HOME": "relative", "XDG_CONFIG_HOME": configHome},
121 want: filepath.Join(configHome, "sbt", "2", "credentials.sbt"),
122 wantSkipped: "SBT_CONFIG_HOME",
123 },
124 {
125 name: "a blank SBT_CONFIG_HOME is skipped and reported, since sbt resolves it to the build directory",
126 goos: "linux",
127 env: map[string]string{"SBT_CONFIG_HOME": " "},
128 want: sbt2CredentialsPath(home),
129 wantSkipped: "SBT_CONFIG_HOME",
130 },
131 {
132 name: "an empty SBT_CONFIG_HOME is unset to sbt too, so it is not reported",
133 goos: "linux",
134 env: map[string]string{"SBT_CONFIG_HOME": "", "XDG_CONFIG_HOME": configHome},
135 want: filepath.Join(configHome, "sbt", "2", "credentials.sbt"),
136 },
137 {
138 name: "LOCALAPPDATA instead of XDG_CONFIG_HOME on Windows",
139 goos: "windows",
140 env: map[string]string{"LOCALAPPDATA": configHome, "XDG_CONFIG_HOME": filepath.Join(home, "xdg")},
141 want: filepath.Join(configHome, "sbt", "2", "credentials.sbt"),
142 wantExtra: []sbtCredentialsFile{launcherFile},
143 },
144 {
145 name: "XDG_CONFIG_HOME is not read on Windows",
146 goos: "windows",
147 env: map[string]string{"XDG_CONFIG_HOME": configHome},
148 want: sbt2CredentialsPath(home),
149 },
150 {
151 name: "SBT_CONFIG_HOME wins over LOCALAPPDATA on Windows, and the sbt.bat base is still written",
152 goos: "windows",
153 env: map[string]string{"SBT_CONFIG_HOME": sbtConfigHome, "LOCALAPPDATA": configHome},
154 want: filepath.Join(sbtConfigHome, "2", "credentials.sbt"),
155 wantExtra: []sbtCredentialsFile{launcherFile},
156 },
157 {
158 name: "LOCALAPPDATA is not read off Windows",
159 goos: "linux",
160 env: map[string]string{"LOCALAPPDATA": configHome},
161 want: sbt2CredentialsPath(home),
162 },
163 }
165 for _, tc := range tests {
166 t.Run(tc.name, func(t *testing.T) {
167 setHome(t, home)
168 for name, value := range tc.env {
169 t.Setenv(name, value)
170 }
172 files, skipped := sbtCredentialsFilesFor(home, tc.goos)
173 want := append([]sbtCredentialsFile{
174 {readBy: "sbt 1.x", path: sbtCredentialsPath(home)},
175 {readBy: "sbt 2.x", path: tc.want},
176 }, tc.wantExtra...)
177 assert.Equal(t, want, files, "the sbt 1.x file follows none of these variables")
178 assert.Equal(t, tc.wantSkipped, skipped)
179 })
180 }
183// TestLoginSbt_FailsNamingTheFileItWroteWhenTheSecondWriteFails pins that a
184// login which reached only the sbt 1.x base is reported as a failure, and that
185// the error names the file that does hold the token. Reporting success would
186// leave every sbt 2.x build unauthenticated after a green check.
187func TestLoginSbt_FailsNamingTheFileItWroteWhenTheSecondWriteFails(t *testing.T) {
188 home := t.TempDir()
189 setHome(t, home)
191 // A regular file where ~/.config has to be a directory, so creating the
192 // sbt 2.x base fails after the sbt 1.x file is written.
193 require.NoError(t, os.WriteFile(filepath.Join(home, ".config"), nil, 0o600))
195 written, _, err := loginSbt("", mustURL(t, "https://ar.example.com"), "tok1")
196 require.Error(t, err)
197 assert.Contains(t, err.Error(), "wrote "+sbtCredentialsPath(home)+" for sbt 1.x, but not the file for sbt 2.x")
198 assert.Equal(t, []sbtCredentialsFile{{readBy: "sbt 1.x", path: sbtCredentialsPath(home)}}, written)
200 got, readErr := os.ReadFile(sbtCredentialsPath(home))
201 require.NoError(t, readErr)
202 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1")+"\n", string(got))
205// TestLoginSbt_FirstWriteFailureWritesNothing pins the other order: a login
206// that cannot write the sbt 1.x file stops there, rather than leaving a token
207// in the sbt 2.x base behind an error.
208func TestLoginSbt_FirstWriteFailureWritesNothing(t *testing.T) {
209 home := t.TempDir()
210 setHome(t, home)
212 fixture := "/* work in progress\n"
213 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
214 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
216 written, _, err := loginSbt("", mustURL(t, "https://ar.example.com"), "tok1")
217 require.Error(t, err)
218 assert.NotContains(t, err.Error(), "wrote ")
219 assert.Empty(t, written)
221 _, statErr := os.Stat(sbt2CredentialsPath(home))
222 assert.ErrorIs(t, statErr, os.ErrNotExist)
225func TestLoginSbt_CreatesFileWhenAbsent(t *testing.T) {
226 home := t.TempDir()
227 setHome(t, home)
229 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
231 want := sbtCredentialsLine("ar.example.com", "tok1") + "\n"
233 got, err := os.ReadFile(sbtCredentialsPath(home))
234 require.NoError(t, err)
235 assert.Equal(t, want, string(got))
237 fileInfo, err := os.Stat(sbtCredentialsPath(home))
238 require.NoError(t, err)
239 assert.Equal(t, os.FileMode(0o600), fileInfo.Mode().Perm())
241 dirInfo, err := os.Stat(filepath.Join(home, ".sbt", "1.0"))
242 require.NoError(t, err)
243 assert.Equal(t, os.FileMode(0o700), dirInfo.Mode().Perm())
246func TestLoginSbt_PreservesUnrelatedContent(t *testing.T) {
247 home := t.TempDir()
248 setHome(t, home)
250 fixture := sbtCredentialsLine("other.example.com", "other-secret") + "\n"
251 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
252 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
254 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
256 got, err := os.ReadFile(sbtCredentialsPath(home))
257 require.NoError(t, err)
258 content := string(got)
260 assert.Contains(t, content, sbtCredentialsLine("other.example.com", "other-secret"))
261 assert.Contains(t, content, sbtCredentialsLine("ar.example.com", "tok1"))
264func TestLoginSbt_UpdatesExistingHostInPlace(t *testing.T) {
265 home := t.TempDir()
266 setHome(t, home)
268 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
269 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok2")
271 got, err := os.ReadFile(sbtCredentialsPath(home))
272 require.NoError(t, err)
273 content := string(got)
275 assert.Equal(t, 1, strings.Count(content, `"ar.example.com"`), "must not duplicate the Credentials(...) line")
276 assert.Contains(t, content, sbtCredentialsLine("ar.example.com", "tok2"))
277 assert.NotContains(t, content, sbtCredentialsLine("ar.example.com", "tok1"))
280// TestLoginSbt_StripsThePort pins the host written for a registry on a
281// non-standard port. sbt's IvyAuthenticator and coursier look credentials up
282// by the port-less requesting host, so a "host:port" entry never matches and
283// the build request goes out unauthenticated — a 401 with nothing wrong at
284// login time.
285func TestLoginSbt_StripsThePort(t *testing.T) {
286 home := t.TempDir()
287 setHome(t, home)
289 requireLoginSbt(t, mustURL(t, "https://ar.example.com:8443"), "tok1")
291 got, err := os.ReadFile(sbtCredentialsPath(home))
292 require.NoError(t, err)
293 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1")+"\n", string(got))
296// TestLoginSbt_RefreshMatchesPortedAndPortlessRegistry covers the upsert side
297// of stripping the port: the same registry given with and without its port
298// must land on one line, not two.
299func TestLoginSbt_RefreshMatchesPortedAndPortlessRegistry(t *testing.T) {
300 home := t.TempDir()
301 setHome(t, home)
303 requireLoginSbt(t, mustURL(t, "https://ar.example.com:8443"), "tok1")
304 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok2")
306 got, err := os.ReadFile(sbtCredentialsPath(home))
307 require.NoError(t, err)
308 content := string(got)
310 assert.Equal(t, 1, strings.Count(content, `"ar.example.com"`))
311 assert.Contains(t, content, sbtCredentialsLine("ar.example.com", "tok2"))
314// TestLoginSbt_RefreshMatchesCRLFFile pins that a credentials.sbt with Windows
315// line endings is refreshed rather than appended to. upsertLines splits on "\n"
316// only, so every line arrives with a trailing carriage return; an anchored
317// pattern that does not allow for it never matches the line it wrote, and every
318// login adds another entry holding a live token.
319func TestLoginSbt_RefreshMatchesCRLFFile(t *testing.T) {
320 home := t.TempDir()
321 setHome(t, home)
323 fixture := sbtCredentialsLine("ar.example.com", "tok1") + "\r\n"
324 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
325 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
327 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok2")
329 got, err := os.ReadFile(sbtCredentialsPath(home))
330 require.NoError(t, err)
331 content := string(got)
333 assert.Equal(t, 1, strings.Count(content, "credentials +="), "must refresh the line, not append a second one")
334 assert.Contains(t, content, sbtCredentialsLine("ar.example.com", "tok2"))
335 assert.NotContains(t, content, "tok1")
338// TestLoginSbt_LowerCasesTheHost pins that the recorded host is lower-cased, so
339// the same registry typed two ways lands on one entry rather than two that
340// disagree about which token is current.
341func TestLoginSbt_LowerCasesTheHost(t *testing.T) {
342 home := t.TempDir()
343 setHome(t, home)
345 requireLoginSbt(t, mustURL(t, "https://AR.Example.COM:8443"), "tok1")
346 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok2")
348 got, err := os.ReadFile(sbtCredentialsPath(home))
349 require.NoError(t, err)
350 content := string(got)
352 assert.Equal(t, 1, strings.Count(content, "credentials +="))
353 assert.Contains(t, content, sbtCredentialsLine("ar.example.com", "tok2"))
356// TestSbtCredentialLineRegexp_QuotesRealmMetacharacters pins that the realm is
357// regex-quoted before it is interpolated into the match pattern.
358//
359// The realm is a constant this package writes into a file it later has to
360// find again (see sbtCredentialRealm). A realm carrying "(" or "[" would still compile
361// without QuoteMeta, and would still be written to the file, but would stop
362// matching the line this package writes, so every login would append another
363// entry holding a live token instead of refreshing the existing one.
364func TestSbtCredentialLineRegexp_QuotesRealmMetacharacters(t *testing.T) {
365 for _, realm := range []string{
366 "Artifact Registry",
367 "Artifact Registry (v2)",
368 "GitLab Artifact Registry [beta]",
369 "ar.example.com",
370 } {
371 t.Run(realm, func(t *testing.T) {
372 re := sbtCredentialLineRegexp(realm)
374 m := re.FindStringSubmatch(sbtCredentialLine(realm, "ar.example.com", "tok1"))
376 require.NotNil(t, m, "the pattern must match the line the same realm produces")
377 assert.Equal(t, "ar.example.com", m[1])
378 })
379 }
382// TestSbtCredentialLineRegexp_DoesNotMatchAnotherRealm guards the flip side of
383// quoting: a metacharacter must be matched literally, not as a wildcard.
384func TestSbtCredentialLineRegexp_DoesNotMatchAnotherRealm(t *testing.T) {
385 re := sbtCredentialLineRegexp("ar.example.com")
387 assert.Nil(t, re.FindStringSubmatch(sbtCredentialLine("arXexampleXcom", "h", "tok1")))
390func TestLoginSbt_DifferentHostAddsSeparateEntry(t *testing.T) {
391 home := t.TempDir()
392 setHome(t, home)
394 requireLoginSbt(t, mustURL(t, "https://ar1.example.com"), "tok1")
395 requireLoginSbt(t, mustURL(t, "https://ar2.example.com"), "tok2")
397 got, err := os.ReadFile(sbtCredentialsPath(home))
398 require.NoError(t, err)
399 content := string(got)
401 assert.Contains(t, content, sbtCredentialsLine("ar1.example.com", "tok1"))
402 assert.Contains(t, content, sbtCredentialsLine("ar2.example.com", "tok2"))
405// TestLoginSbt_LeavesACommentedEntryAlone pins that an entry inside a /* */
406// comment is not refreshed in place. sbt compiles credentials.sbt as Scala, so
407// updating the commented line would put a live token where sbt never reads it:
408// the build would still go out unauthenticated, after a login that reported
409// success.
410func TestLoginSbt_LeavesACommentedEntryAlone(t *testing.T) {
411 home := t.TempDir()
412 setHome(t, home)
414 fixture := "/*\n" + sbtCredentialsLine("ar.example.com", "commented-out") + "\n*/\n"
415 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
416 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
418 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
420 got, err := os.ReadFile(sbtCredentialsPath(home))
421 require.NoError(t, err)
422 content := string(got)
424 assert.Contains(t, content, sbtCredentialsLine("ar.example.com", "commented-out"), "the commented entry stays as it was")
425 lines := strings.Split(strings.TrimSuffix(content, "\n"), "\n")
426 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1"), lines[len(lines)-1], "the live entry is appended after the comment")
429// TestLoginSbt_LeavesALineCommentedEntryAlone covers the line-comment form of
430// the same case.
431func TestLoginSbt_LeavesALineCommentedEntryAlone(t *testing.T) {
432 home := t.TempDir()
433 setHome(t, home)
435 fixture := "// " + sbtCredentialsLine("ar.example.com", "commented-out") + "\n"
436 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
437 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
439 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
441 got, err := os.ReadFile(sbtCredentialsPath(home))
442 require.NoError(t, err)
443 content := string(got)
445 assert.Contains(t, content, "// "+sbtCredentialsLine("ar.example.com", "commented-out"))
446 lines := strings.Split(strings.TrimSuffix(content, "\n"), "\n")
447 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1"), lines[len(lines)-1])
450// TestLoginSbt_RefusesAnUnterminatedComment pins that a file ending inside a
451// comment it never closes is left untouched, with an error naming that comment.
452// The credentials line would otherwise be appended inside it, which is a login
453// that quietly does nothing.
454func TestLoginSbt_RefusesAnUnterminatedComment(t *testing.T) {
455 home := t.TempDir()
456 setHome(t, home)
458 fixture := "/* work in progress\n" + sbtCredentialsLine("other.example.com", "old") + "\n"
459 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
460 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
462 _, _, err := loginSbt("", mustURL(t, "https://ar.example.com"), "tok1")
463 require.Error(t, err)
464 assert.Contains(t, err.Error(), "unterminated /* comment")
466 got, readErr := os.ReadFile(sbtCredentialsPath(home))
467 require.NoError(t, readErr)
468 assert.Equal(t, fixture, string(got), "the file must be left untouched")
471// TestLoginSbt_RefreshMatchesAnIndentedEntry pins that an entry the user
472// indented, or spaced differently, is refreshed rather than duplicated. Scala
473// ignores that whitespace, so both spellings are the same credential, and sbt
474// would otherwise hold two entries for one host that disagree on the token.
475func TestLoginSbt_RefreshMatchesAnIndentedEntry(t *testing.T) {
476 home := t.TempDir()
477 setHome(t, home)
479 fixture := ` credentials += Credentials( "artifact-registry" , "ar.example.com" , "__token__" , "old" )` + "\n"
480 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
481 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
483 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
485 got, err := os.ReadFile(sbtCredentialsPath(home))
486 require.NoError(t, err)
488 assert.Equal(t, " "+sbtCredentialsLine("ar.example.com", "tok1")+"\n", string(got), "the entry is refreshed in place, keeping its indentation")
491// TestLoginSbt_RefreshKeepsATrailingComment pins the same for an entry with a
492// note on the end of it: the token is refreshed, the note stays.
493func TestLoginSbt_RefreshKeepsATrailingComment(t *testing.T) {
494 home := t.TempDir()
495 setHome(t, home)
497 fixture := sbtCredentialsLine("ar.example.com", "old") + " // refreshed by glab\n"
498 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
499 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
501 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
503 got, err := os.ReadFile(sbtCredentialsPath(home))
504 require.NoError(t, err)
506 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1")+" // refreshed by glab\n", string(got))
509// TestLoginSbt_RefreshKeepsATrailingBlockComment covers the "/* */" spelling of
510// a trailing comment. Missing it appends a second entry for the host, and
511// Credentials.forHost takes the first one it finds, so sbt would keep sending
512// the stale token while this command reported a fresh login.
513func TestLoginSbt_RefreshKeepsATrailingBlockComment(t *testing.T) {
514 home := t.TempDir()
515 setHome(t, home)
517 fixture := sbtCredentialsLine("ar.example.com", "old") + " /* written by glab */\n"
518 require.NoError(t, os.MkdirAll(filepath.Dir(sbtCredentialsPath(home)), 0o700))
519 require.NoError(t, os.WriteFile(sbtCredentialsPath(home), []byte(fixture), 0o600))
521 requireLoginSbt(t, mustURL(t, "https://ar.example.com"), "tok1")
523 got, err := os.ReadFile(sbtCredentialsPath(home))
524 require.NoError(t, err)
526 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1")+" /* written by glab */\n", string(got))
527 assert.Equal(t, 1, strings.Count(string(got), "credentials +="), "must refresh the entry, not add a second one")
530// TestSbtCommentedLines pins the nesting and the line-comment rule the scan
531// relies on.
532func TestSbtCommentedLines(t *testing.T) {
533 lines := []string{
534 "credentials += one",
535 "/* /* still open",
536 "inside",
537 "*/ still inside",
538 "*/",
539 "credentials += two",
540 "// /* not a comment",
541 "credentials += three",
542 }
544 commented, depth := sbtCommentedLines(lines)
546 assert.Equal(t, []bool{false, false, true, true, true, false, false, false}, commented)
547 assert.Equal(t, 0, depth)
550func TestLoginSbt_WritesConfigFile(t *testing.T) {
551 home := t.TempDir()
552 setHome(t, home)
554 path := filepath.Join(t.TempDir(), "sbt", "2", "credentials.sbt")
555 written, skipped, err := loginSbt(path, mustURL(t, "https://ar.example.com"), "tok1")
556 require.NoError(t, err)
557 assert.Equal(t, []sbtCredentialsFile{{readBy: "--config-file", path: path}}, written)
558 assert.Empty(t, skipped)
560 got, err := os.ReadFile(path)
561 require.NoError(t, err)
562 assert.Equal(t, sbtCredentialsLine("ar.example.com", "tok1")+"\n", string(got))
564 _, err = os.Stat(sbtCredentialsPath(home))
565 assert.ErrorIs(t, err, os.ErrNotExist, "nothing may be written under ~/.sbt when a path is given")
Diagnostics8 git calls · 32 ms
CallStatusSizeTime032 ms
discover200185 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 KiB · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=4
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273
git.response.cache=HIT
git.response.duration_ms=2
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:0 · deepen 1020032 KiB · hit5 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=32926
git.response.cache=HIT
git.response.duration_ms=5
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
fetchtree:02001.8 KiB · 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=1857
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
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=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200242 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=242
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=5da85e254ab77f5f6e7e1419be57d524a9f8208e
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=242
git.pkt.payload.max=215
git.sideband.band1.bytes=215
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=215
git.pack.trailer=bd426fb1ae09d9fdcf755902c6903cd2eea819b2
Open pack map
fetchtree:0200494 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=20
git.response.bytes=494
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=400358373377bb8e4922d8b821520ebdf8130c4d
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=494
git.pkt.payload.max=467
git.sideband.band1.bytes=467
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=467
git.pack.trailer=363b27e4d94209a9331c11b44fe1ce6e5f4e6e81
Open pack map
fetch2004.6 KiB · hit20 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4749
git.response.cache=HIT
git.response.duration_ms=20
git.fetch.wants=39bb150f4e8a5a9c2c3e650d0218fdb41b5b9d50
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=4749
git.pkt.payload.max=4722
git.sideband.band1.bytes=4722
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
a5e2f8adbfc090c0
Request
2fc5e328…
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