GitLabgitlab-org
cli
main
1//go:build !integration
3package cmdutils
5import (
6 "bytes"
7 "io"
8 "sync"
9 "testing"
11 "github.com/google/shlex"
12 "github.com/spf13/cobra"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 "gitlab.com/gitlab-org/cli/internal/api"
19 "gitlab.com/gitlab-org/cli/internal/config"
20 "gitlab.com/gitlab-org/cli/internal/git"
21 "gitlab.com/gitlab-org/cli/internal/glinstance"
22 "gitlab.com/gitlab-org/cli/internal/glrepo"
23 "gitlab.com/gitlab-org/cli/internal/iostreams"
24)
26type options struct {
27 group string
28 baseRepo func() (glrepo.Interface, error)
29}
31type dummyFactory struct {
32 baseRepo glrepo.Interface
33}
35func (f *dummyFactory) RepoOverride(repo string) error {
36 baseRepo, err := glrepo.FromFullName(repo, glinstance.DefaultHostname, config.NewBlankConfig())
37 if err != nil {
38 return err
39 }
40 f.baseRepo = baseRepo
41 return nil
42}
44func (f *dummyFactory) ApiClient(repoHost string) (*api.Client, error) {
45 return nil, nil
46}
48func (f *dummyFactory) GitLabClient() (*gitlab.Client, error) { return nil, nil }
50func (f *dummyFactory) BaseRepo() (glrepo.Interface, error) {
51 if f.baseRepo != nil {
52 return f.baseRepo, nil
53 }
55 return glrepo.New("OWNER", "REPO", glinstance.DefaultHostname), nil
56}
58func (f *dummyFactory) Remotes() (glrepo.Remotes, error) { return nil, nil }
60func (f *dummyFactory) Config() config.Config { return config.NewBlankConfig() }
62func (f *dummyFactory) Branch() (string, error) { return "", nil }
64func (f *dummyFactory) IO() *iostreams.IOStreams { return nil }
66func (f *dummyFactory) DefaultHostname() string { return "" }
68func (f *dummyFactory) BuildInfo() api.BuildInfo { return api.BuildInfo{} }
70func (f *dummyFactory) Executor() Executor { return nil }
72func (f *dummyFactory) GitRunner() git.GitRunner { return git.StandardGitCommand{} }
74func NewDummyCmd(f Factory, runE func(opts *options) error) *cobra.Command {
75 opts := &options{
76 baseRepo: f.BaseRepo,
77 }
79 cmd := &cobra.Command{
80 Use: "list [flags]",
81 Short: "List objects",
82 Long: "List objects",
83 Args: cobra.NoArgs,
84 RunE: func(cmd *cobra.Command, args []string) error {
85 if err := opts.complete(cmd); err != nil {
86 return err
87 }
89 if runE != nil {
90 return runE(opts)
91 }
93 return nil
94 },
95 }
96 EnableRepoOverride(cmd, f)
97 cmd.PersistentFlags().StringP("group", "g", "", "Select another group or it's subgroups")
99 return cmd
100}
102func (o *options) complete(cmd *cobra.Command) error {
103 group, err := GroupOverride(cmd)
104 if err != nil {
105 return err
106 }
107 o.group = group
109 return nil
110}
112func runCommand(cli string, runE func(opts *options) error) error {
113 cmd := NewDummyCmd(&dummyFactory{}, runE)
115 argv, err := shlex.Split(cli)
116 if err != nil {
117 return err
118 }
120 cmd.SetArgs(argv)
121 cmd.SetIn(&bytes.Buffer{})
122 cmd.SetOut(io.Discard)
123 cmd.SetErr(io.Discard)
125 _, err = cmd.ExecuteC()
126 return err
127}
129func TestGroupOverride(t *testing.T) {
130 // set GITLAB_GROUP environment variable
131 t.Setenv("GITLAB_GROUP", "GROUP/NAME")
133 t.Run("List command with GITLAB_GROUP env var and group", func(t *testing.T) {
134 gotOpts := &options{}
136 err := runCommand("--group GROUP/OVERRIDE", func(opts *options) error {
137 gotOpts = opts
138 return nil
139 })
141 require.NoError(t, err)
142 gotGroup := gotOpts.group
143 // make sure the GITLAB_GROUP env variable is not used
144 assert.NotEqual(t, "GROUP/NAME", gotGroup)
145 // make sure the group flag was used instead
146 assert.Equal(t, "GROUP/OVERRIDE", gotGroup)
147 })
149 t.Run("List command with GITLAB_GROUP env var and no group flag", func(t *testing.T) {
150 gotOpts := &options{}
152 err := runCommand("", func(opts *options) error {
153 gotOpts = opts
154 return nil
155 })
157 require.NoError(t, err)
158 gotGroup := gotOpts.group
159 // make sure the GITLAB_GROUP env variable is used
160 assert.Equal(t, "GROUP/NAME", gotGroup)
161 })
163 t.Run("List command with GITLAB_GROUP env var and repo flag", func(t *testing.T) {
164 gotOpts := &options{}
166 err := runCommand("--repo OWNER2/REPO2", func(opts *options) error {
167 gotOpts = opts
168 return nil
169 })
171 require.NoError(t, err)
172 gotGroup := gotOpts.group
173 // make sure the GITLAB_GROUP env variable is not used
174 assert.Empty(t, gotGroup)
175 // make sure the repo option is used instead
176 gotRepo, _ := gotOpts.baseRepo()
177 assert.Equal(t, "OWNER2/REPO2", gotRepo.FullName())
178 })
180 t.Run("List command with GITLAB_GROUP env var and base repo but no repo flag", func(t *testing.T) {
181 gotOpts := &options{}
183 err := runCommand("", func(opts *options) error {
184 gotOpts = opts
185 return nil
186 })
188 require.NoError(t, err)
189 gotGroup := gotOpts.group
190 // make sure the GITLAB_GROUP env variable is used
191 assert.Equal(t, "GROUP/NAME", gotGroup)
192 // make sure the default baserepo is still used
193 gotRepo, _ := gotOpts.baseRepo()
194 assert.Equal(t, "OWNER/REPO", gotRepo.FullName())
195 })
197 t.Run("concurrent calls do not race", func(t *testing.T) {
198 var wg sync.WaitGroup
199 const n = 50
200 wg.Add(n)
202 for range n {
203 go func() {
204 defer wg.Done()
205 cmd := NewDummyCmd(&dummyFactory{}, nil)
206 _ = cmd.ParseFlags([]string{})
207 group, err := GroupOverride(cmd)
208 assert.NoError(t, err)
209 assert.Equal(t, "GROUP/NAME", group)
210 }()
211 }
213 wg.Wait()
214 })
216 t.Run("List command with GITLAB_GROUP env var, repo and groups flags", func(t *testing.T) {
217 gotOpts := &options{}
219 err := runCommand("--repo OWNER2/REPO2 --group GROUP/OVERRIDE", func(opts *options) error {
220 gotOpts = opts
221 return nil
222 })
224 require.NoError(t, err)
225 gotGroup := gotOpts.group
226 // make sure no group option is set at all (group settings ignored)
227 assert.Empty(t, gotGroup)
228 // make sure the repo flag is used instead
229 gotRepo, _ := gotOpts.baseRepo()
230 assert.Equal(t, "OWNER2/REPO2", gotRepo.FullName())
231 })
232}
Diagnostics6 git calls · 27 ms
CallStatusSizeTime027 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 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=3 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
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=3 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=44Open pack map
fetchtree:02001.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=18 git.response.bytes=1238 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17 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=1238 git.pkt.payload.max=1211 git.sideband.band1.bytes=1211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1211 git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetch2001.6 KiB · hit18 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1616 git.response.cache=HIT git.response.duration_ms=18 git.fetch.wants=f48394b8e1c5cfcd66a44366a57adf0640454e1d 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=1616 git.pkt.payload.max=1589 git.sideband.band1.bytes=1589 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
- a5e186e8ddf85e37
- Request
- 3f060277…
- 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