GitLabgitlab-org

cli

main
ply7 git calls229 ms98 KiB5 cachedprotocol v2
355 lines9.0 KiBRaw
1package cmdtest
3import (
4 "bytes"
5 io "io"
6 "runtime"
7 "testing"
9 tea "charm.land/bubbletea/v2"
10 "git.sr.ht/~timofurrer/ugh"
11 "github.com/google/shlex"
12 "github.com/spf13/cobra"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/config"
19 "gitlab.com/gitlab-org/cli/internal/git"
20 "gitlab.com/gitlab-org/cli/internal/glinstance"
21 "gitlab.com/gitlab-org/cli/internal/glrepo"
22 "gitlab.com/gitlab-org/cli/internal/iostreams"
23 "gitlab.com/gitlab-org/cli/test"
26type CmdExecFunc func(cli string) (*test.CmdOut, error)
28type CmdFunc func(cmdutils.Factory) *cobra.Command
30// FactoryOption is a function that configures a Factory
31type FactoryOption func(f *Factory)
33type Factory struct {
34 ApiClientStub func(repoHost string) (*api.Client, error)
35 GitLabClientStub func() (*gitlab.Client, error)
36 BaseRepoStub func() (glrepo.Interface, error)
37 RemotesStub func() (glrepo.Remotes, error)
38 ConfigStub func() config.Config
39 BranchStub func() (string, error)
40 IOStub *iostreams.IOStreams
41 BuildInfoStub api.BuildInfo
42 ExecutorStub cmdutils.Executor
43 GitRunnerStub git.GitRunner
45 // DefaultHostnameStub is what the real factory resolves from GITLAB_HOST
46 // and the config before any command runs.
47 DefaultHostnameStub string
49 repoOverride string
51 // captured standard ios for assertion purposes
52 stdin safeBuffer
53 stdout bytes.Buffer
54 stderr bytes.Buffer
56 // functions to run before and after exec a command
57 // NOTE: this is only supported via SetupCmdForTest
58 execSetup []func()
59 execCleanup []func()
62// NewTestFactory creates a Factory configured for testing with the given options
63func NewTestFactory(ios *iostreams.IOStreams, opts ...FactoryOption) *Factory {
64 f := &Factory{
65 IOStub: ios,
66 ApiClientStub: func(repoHost string) (*api.Client, error) {
67 return &api.Client{}, nil
68 },
69 GitLabClientStub: func() (*gitlab.Client, error) {
70 return &gitlab.Client{}, nil
71 },
72 ConfigStub: config.NewBlankConfig,
73 BaseRepoStub: func() (glrepo.Interface, error) {
74 return glrepo.New("OWNER", "REPO", glinstance.DefaultHostname), nil
75 },
76 BranchStub: func() (string, error) {
77 return "main", nil
78 },
79 BuildInfoStub: api.BuildInfo{Version: "test", Commit: "test", Platform: runtime.GOOS, Architecture: runtime.GOARCH},
80 DefaultHostnameStub: glinstance.DefaultHostname,
81 GitRunnerStub: git.StandardGitCommand{},
82 }
84 // Apply all options
85 for _, opt := range opts {
86 opt(f)
87 }
89 // Fall back to a fully-initialized stub IOStreams when callers (or test
90 // helpers like the ones that exercise only the command tree) didn't
91 // provide one. This means production code never has to defend against
92 // a nil Factory.IO() return value.
93 if f.IOStub == nil {
94 f.IOStub, _, _, _ = TestIOStreams()
95 }
97 // Create multi writers to assert outputs
98 f.IOStub.In = newTeeReadCloser(f.IOStub.In, &f.stdin)
99 f.IOStub.StdOut = io.MultiWriter(&f.stdout, f.IOStub.StdOut)
100 f.IOStub.StdErr = io.MultiWriter(&f.stderr, f.IOStub.StdErr)
102 // run setup functions (may have been registered by FactoryOption functions)
103 for _, sf := range f.execSetup {
104 sf()
105 }
107 return f
110// SetupCmdForTest creates a test environment with a configured Factory
111func SetupCmdForTest(t *testing.T, cmdFunc CmdFunc, isTTY bool, opts ...FactoryOption) CmdExecFunc {
112 t.Helper()
114 ios, _, _, _ := TestIOStreams(WithTestIOStreamsAsTTY(isTTY))
116 f := NewTestFactory(ios, opts...)
117 return func(cli string) (*test.CmdOut, error) {
118 defer f.cleanup()
120 argv, err := shlex.Split(cli)
121 if err != nil {
122 return nil, err
123 }
125 cmd := cmdFunc(f)
126 cmd.SetArgs(argv)
128 _, err = cmd.ExecuteC()
129 return &test.CmdOut{
130 OutBuf: &f.stdout,
131 ErrBuf: &f.stderr,
132 }, err
133 }
136func (f *Factory) cleanup() {
137 for _, cf := range f.execCleanup {
138 cf()
139 }
142func (f *Factory) RepoOverride(repo string) error {
143 if repo != "" {
144 if _, err := glrepo.FromFullName(repo, glinstance.DefaultHostname, f.Config()); err != nil {
145 return err
146 }
147 }
148 f.repoOverride = repo
149 return nil
152func (f *Factory) ApiClient(repoHost string) (*api.Client, error) {
153 return f.ApiClientStub(repoHost)
156func (f *Factory) GitLabClient() (*gitlab.Client, error) {
157 return f.GitLabClientStub()
160func (f *Factory) BaseRepo() (glrepo.Interface, error) {
161 if f.repoOverride != "" {
162 return glrepo.FromFullName(f.repoOverride, glinstance.DefaultHostname, f.Config())
163 }
164 return f.BaseRepoStub()
167func (f *Factory) Remotes() (glrepo.Remotes, error) {
168 return f.RemotesStub()
171func (f *Factory) Config() config.Config {
172 return f.ConfigStub()
175func (f *Factory) Branch() (string, error) {
176 return f.BranchStub()
179func (f *Factory) IO() *iostreams.IOStreams {
180 return f.IOStub
183func (f *Factory) DefaultHostname() string {
184 return f.DefaultHostnameStub
187func (f *Factory) BuildInfo() api.BuildInfo {
188 return f.BuildInfoStub
191func (f *Factory) Executor() cmdutils.Executor {
192 return f.ExecutorStub
195func (f *Factory) GitRunner() git.GitRunner {
196 return f.GitRunnerStub
199// WithApiClient configures the Factory with a specific API client
200func WithApiClient(client *api.Client) FactoryOption {
201 return func(f *Factory) {
202 f.ApiClientStub = func(repoHost string) (*api.Client, error) {
203 return client, nil
204 }
205 }
208// WithApiClientFunc configures the Factory to build API clients with the given function
209func WithApiClientFunc(apiClient func(repoHost string) (*api.Client, error)) FactoryOption {
210 return func(f *Factory) {
211 f.ApiClientStub = apiClient
212 }
215// WithGitLabClient configures the Factory with a specific GitLab client
216func WithGitLabClient(client *gitlab.Client) FactoryOption {
217 return func(f *Factory) {
218 f.GitLabClientStub = func() (*gitlab.Client, error) {
219 return client, nil
220 }
221 }
224// WithConfig configures the Factory with a specific config
225func WithConfig(cfg config.Config) FactoryOption {
226 return func(f *Factory) {
227 f.ConfigStub = func() config.Config {
228 return cfg
229 }
230 }
233// WithGitLabClientError configures the Factory to return an error when creating GitLab client
234func WithGitLabClientError(err error) FactoryOption {
235 return func(f *Factory) {
236 f.GitLabClientStub = func() (*gitlab.Client, error) {
237 return nil, err
238 }
239 }
242// WithBaseRepoError configures the Factory to return an error when getting base repo
243func WithBaseRepoError(err error) FactoryOption {
244 return func(f *Factory) {
245 f.BaseRepoStub = func() (glrepo.Interface, error) {
246 return nil, err
247 }
248 }
251// WithBranchError configures the Factory to return an error when getting branch
252func WithBranchError(err error) FactoryOption {
253 return func(f *Factory) {
254 f.BranchStub = func() (string, error) {
255 return "", err
256 }
257 }
260// WithBaseRepo configures the Factory with a specific base repository
261func WithBaseRepo(owner, repo string, hostname string) FactoryOption {
262 return func(f *Factory) {
263 f.BaseRepoStub = func() (glrepo.Interface, error) {
264 repoHostname := glinstance.DefaultHostname
265 if hostname != "" {
266 repoHostname = hostname
267 }
268 return glrepo.New(owner, repo, repoHostname), nil
269 }
270 }
273// WithDefaultHostname configures the Factory with a specific default hostname
274func WithDefaultHostname(hostname string) FactoryOption {
275 return func(f *Factory) {
276 f.DefaultHostnameStub = hostname
277 }
280// WithBranch configures the Factory with a specific branch
281func WithBranch(branch string) FactoryOption {
282 return func(f *Factory) {
283 f.BranchStub = func() (string, error) {
284 return branch, nil
285 }
286 }
289// WithBuildInfo configures the Factory build information
290func WithBuildInfo(buildInfo api.BuildInfo) FactoryOption {
291 return func(f *Factory) {
292 f.BuildInfoStub = buildInfo
293 }
296// WithStdin configures the Factory with specific stdin content
297func WithStdin(stdin string) FactoryOption {
298 return func(f *Factory) {
299 f.IOStub.In = io.NopCloser(bytes.NewBufferString(stdin))
300 }
303// WithIOStreamsOverride configures an IOStreams instance for testing
304//
305// Attention: only use as a last resort and prefer functions like WithStdin etc to configure the IOStreams that's already created.
306func WithIOStreamsOverride(ios *iostreams.IOStreams) FactoryOption {
307 return func(f *Factory) {
308 f.IOStub = ios
309 }
312func WithExecutor(exec cmdutils.Executor) FactoryOption {
313 return func(f *Factory) {
314 f.ExecutorStub = exec
315 }
318func WithGitRunner(gr git.GitRunner) FactoryOption {
319 return func(f *Factory) {
320 f.GitRunnerStub = gr
321 }
324const (
325 ConsoleWidth = 120
326 ConsoleHeight = 40
329func WithConsole(t *testing.T, console *ugh.Console) FactoryOption {
330 t.Helper()
332 return func(f *Factory) {
333 appInR, appInW := io.Pipe()
334 appOutR, appOutW := io.Pipe()
336 f.IOStub = iostreams.New(
337 iostreams.WithStdin(appInR, true),
338 iostreams.WithStdout(appOutW, true),
339 iostreams.WithStderr(f.IOStub.StdErr, f.IOStub.IsErrTTY),
340 iostreams.WithProgramOptions(tea.WithWindowSize(ConsoleWidth, ConsoleHeight)),
341 )
343 startConsole := func() {
344 wait := console.Start(t.Context(), appOutR, appInW)
346 f.execCleanup = append(f.execCleanup, func() {
347 appInW.Close()
348 appOutR.Close()
349 wait()
350 })
351 }
353 f.execSetup = append(f.execSetup, startConsole)
354 }
Diagnostics7 git calls · 229 ms
CallStatusSizeTime0229 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 KiB · hit3 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=HIT
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273
git.response.cache=HIT
git.response.duration_ms=3
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 · 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
fetchtree:0200233 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=233
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=be3b2f46264fd78e3aeec243c47ee7f44f4b940d
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=233
git.pkt.payload.max=206
git.sideband.band1.bytes=206
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=206
git.pack.trailer=60d385382f0daa4bf5b182241b9c3f6f6404da58
Open pack map
fetchtree:0200425 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=425
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=a60e63d01319c0073dd508088207ea8a30fdb705
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=425
git.pkt.payload.max=398
git.sideband.band1.bytes=398
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=398
git.pack.trailer=a85ea41c4277ec613536d00d6cd4a6b2898770d0
Open pack map
fetch2002.6 KiB219 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=219
git.response.bytes=2682
git.response.cache=MISS
git.response.duration_ms=219
git.fetch.wants=36058b50e6c9fa7672b2fbe84a6618ddbc2968a4
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=2682
git.pkt.payload.max=2655
git.sideband.band1.bytes=2655
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
a5e135292cff18aa
Request
f2e33b1a…
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