GitLabgitlab-org

cli

main
ply8 git calls435 ms103 KiB6 cachedprotocol v2
780 lines27 KiBRaw
1//go:build !integration
3package login
5import (
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
15func settingsPath(home string) string {
16 return filepath.Join(home, ".m2", "settings.xml")
19func wantServerBlock(alias, token string) string {
20 return wantServerBlockIndented(" ", alias, token)
23// wantServerBlockIndented renders the expected <server> block at an arbitrary
24// indentation, for the fixtures that don't use the two-space convention.
25func wantServerBlockIndented(indent, alias, token string) string {
26 return indent + "<server>\n" +
27 indent + " <id>" + alias + "</id>\n" +
28 indent + " <username>__token__</username>\n" +
29 indent + " <password>" + token + "</password>\n" +
30 indent + "</server>\n"
33func TestLoginMaven_CreatesFileWhenAbsent(t *testing.T) {
34 home := t.TempDir()
35 setHome(t, home)
37 require.NoError(t, loginMaven("", "my-alias", "tok1"))
39 want := "<settings>\n" +
40 " <servers>\n" +
41 wantServerBlock("my-alias", "tok1") +
42 " </servers>\n" +
43 "</settings>\n"
45 got, err := os.ReadFile(settingsPath(home))
46 require.NoError(t, err)
47 assert.Equal(t, want, string(got))
49 fileInfo, err := os.Stat(settingsPath(home))
50 require.NoError(t, err)
51 assert.Equal(t, os.FileMode(0o600), fileInfo.Mode().Perm())
53 dirInfo, err := os.Stat(filepath.Join(home, ".m2"))
54 require.NoError(t, err)
55 assert.Equal(t, os.FileMode(0o700), dirInfo.Mode().Perm())
58func TestLoginMaven_PreservesUnrelatedContent(t *testing.T) {
59 home := t.TempDir()
60 setHome(t, home)
62 fixture := "<settings>\n" +
63 " <servers>\n" +
64 wantServerBlock("other-server", "other-secret") +
65 " </servers>\n" +
66 " <mirrors>\n" +
67 " <mirror>\n" +
68 " <id>central-mirror</id>\n" +
69 " <url>https://mirror.example.com/repo</url>\n" +
70 " <mirrorOf>central</mirrorOf>\n" +
71 " </mirror>\n" +
72 " </mirrors>\n" +
73 " <!-- a comment that should survive -->\n" +
74 "</settings>\n"
76 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
77 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
79 require.NoError(t, loginMaven("", "my-alias", "tok1"))
81 got, err := os.ReadFile(settingsPath(home))
82 require.NoError(t, err)
83 content := string(got)
85 assert.Contains(t, content, wantServerBlock("other-server", "other-secret"))
86 assert.Contains(t, content, "<mirrors>")
87 assert.Contains(t, content, "central-mirror")
88 assert.Contains(t, content, "https://mirror.example.com/repo")
89 assert.Contains(t, content, "<!-- a comment that should survive -->")
90 assert.Contains(t, content, wantServerBlock("my-alias", "tok1"))
93func TestLoginMaven_UpdatesExistingAliasInPlace(t *testing.T) {
94 home := t.TempDir()
95 setHome(t, home)
97 require.NoError(t, loginMaven("", "my-alias", "tok1"))
98 require.NoError(t, loginMaven("", "my-alias", "tok2"))
100 got, err := os.ReadFile(settingsPath(home))
101 require.NoError(t, err)
102 content := string(got)
104 assert.Equal(t, 1, strings.Count(content, "<id>my-alias</id>"), "must not duplicate the <server> entry")
105 assert.Contains(t, content, "<password>tok2</password>")
106 assert.NotContains(t, content, "<password>tok1</password>")
109func TestLoginMaven_DifferentAliasAddsSeparateEntry(t *testing.T) {
110 home := t.TempDir()
111 setHome(t, home)
113 require.NoError(t, loginMaven("", "alias-one", "tok1"))
114 require.NoError(t, loginMaven("", "alias-two", "tok2"))
116 got, err := os.ReadFile(settingsPath(home))
117 require.NoError(t, err)
118 content := string(got)
120 assert.Contains(t, content, wantServerBlock("alias-one", "tok1"))
121 assert.Contains(t, content, wantServerBlock("alias-two", "tok2"))
122 assert.Equal(t, 1, strings.Count(content, "<id>alias-one</id>"))
123 assert.Equal(t, 1, strings.Count(content, "<id>alias-two</id>"))
126func TestLoginMaven_AddsServersSectionWhenAbsent(t *testing.T) {
127 home := t.TempDir()
128 setHome(t, home)
130 // A settings.xml holding only <mirrors> is common, and used to fail with
131 // "no <servers> closing tag found", leaving the user to hand-edit XML.
132 fixture := "<settings>\n" +
133 " <mirrors>\n" +
134 " <mirror>\n" +
135 " <id>central-mirror</id>\n" +
136 " </mirror>\n" +
137 " </mirrors>\n" +
138 "</settings>\n"
140 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
141 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
143 require.NoError(t, loginMaven("", "my-alias", "tok1"))
145 want := "<settings>\n" +
146 " <mirrors>\n" +
147 " <mirror>\n" +
148 " <id>central-mirror</id>\n" +
149 " </mirror>\n" +
150 " </mirrors>\n" +
151 " <servers>\n" +
152 wantServerBlock("my-alias", "tok1") +
153 " </servers>\n" +
154 "</settings>\n"
156 got, err := os.ReadFile(settingsPath(home))
157 require.NoError(t, err)
158 assert.Equal(t, want, string(got))
161// TestLoginMaven_InsertedBlockFollowsSectionIndentation pins that a block
162// inserted before an existing </servers> inherits that line's indentation,
163// rather than a hardcoded four spaces that would leave the block misaligned
164// with its siblings in a tab-indented file.
165func TestLoginMaven_InsertedBlockFollowsSectionIndentation(t *testing.T) {
166 home := t.TempDir()
167 setHome(t, home)
169 fixture := "<settings>\n" +
170 "\t<servers>\n" +
171 "\t</servers>\n" +
172 "</settings>\n"
174 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
175 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
177 require.NoError(t, loginMaven("", "my-alias", "tok1"))
179 want := "<settings>\n" +
180 "\t<servers>\n" +
181 wantServerBlockIndented("\t ", "my-alias", "tok1") +
182 "\t</servers>\n" +
183 "</settings>\n"
185 got, err := os.ReadFile(settingsPath(home))
186 require.NoError(t, err)
187 assert.Equal(t, want, string(got))
190// TestLoginMaven_EscapesTokenAsXMLText covers a token carrying characters that
191// are markup in XML text content. Writing them literally would produce a
192// settings.xml Maven rejects, and one whose <password> element these regexes
193// could no longer replace on the next refresh.
194func TestLoginMaven_EscapesTokenAsXMLText(t *testing.T) {
195 home := t.TempDir()
196 setHome(t, home)
198 require.NoError(t, loginMaven("", "my-alias", "a&b<c>d"))
200 got, err := os.ReadFile(settingsPath(home))
201 require.NoError(t, err)
202 assert.Contains(t, string(got), "<password>a&amp;b&lt;c&gt;d</password>")
204 // A refresh over the escaped block must still find and replace it, rather
205 // than adding a second <server> for the same alias.
206 require.NoError(t, loginMaven("", "my-alias", "tok2"))
208 got, err = os.ReadFile(settingsPath(home))
209 require.NoError(t, err)
210 content := string(got)
211 assert.Contains(t, content, "<password>tok2</password>")
212 assert.NotContains(t, content, "a&amp;b&lt;c&gt;d")
213 assert.Equal(t, 1, strings.Count(content, "<id>my-alias</id>"))
216// TestLoginMaven_EscapesTokenWhenAddingCredentialsToExistingBlock covers the
217// other write path for a token: setMavenServerChild inserting a <password>
218// into a user's existing <server> block, which escapes separately from
219// mavenServerBlock.
220func TestLoginMaven_EscapesTokenWhenAddingCredentialsToExistingBlock(t *testing.T) {
221 home := t.TempDir()
222 setHome(t, home)
224 fixture := "<settings>\n" +
225 " <servers>\n" +
226 " <server>\n" +
227 " <id>my-alias</id>\n" +
228 " </server>\n" +
229 " </servers>\n" +
230 "</settings>\n"
232 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
233 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
235 require.NoError(t, loginMaven("", "my-alias", "a&b"))
237 got, err := os.ReadFile(settingsPath(home))
238 require.NoError(t, err)
239 assert.Contains(t, string(got), "<password>a&amp;b</password>")
242func TestLoginMaven_ExpandsSelfClosingServers(t *testing.T) {
243 home := t.TempDir()
244 setHome(t, home)
246 // <servers/> is valid and means "no servers configured", but it carries no
247 // closing tag to insert a block before.
248 fixture := "<settings>\n" +
249 " <servers/>\n" +
250 "</settings>\n"
252 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
253 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
255 require.NoError(t, loginMaven("", "my-alias", "tok1"))
257 want := "<settings>\n" +
258 " <servers>\n" +
259 wantServerBlock("my-alias", "tok1") +
260 " </servers>\n" +
261 "</settings>\n"
263 got, err := os.ReadFile(settingsPath(home))
264 require.NoError(t, err)
265 assert.Equal(t, want, string(got))
268// TestLoginMaven_LeavesFileIntactOnError pins that an unusable settings.xml is
269// never partially rewritten: the caller can fix the file by hand and retry.
270func TestLoginMaven_LeavesFileIntactOnError(t *testing.T) {
271 home := t.TempDir()
272 setHome(t, home)
274 fixture := "<not-settings>\n <mirrors/>\n</not-settings>\n"
275 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
276 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
278 err := loginMaven("", "my-alias", "tok1")
279 require.ErrorContains(t, err, "</settings>")
281 got, readErr := os.ReadFile(settingsPath(home))
282 require.NoError(t, readErr)
283 assert.Equal(t, fixture, string(got), "the file must be left byte-for-byte intact")
286// TestLoginMaven_RefusesToAddASecondServersSection covers a <servers> element
287// whose tags share a line: neither the </servers> nor the <servers/> pattern
288// can place a block in it, and adding a whole new section would leave the file
289// with two <servers> elements, which is invalid.
290func TestLoginMaven_RefusesToAddASecondServersSection(t *testing.T) {
291 home := t.TempDir()
292 setHome(t, home)
294 fixture := "<settings>\n <servers></servers>\n</settings>\n"
295 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
296 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
298 err := loginMaven("", "my-alias", "tok1")
299 require.ErrorContains(t, err, "cannot edit")
301 got, readErr := os.ReadFile(settingsPath(home))
302 require.NoError(t, readErr)
303 assert.Equal(t, fixture, string(got))
304 assert.Equal(t, 1, strings.Count(string(got), "<servers>"))
307// TestLoginMaven_PreservesUserAddedChildrenOnRefresh covers the token-refresh
308// case: replacing the whole <server> block with the canonical four lines would
309// silently drop everything the user added to it.
310func TestLoginMaven_PreservesUserAddedChildrenOnRefresh(t *testing.T) {
311 home := t.TempDir()
312 setHome(t, home)
314 fixture := "<settings>\n" +
315 " <servers>\n" +
316 " <server>\n" +
317 " <id>my-alias</id>\n" +
318 " <username>__token__</username>\n" +
319 " <password>tok1</password>\n" +
320 " <!-- keep me -->\n" +
321 " <filePermissions>660</filePermissions>\n" +
322 " <configuration>\n" +
323 " <httpHeaders>\n" +
324 " <property>\n" +
325 " <name>X-Custom</name>\n" +
326 " <value>1</value>\n" +
327 " </property>\n" +
328 " </httpHeaders>\n" +
329 " </configuration>\n" +
330 " </server>\n" +
331 " </servers>\n" +
332 "</settings>\n"
334 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
335 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
337 require.NoError(t, loginMaven("", "my-alias", "tok2"))
339 got, err := os.ReadFile(settingsPath(home))
340 require.NoError(t, err)
341 content := string(got)
343 assert.Contains(t, content, "<password>tok2</password>")
344 assert.NotContains(t, content, "<password>tok1</password>")
345 assert.Contains(t, content, "<!-- keep me -->")
346 assert.Contains(t, content, "<filePermissions>660</filePermissions>")
347 assert.Contains(t, content, "<name>X-Custom</name>")
348 assert.Equal(t, 1, strings.Count(content, "<id>my-alias</id>"))
351// TestLoginMaven_AddsCredentialsToBlockWithoutThem covers a <server> block a
352// user created with only an <id>, for example while setting up a mirror.
353func TestLoginMaven_AddsCredentialsToBlockWithoutThem(t *testing.T) {
354 home := t.TempDir()
355 setHome(t, home)
357 fixture := "<settings>\n" +
358 " <servers>\n" +
359 " <server>\n" +
360 " <id>my-alias</id>\n" +
361 " </server>\n" +
362 " </servers>\n" +
363 "</settings>\n"
365 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
366 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
368 require.NoError(t, loginMaven("", "my-alias", "tok1"))
370 got, err := os.ReadFile(settingsPath(home))
371 require.NoError(t, err)
372 assert.Equal(t, "<settings>\n"+
373 " <servers>\n"+
374 wantServerBlock("my-alias", "tok1")+
375 " </servers>\n"+
376 "</settings>\n", string(got))
379// TestLoginMaven_UpdatingSecondAliasPreservesFirst guards against a regex
380// that finds <id>alias-two</id> by lazily scanning from the *first*
381// <server> tag in the file: if alias-two's block isn't the first one,
382// such a regex spans across alias-one's whole block to reach alias-two's
383// <id>, and replacing that span silently deletes alias-one's entry.
384func TestLoginMaven_UpdatingSecondAliasPreservesFirst(t *testing.T) {
385 home := t.TempDir()
386 setHome(t, home)
388 require.NoError(t, loginMaven("", "alias-one", "tok1"))
389 require.NoError(t, loginMaven("", "alias-two", "tok2"))
390 // alias-two's <server> block now sits after alias-one's. Refreshing it
391 // (a normal token-renewal call) must not disturb alias-one.
392 require.NoError(t, loginMaven("", "alias-two", "tok2-refreshed"))
394 got, err := os.ReadFile(settingsPath(home))
395 require.NoError(t, err)
396 content := string(got)
398 assert.Contains(t, content, wantServerBlock("alias-one", "tok1"), "alias-one must survive refreshing alias-two")
399 assert.Contains(t, content, wantServerBlock("alias-two", "tok2-refreshed"))
400 assert.Equal(t, 1, strings.Count(content, "<id>alias-one</id>"))
401 assert.Equal(t, 1, strings.Count(content, "<id>alias-two</id>"))
404// writeSettings puts fixture at ~/.m2/settings.xml for a home that setHome has
405// already pointed at.
406func writeSettings(t *testing.T, home, fixture string) {
407 t.Helper()
409 require.NoError(t, os.MkdirAll(filepath.Dir(settingsPath(home)), 0o700))
410 require.NoError(t, os.WriteFile(settingsPath(home), []byte(fixture), 0o600))
413// TestLoginMaven_RefusesOneLineServerBlock covers the shape that used to make
414// this command a silent no-op: mavenServerBlockRe matches a one-line block, but
415// mavenServerCloseRe needs </server> at the start of a line, so both credential
416// inserts were skipped and the caller still printed a success line. Maven was
417// left with no credentials and nothing said so.
418func TestLoginMaven_RefusesOneLineServerBlock(t *testing.T) {
419 home := t.TempDir()
420 setHome(t, home)
422 fixture := "<settings>\n <servers>\n <server><id>my-alias</id></server>\n </servers>\n</settings>\n"
423 writeSettings(t, home, fixture)
425 err := loginMaven("", "my-alias", "tok1")
426 require.ErrorContains(t, err, "cannot edit")
428 got, readErr := os.ReadFile(settingsPath(home))
429 require.NoError(t, readErr)
430 assert.Equal(t, fixture, string(got), "a refusal must leave the file byte-for-byte intact")
433// TestLoginMaven_RefusesBlockWhoseLastChildSharesTheClosingLine covers the
434// partial-write half of the same hole: <username> is on a line of its own so it
435// was rewritten, then the <password> insert was skipped, leaving a <server>
436// with a username and no password.
437func TestLoginMaven_RefusesBlockWhoseLastChildSharesTheClosingLine(t *testing.T) {
438 home := t.TempDir()
439 setHome(t, home)
441 fixture := "<settings>\n <servers>\n <server>\n <id>my-alias</id>\n <username>foo</username></server>\n </servers>\n</settings>\n"
442 writeSettings(t, home, fixture)
444 err := loginMaven("", "my-alias", "tok1")
445 require.ErrorContains(t, err, "cannot edit")
447 got, readErr := os.ReadFile(settingsPath(home))
448 require.NoError(t, readErr)
449 assert.Equal(t, fixture, string(got), "neither credential may be written when only one can be placed")
450 assert.NotContains(t, string(got), "__token__")
453// TestLoginMaven_SkipsCommentedOutServersSection covers the stock apache-maven
454// settings.xml shape: commented-out examples ahead of the live section.
455// Inserting before the first </servers> would put the block, and the <id> that
456// finds it again, inside the comment, so every later refresh would claim the
457// commented block and the live section would stay empty forever.
458func TestLoginMaven_SkipsCommentedOutServersSection(t *testing.T) {
459 home := t.TempDir()
460 setHome(t, home)
462 fixture := "<settings>\n" +
463 " <!--\n" +
464 " <servers>\n" +
465 " <server><id>example</id></server>\n" +
466 " </servers>\n" +
467 " -->\n" +
468 " <servers>\n" +
469 " </servers>\n" +
470 "</settings>\n"
471 writeSettings(t, home, fixture)
473 require.NoError(t, loginMaven("", "my-alias", "tok1"))
475 got, readErr := os.ReadFile(settingsPath(home))
476 require.NoError(t, readErr)
477 content := string(got)
479 // The block belongs to the live section, which is the one after the comment.
480 _, live, found := strings.Cut(content, "-->")
481 require.True(t, found, "the comment must survive")
482 assert.Contains(t, live, wantServerBlock("my-alias", "tok1"))
483 assert.Contains(t, content, "<server><id>example</id></server>", "the commented example must be untouched")
486// TestLoginMaven_SkipsCommentedOutServerBlockForSameAlias pins that a
487// commented-out block carrying this alias does not absorb the refresh. It used
488// to match, so credentials were rewritten inside the comment and the live
489// section never got a block.
490func TestLoginMaven_SkipsCommentedOutServerBlockForSameAlias(t *testing.T) {
491 home := t.TempDir()
492 setHome(t, home)
494 fixture := "<settings>\n" +
495 " <servers>\n" +
496 " <!--\n" +
497 " <server>\n" +
498 " <id>my-alias</id>\n" +
499 " <username>old</username>\n" +
500 " <password>old-secret</password>\n" +
501 " </server>\n" +
502 " -->\n" +
503 " </servers>\n" +
504 "</settings>\n"
505 writeSettings(t, home, fixture)
507 require.NoError(t, loginMaven("", "my-alias", "tok1"))
509 got, readErr := os.ReadFile(settingsPath(home))
510 require.NoError(t, readErr)
511 content := string(got)
513 assert.Contains(t, content, "<username>old</username>", "the commented block must be untouched")
514 assert.Contains(t, content, "<password>old-secret</password>", "the commented block must be untouched")
515 assert.Contains(t, content, wantServerBlock("my-alias", "tok1"), "a live block must be added")
516 assert.Equal(t, 1, strings.Count(content, "__token__"))
519// TestLoginMaven_CommentedCredentialDoesNotAbsorbTheRefresh covers the worst of
520// the three comment cases, because it reports success while leaving Maven on
521// the expiring token: a commented-out <password> ahead of the live one used to
522// take the replacement.
523func TestLoginMaven_CommentedCredentialDoesNotAbsorbTheRefresh(t *testing.T) {
524 home := t.TempDir()
525 setHome(t, home)
527 fixture := "<settings>\n" +
528 " <servers>\n" +
529 " <server>\n" +
530 " <id>my-alias</id>\n" +
531 " <username>__token__</username>\n" +
532 " <!-- <password>tok0</password> -->\n" +
533 " <password>tok1</password>\n" +
534 " </server>\n" +
535 " </servers>\n" +
536 "</settings>\n"
537 writeSettings(t, home, fixture)
539 require.NoError(t, loginMaven("", "my-alias", "tok2"))
541 got, readErr := os.ReadFile(settingsPath(home))
542 require.NoError(t, readErr)
543 content := string(got)
545 assert.Contains(t, content, "<!-- <password>tok0</password> -->", "the commented credential must be untouched")
546 assert.Contains(t, content, " <password>tok2</password>\n", "the live credential must be the one rewritten")
547 assert.NotContains(t, content, "<password>tok1</password>")
550// TestLoginMaven_AddsSectionWhenServersOnlyExistsInAComment pins that the
551// "cannot edit" refusal keys off a live <servers>. A file whose only <servers>
552// is commented out has no real one, so the </settings> path applies, and the
553// old advice about putting tags on their own lines named tags the user never
554// wrote.
555func TestLoginMaven_AddsSectionWhenServersOnlyExistsInAComment(t *testing.T) {
556 home := t.TempDir()
557 setHome(t, home)
559 fixture := "<settings>\n <!-- <servers></servers> -->\n</settings>\n"
560 writeSettings(t, home, fixture)
562 require.NoError(t, loginMaven("", "my-alias", "tok1"))
564 got, readErr := os.ReadFile(settingsPath(home))
565 require.NoError(t, readErr)
566 content := string(got)
568 assert.Contains(t, content, " <servers>\n")
569 assert.Contains(t, content, wantServerBlock("my-alias", "tok1"))
570 assert.Contains(t, content, "<!-- <servers></servers> -->", "the comment must survive")
573// TestLoginMaven_ExpandsSelfClosingServersOnCRLF pins that a CRLF file's line
574// separator is consumed whole. The pattern used to stop before the \r, leaving
575// a line holding nothing but a carriage return where <servers/> had been.
576func TestLoginMaven_ExpandsSelfClosingServersOnCRLF(t *testing.T) {
577 home := t.TempDir()
578 setHome(t, home)
580 writeSettings(t, home, "<settings>\r\n <servers/>\r\n</settings>\r\n")
582 require.NoError(t, loginMaven("", "my-alias", "tok1"))
584 got, readErr := os.ReadFile(settingsPath(home))
585 require.NoError(t, readErr)
586 content := string(got)
588 assert.Contains(t, content, wantServerBlock("my-alias", "tok1"))
589 assert.NotContains(t, content, "\n\r\n", "the <servers/> line must not be left behind as a line holding only a carriage return")
590 assert.NotContains(t, content, "<servers/>")
593// TestLoginMaven_TreatsEmptyFileAsAbsent covers `touch ~/.m2/settings.xml`: an
594// empty file has no <servers> section and no </settings> tag, so it used to
595// fail with advice about tags that were never there.
596func TestLoginMaven_TreatsEmptyFileAsAbsent(t *testing.T) {
597 home := t.TempDir()
598 setHome(t, home)
600 writeSettings(t, home, "")
602 require.NoError(t, loginMaven("", "my-alias", "tok1"))
604 got, readErr := os.ReadFile(settingsPath(home))
605 require.NoError(t, readErr)
607 want := "<settings>\n" +
608 " <servers>\n" +
609 wantServerBlock("my-alias", "tok1") +
610 " </servers>\n" +
611 "</settings>\n"
612 assert.Equal(t, want, string(got))
615// TestLoginMaven_TreatsWhitespaceOnlyFileAsAbsent is the same case one step
616// less exact, since an interrupted editor session leaves a newline behind.
617func TestLoginMaven_TreatsWhitespaceOnlyFileAsAbsent(t *testing.T) {
618 home := t.TempDir()
619 setHome(t, home)
621 writeSettings(t, home, "\n\n \n")
623 require.NoError(t, loginMaven("", "my-alias", "tok1"))
625 got, readErr := os.ReadFile(settingsPath(home))
626 require.NoError(t, readErr)
627 assert.Contains(t, string(got), wantServerBlock("my-alias", "tok1"))
630// TestLoginMaven_ProseCommentNamingServerKeepsTheLiveBlockVisible covers a
631// comment that names <server> without closing it, which the stock apache-maven
632// settings.xml avoids only because its examples are self-closed. Skipping a
633// match that starts inside a comment was not enough: mavenServerBlockRe started
634// at the commented <server> and ran to the live block's </server>, so skipping
635// it skipped the live block too, and a second <id>my-alias</id> was appended.
636// Maven silently keeps the stale first block in that file.
637func TestLoginMaven_ProseCommentNamingServerKeepsTheLiveBlockVisible(t *testing.T) {
638 home := t.TempDir()
639 setHome(t, home)
641 comment := " <!-- add one <server> entry per registry -->\n"
642 fixture := "<settings>\n" +
643 " <servers>\n" +
644 comment +
645 " <server>\n" +
646 " <id>my-alias</id>\n" +
647 " <username>__token__</username>\n" +
648 " <password>tok0</password>\n" +
649 " </server>\n" +
650 " </servers>\n" +
651 "</settings>\n"
652 writeSettings(t, home, fixture)
654 require.NoError(t, loginMaven("", "my-alias", "tok2"))
656 got, readErr := os.ReadFile(settingsPath(home))
657 require.NoError(t, readErr)
658 content := string(got)
660 assert.Contains(t, content, comment+wantServerBlock("my-alias", "tok2"), "the block after the comment must be the refreshed one")
661 assert.Equal(t, 1, strings.Count(content, "<id>my-alias</id>"), "the live block must be refreshed, not duplicated")
662 assert.NotContains(t, content, "tok0")
665// TestLoginMaven_ProseCommentNamingPasswordDoesNotDuplicateIt is the same hole
666// one element down, and the worse half: the commented <password> swallowed the
667// live one, so the live element looked absent and a second <password> was
668// inserted next to it. Duplicate children make settings.xml non-parseable, so
669// Maven fails on every command until the user edits the file by hand.
670func TestLoginMaven_ProseCommentNamingPasswordDoesNotDuplicateIt(t *testing.T) {
671 home := t.TempDir()
672 setHome(t, home)
674 fixture := "<settings>\n" +
675 " <servers>\n" +
676 " <server>\n" +
677 " <id>my-alias</id>\n" +
678 " <username>__token__</username>\n" +
679 " <!-- put the <password> below -->\n" +
680 " <password>tok0</password>\n" +
681 " </server>\n" +
682 " </servers>\n" +
683 "</settings>\n"
684 writeSettings(t, home, fixture)
686 require.NoError(t, loginMaven("", "my-alias", "tok2"))
688 got, readErr := os.ReadFile(settingsPath(home))
689 require.NoError(t, readErr)
690 content := string(got)
692 assert.Contains(t, content, " <password>tok2</password>\n")
693 assert.Equal(t, 1, strings.Count(content, "</password>"), "a live <password> must be rewritten, not joined by a second one")
694 assert.Contains(t, content, "<!-- put the <password> below -->", "the comment must survive")
697// TestLoginMaven_MatchesAnIDPaddedWithWhitespace pins that <id> is compared the
698// way Maven reads it, which is trimmed. Requiring the exact spelling appended a
699// second block with the same id, and Maven keeps using the stale first one.
700func TestLoginMaven_MatchesAnIDPaddedWithWhitespace(t *testing.T) {
701 home := t.TempDir()
702 setHome(t, home)
704 fixture := "<settings>\n" +
705 " <servers>\n" +
706 " <server>\n" +
707 " <id> my-alias </id>\n" +
708 " <username>__token__</username>\n" +
709 " <password>tok0</password>\n" +
710 " </server>\n" +
711 " </servers>\n" +
712 "</settings>\n"
713 writeSettings(t, home, fixture)
715 require.NoError(t, loginMaven("", "my-alias", "tok2"))
717 got, readErr := os.ReadFile(settingsPath(home))
718 require.NoError(t, readErr)
719 content := string(got)
721 assert.Contains(t, content, " <id> my-alias </id>\n", "the user's spelling of the id must be left as it is")
722 assert.Contains(t, content, " <password>tok2</password>\n")
723 assert.Equal(t, 1, strings.Count(content, "<server>"), "the existing block must be refreshed, not duplicated")
726// TestLoginMaven_ReportsASettingsCloseTagItCannotUse covers a </settings> that
727// shares a line with another element. The error used to say the file had no
728// </settings> tag, which sent the user looking for a tag that is right there.
729func TestLoginMaven_ReportsASettingsCloseTagItCannotUse(t *testing.T) {
730 home := t.TempDir()
731 setHome(t, home)
733 fixture := "<settings>\n <mirrors/></settings>\n"
734 writeSettings(t, home, fixture)
736 err := loginMaven("", "my-alias", "tok1")
737 require.ErrorContains(t, err, "it does not start its own line")
739 got, readErr := os.ReadFile(settingsPath(home))
740 require.NoError(t, readErr)
741 assert.Equal(t, fixture, string(got), "a refusal must leave the file byte-for-byte intact")
744// TestLoginMaven_IndentsPastACommentSharingTheClosingLine covers the cosmetic
745// side of masking comments: the mask blanks a comment ahead of </servers> on the
746// same line, so the captured "indentation" is as wide as the comment unless the
747// line's real leading whitespace is what gets used.
748func TestLoginMaven_IndentsPastACommentSharingTheClosingLine(t *testing.T) {
749 home := t.TempDir()
750 setHome(t, home)
752 writeSettings(t, home, "<settings>\n <servers>\n <!-- one entry per registry --></servers>\n</settings>\n")
754 require.NoError(t, loginMaven("", "my-alias", "tok1"))
756 got, readErr := os.ReadFile(settingsPath(home))
757 require.NoError(t, readErr)
759 want := "<settings>\n" +
760 " <servers>\n" +
761 wantServerBlock("my-alias", "tok1") +
762 " <!-- one entry per registry --></servers>\n" +
763 "</settings>\n"
764 assert.Equal(t, want, string(got))
767func TestLoginMaven_WritesConfigFile(t *testing.T) {
768 home := t.TempDir()
769 setHome(t, home)
771 path := filepath.Join(t.TempDir(), "ci", "settings.xml")
772 require.NoError(t, loginMaven(path, "my-alias", "tok1"))
774 got, err := os.ReadFile(path)
775 require.NoError(t, err)
776 assert.Contains(t, string(got), wantServerBlock("my-alias", "tok1"))
778 _, err = os.Stat(settingsPath(home))
779 assert.ErrorIs(t, err, os.ErrNotExist, "nothing may be written under ~/.m2 when a path is given")
Diagnostics8 git calls · 435 ms
CallStatusSizeTime0435 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=2
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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=2
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=3
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 · hit3 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=3
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=422
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
fetch2005.1 KiB422 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=5229
git.response.cache=MISS
git.response.duration_ms=422
git.fetch.wants=24cb923cbb9bed47e15e04077dcdb15ddf35b8c3
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=5229
git.pkt.payload.max=5202
git.sideband.band1.bytes=5202
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
a5e3026beaa17929
Request
bd84f27f…
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