GitLabgitlab-org
cli
main
1//go:build !integration
3package infer
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
11 "gitlab.com/gitlab-org/cli/internal/commands/stack/stackutils"
12)
14func TestParseCommitSelection(t *testing.T) {
15 tests := []struct {
16 name string
17 input string
18 expected []string
19 wantErr bool
20 }{
21 {
22 name: "single commit",
23 input: "abc123\n",
24 expected: []string{"abc123"},
25 wantErr: false,
26 },
27 {
28 name: "multiple commits",
29 input: "abc123\ndef456\nghi789\n",
30 expected: []string{"abc123", "def456", "ghi789"},
31 wantErr: false,
32 },
33 {
34 name: "commits with comments",
35 input: "abc123 # Fix critical bug\ndef456 # Add new feature\n",
36 expected: []string{"abc123", "def456"},
37 wantErr: false,
38 },
39 {
40 name: "ignore comment lines",
41 input: "# This is a comment\nabc123\n# Another comment\ndef456\n",
42 expected: []string{"abc123", "def456"},
43 wantErr: false,
44 },
45 {
46 name: "ignore empty lines",
47 input: "abc123\n\ndef456\n\n\nghi789\n",
48 expected: []string{"abc123", "def456", "ghi789"},
49 wantErr: false,
50 },
51 {
52 name: "mixed comments and empty lines",
53 input: "# Choose commits for your stack\nabc123 # First commit\n\n# Second commit below\ndef456\n# End of selection\n",
54 expected: []string{"abc123", "def456"},
55 wantErr: false,
56 },
57 {
58 name: "commits with whitespace",
59 input: " abc123 \n def456 # with comment \n",
60 expected: []string{"abc123", "def456"},
61 wantErr: false,
62 },
63 {
64 name: "invalid format - extra content without comment",
65 input: "abc123 extra content without hash\n",
66 expected: nil,
67 wantErr: true,
68 },
69 {
70 name: "invalid format - multiple words without comment",
71 input: "abc123 def456 ghi789\n",
72 expected: nil,
73 wantErr: true,
74 },
75 {
76 name: "empty input",
77 input: "",
78 expected: []string{},
79 wantErr: false,
80 },
81 {
82 name: "only comments",
83 input: "# Comment 1\n# Comment 2\n# Comment 3\n",
84 expected: []string{},
85 wantErr: false,
86 },
87 {
88 name: "only empty lines",
89 input: "\n\n\n",
90 expected: []string{},
91 wantErr: false,
92 },
93 }
95 for _, tt := range tests {
96 t.Run(tt.name, func(t *testing.T) {
97 result, err := parseCommitSelection(tt.input)
99 if tt.wantErr {
100 require.Error(t, err)
101 assert.Contains(t, err.Error(), "improperly formatted reorder file")
102 } else {
103 require.NoError(t, err)
104 assert.Equal(t, tt.expected, result)
105 }
106 })
107 }
108}
110func TestHasComment(t *testing.T) {
111 tests := []struct {
112 name string
113 words []string
114 expected bool
115 }{
116 {
117 name: "has comment",
118 words: []string{"abc123", "# This is a comment"},
119 expected: true,
120 },
121 {
122 name: "has comment with multiple words",
123 words: []string{"abc123", "# Fix", "the", "bug"},
124 expected: true,
125 },
126 {
127 name: "no comment",
128 words: []string{"abc123", "def456"},
129 expected: false,
130 },
131 {
132 name: "single word",
133 words: []string{"abc123"},
134 expected: false,
135 },
136 {
137 name: "empty slice",
138 words: []string{},
139 expected: false,
140 },
141 {
142 name: "comment not at second position",
143 words: []string{"abc123", "def456", "# comment"},
144 expected: false,
145 },
146 {
147 name: "hash without space",
148 words: []string{"abc123", "#comment"},
149 expected: true,
150 },
151 }
153 for _, tt := range tests {
154 t.Run(tt.name, func(t *testing.T) {
155 result := stackutils.HasComment(tt.words)
156 assert.Equal(t, tt.expected, result)
157 })
158 }
159}
161func TestParseCommitSelectionErrorMessages(t *testing.T) {
162 tests := []struct {
163 name string
164 input string
165 expectedErr string
166 }{
167 {
168 name: "error contains line content",
169 input: "abc123 invalid content here\n",
170 expectedErr: "abc123 invalid content here",
171 },
172 {
173 name: "error with multiple invalid lines - first one reported",
174 input: "abc123 invalid\ndef456 also invalid\n",
175 expectedErr: "abc123 invalid",
176 },
177 }
179 for _, tt := range tests {
180 t.Run(tt.name, func(t *testing.T) {
181 _, err := parseCommitSelection(tt.input)
182 require.Error(t, err)
183 assert.Contains(t, err.Error(), tt.expectedErr)
184 })
185 }
186}
Diagnostics8 git calls · 523 ms
CallStatusSizeTime0523 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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 KiB156 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=156 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=156 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=4 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=44Open 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200432 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=432 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=ff2d1e7abc74964e5679ebdb51f1ac0d0dc3aff0 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=432 git.pkt.payload.max=405 git.sideband.band1.bytes=405 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=405 git.pack.trailer=8f3029a269217d94d12d0671e0490fe3d5506303Open pack map
fetchtree:0200199 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=199 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=8109696a12928135171f031112e487158b02c470 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=199 git.pkt.payload.max=172 git.sideband.band1.bytes=172 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=172 git.pack.trailer=7619d11a8f208890971c50dea8f51b0e072e21b9Open pack map
fetch2001.1 KiB355 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=354 git.response.bytes=1097 git.response.cache=MISS git.response.duration_ms=355 git.fetch.wants=3d87e3ff504ba33413dd65c9c0becb52a907ba5f 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=1097 git.pkt.payload.max=1070 git.sideband.band1.bytes=1070 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
- a5e19ccf987d3cd0
- Request
- 5b475506…
- 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