GitLabgitlab-org

cli

main
ply8 git calls321 ms99 KiB5 cachedprotocol v2
234 lines6.2 KiBRaw
1//go:build !integration
3package list
5import (
6 "errors"
7 "fmt"
8 "strings"
9 "testing"
10 "time"
12 "github.com/acarl005/stripansi"
13 "github.com/google/shlex"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
19 "gitlab.com/gitlab-org/cli/internal/cmdutils"
20 cmdTestUtils "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
23func TestNewCmdReleaseList(t *testing.T) {
24 oldGetRelease := getRelease
25 timer, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
26 getRelease = func(client *gitlab.Client, projectID any, tag string) (*gitlab.Release, error) {
27 if projectID == "" || projectID == "WRONG_REPO" {
28 return nil, fmt.Errorf("error expected")
29 }
30 return &gitlab.Release{
31 TagName: tag,
32 Name: tag,
33 Description: "Dummy description for " + tag,
34 Author: gitlab.BasicUser{
35 ID: 1,
36 Name: "John Dev Wick",
37 Username: "jdwick",
38 },
39 CreatedAt: &timer,
40 }, nil
41 }
43 oldListReleases := listReleases
44 listReleases = func(client *gitlab.Client, projectID any, opts *gitlab.ListReleasesOptions) ([]*gitlab.Release, *gitlab.Response, error) {
45 if projectID == "" || projectID == "WRONG_REPO" {
46 return nil, nil, errors.New("fatal: wrong Repository")
47 }
48 return append([]*gitlab.Release{}, &gitlab.Release{
49 TagName: "0.1.0",
50 Name: "Initial Release",
51 Description: "Dummy description for 0.1.0",
52 Author: gitlab.BasicUser{
53 ID: 1,
54 Name: "John Dev Wick",
55 Username: "jdwick",
56 },
57 CreatedAt: &timer,
58 }), nil, nil
59 }
61 tests := []struct {
62 name string
63 args string
64 stdOutFunc func(t *testing.T, out string)
65 stdErr string
66 wantErr bool
67 }{
68 {
69 name: "releases list on test repo",
70 wantErr: false,
71 stdOutFunc: func(t *testing.T, out string) {
72 t.Helper()
74 assert.Contains(t, out, "Showing 1 release on cli-automated-testing/test")
75 },
76 },
77 {
78 name: "get release by tag on test repo",
79 wantErr: false,
80 args: "--tag v0.0.1-beta",
81 stdOutFunc: func(t *testing.T, out string) {
82 t.Helper()
84 assert.Contains(t, out, "Dummy description for v0.0.1-beta")
85 },
86 },
87 {
88 name: "releases list on custom repo",
89 wantErr: false,
90 args: "-R profclems/glab",
91 stdOutFunc: func(t *testing.T, out string) {
92 t.Helper()
94 assert.Contains(t, out, "Showing 1 release on profclems/glab")
95 },
96 },
97 {
98 name: "ERR - wrong repo",
99 wantErr: true,
100 args: "-R WRONG_REPO",
101 },
102 {
103 name: "ERR - wrong repo with tag",
104 wantErr: true,
105 args: "-R WRONG_REPO --tag v0.0.1-beta",
106 },
107 }
109 io, _, stdout, stderr := cmdTestUtils.TestIOStreams(cmdTestUtils.WithTestIOStreamsAsTTY(true))
110 f := cmdTestUtils.NewTestFactory(io, cmdTestUtils.WithBaseRepo("cli-automated-testing", "test", ""))
112 for _, tt := range tests {
113 t.Run(tt.name, func(t *testing.T) {
114 cmd := NewCmdReleaseList(f)
115 cmdutils.EnableRepoOverride(cmd, f)
117 argv, err := shlex.Split(tt.args)
118 if err != nil {
119 t.Fatal(err)
120 }
121 cmd.SetArgs(argv)
122 _, err = cmd.ExecuteC()
123 if err != nil {
124 if tt.wantErr {
125 require.Error(t, err)
126 return
127 } else {
128 t.Fatal(err)
129 }
130 }
132 out := stripansi.Strip(stdout.String())
133 outErr := stripansi.Strip(stderr.String())
135 tt.stdOutFunc(t, out)
136 assert.Contains(t, outErr, tt.stdErr)
137 })
138 }
140 getRelease = oldGetRelease
141 listReleases = oldListReleases
144func TestReleaseList_HeaderTotal(t *testing.T) {
145 timer, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
146 releases := []*gitlab.Release{{TagName: "v1.0.0", Name: "First Release", CreatedAt: &timer}}
148 tests := []struct {
149 name string
150 args string
151 resp *gitlab.Response
152 wantHeader string
153 }{
154 {
155 name: "shows the total from the API",
156 args: "--per-page 1 --page 3",
157 resp: &gitlab.Response{CurrentPage: 3, TotalPages: 12, TotalItems: 12},
158 wantHeader: "Showing 1 of 12 releases on cli-automated-testing/test.",
159 },
160 {
161 name: "without a total from the API",
162 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2},
163 wantHeader: "Showing 1 release on cli-automated-testing/test.",
164 },
165 }
167 oldListReleases := listReleases
168 defer func() { listReleases = oldListReleases }()
170 for _, tt := range tests {
171 t.Run(tt.name, func(t *testing.T) {
172 listReleases = func(*gitlab.Client, any, *gitlab.ListReleasesOptions) ([]*gitlab.Release, *gitlab.Response, error) {
173 return releases, tt.resp, nil
174 }
176 io, _, stdout, stderr := cmdTestUtils.TestIOStreams(cmdTestUtils.WithTestIOStreamsAsTTY(true))
177 f := cmdTestUtils.NewTestFactory(io, cmdTestUtils.WithBaseRepo("cli-automated-testing", "test", ""))
179 cmd := NewCmdReleaseList(f)
180 cmdutils.EnableRepoOverride(cmd, f)
182 argv, err := shlex.Split(tt.args)
183 require.NoError(t, err)
184 cmd.SetArgs(argv)
185 _, err = cmd.ExecuteC()
186 require.NoError(t, err)
188 out := stripansi.Strip(stdout.String())
189 assert.True(t, strings.HasPrefix(out, tt.wantHeader), "got %q", out)
190 assert.Empty(t, stderr.String())
191 })
192 }
195func TestReleaseList_JSON(t *testing.T) {
196 t.Parallel()
198 oldListReleases := listReleases
199 timer, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
200 listReleases = func(client *gitlab.Client, projectID any, opts *gitlab.ListReleasesOptions) ([]*gitlab.Release, *gitlab.Response, error) {
201 return []*gitlab.Release{
202 {
203 TagName: "v1.0.0",
204 Name: "First Release",
205 Description: "Test release description",
206 Author: gitlab.BasicUser{
207 ID: 1,
208 Name: "John Dev Wick",
209 Username: "jdwick",
210 },
211 CreatedAt: &timer,
212 },
213 }, nil, nil
214 }
215 defer func() { listReleases = oldListReleases }()
217 io, _, stdout, stderr := cmdTestUtils.TestIOStreams()
218 f := cmdTestUtils.NewTestFactory(io, cmdTestUtils.WithBaseRepo("cli-automated-testing", "test", ""))
220 cmd := NewCmdReleaseList(f)
221 cmdutils.EnableRepoOverride(cmd, f)
223 argv, err := shlex.Split("-F json")
224 require.NoError(t, err)
225 cmd.SetArgs(argv)
226 _, err = cmd.ExecuteC()
227 require.NoError(t, err)
229 out := stdout.String()
230 assert.Contains(t, out, `"tag_name":"v1.0.0"`)
231 assert.Contains(t, out, `"name":"First Release"`)
232 assert.Contains(t, out, `"description":"Test release description"`)
233 assert.Empty(t, stderr.String())
Diagnostics8 git calls · 321 ms
CallStatusSizeTime0321 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 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=2
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 · 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
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:02001.8 KiB · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=6
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:0200350 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=350
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=b2a837e646ba305993f35203fafafb109e57a7ca
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=350
git.pkt.payload.max=323
git.sideband.band1.bytes=323
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=323
git.pack.trailer=1232131042f2ba4c9c4dbd99382667a1f0849e15
Open pack map
fetchtree:0200142 B · 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=142
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c961dd4b66b0ecad8dfb3fe25c3e67c349274d62
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=142
git.pkt.payload.max=115
git.sideband.band1.bytes=115
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=115
git.pack.trailer=d9c0e09d50b35c9f3e658a20b69f00ed7ba0a6fe
Open pack map
fetch2001.7 KiB303 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=303
git.response.bytes=1750
git.response.cache=MISS
git.response.duration_ms=303
git.fetch.wants=6dc14969c01f6171f87c68adc60bf60b4745ca29
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=1750
git.pkt.payload.max=1723
git.sideband.band1.bytes=1723
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
a5e2fe280a70f3c9
Request
b5c3692d…
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