GitLabgitlab-org
cli
main
1//go:build !integration
3package orbit
5import (
6 "context"
7 "errors"
8 "os"
9 "path/filepath"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "github.com/zalando/go-keyring"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/binarymgr"
18 "gitlab.com/gitlab-org/cli/internal/binarymgr/binaries"
19 "gitlab.com/gitlab-org/cli/internal/config"
20 "gitlab.com/gitlab-org/cli/internal/iostreams"
21 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22)
24func TestNewCmd_PassthroughStructure(t *testing.T) {
25 t.Parallel()
26 ios, _, _, _ := cmdtest.TestIOStreams()
27 f := cmdtest.NewTestFactory(ios)
29 cmd := NewCmd(f)
31 assert.Equal(t, "orbit", cmd.Name())
32 assert.True(t, cmd.DisableFlagParsing)
33 assert.Empty(t, cmd.Annotations["mcp:safe"])
34 assert.NotNil(t, cmd.Flags().Lookup("install"))
35 assert.NotNil(t, cmd.Flags().Lookup("update"))
36 yesFlag := cmd.Flags().Lookup("yes")
37 require.NotNil(t, yesFlag)
38 assert.Equal(t, "y", yesFlag.Shorthand)
39}
41func TestValidate_InstallUpdateMutuallyExclusive(t *testing.T) {
42 t.Parallel()
43 opts := &options{flags: binarymgr.SplitFlags([]string{"--install", "--update"})}
45 err := opts.validate()
46 require.Error(t, err)
47 assert.Contains(t, err.Error(), "mutually exclusive")
48}
50func TestValidate_InstallWithCommandErrors(t *testing.T) {
51 t.Parallel()
52 opts := &options{flags: binarymgr.SplitFlags([]string{"--update", "status"})}
54 err := opts.validate()
55 require.Error(t, err)
56 assert.Contains(t, err.Error(), "cannot be combined with a command")
57}
59func TestNewCmd_HelpShowsInstallHintWhenBinaryIsMissing(t *testing.T) {
60 t.Setenv("GLAB_ORBIT_CLI_BINARY_PATH", "")
61 t.Setenv("GLAB_CONFIG_DIR", t.TempDir())
62 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, cmdtest.WithConfig(config.NewBlankConfig()))
64 for _, args := range []string{"", "--help", "-h status"} {
65 out, err := exec(args)
66 require.NoError(t, err, args)
67 assert.Contains(t, out.String(), "The GitLab Orbit CLI is not installed.", args)
68 assert.Contains(t, out.String(), "glab orbit --install --yes", args)
69 }
70}
72func TestRun_HelpExecsTheInstalledBinaryWithoutRunLifecycle(t *testing.T) {
73 binary := filepath.Join(t.TempDir(), "orbit")
74 require.NoError(t, os.WriteFile(binary, []byte("#!/bin/sh\n"), 0o755))
75 t.Setenv("GLAB_ORBIT_CLI_BINARY_PATH", binary)
76 t.Setenv("GITLAB_TOKEN", "glpat-remote")
77 client, err := api.NewClientFromConfig("gitlab.com", config.NewBlankConfig(), false, "test-agent")
78 require.NoError(t, err)
79 ios, _, stdout, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(true))
80 f := cmdtest.NewTestFactory(ios, cmdtest.WithConfig(config.NewBlankConfig()), cmdtest.WithApiClient(client))
82 for _, args := range [][]string{{}, {"--help"}, {"-h", "status"}, {"--yes", "--help"}} {
83 var forwarded []string
84 opts := &options{
85 io: ios,
86 cfg: f.Config(),
87 factory: f,
88 flags: binarymgr.SplitFlags(args),
89 execute: func(_ context.Context, _ *iostreams.IOStreams, path string, execArgs, env []string) error {
90 assert.Equal(t, binary, path, "%v", args)
91 assert.Contains(t, env, "ORBIT_AUTH_HEADER_VALUE=glpat-remote", "%v", args)
92 forwarded = execArgs
93 return nil
94 },
95 }
97 require.NoError(t, opts.run(t.Context()), "%v", args)
98 assert.Equal(t, opts.flags.Forwarded, forwarded, "%v", args)
99 }
100 assert.Empty(t, stdout.String())
101}
103func TestNewCmd_HelpNamesBrokenCustomBinaryPath(t *testing.T) {
104 missing := filepath.Join(t.TempDir(), "missing-orbit")
105 t.Setenv("GLAB_ORBIT_CLI_BINARY_PATH", missing)
106 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, cmdtest.WithConfig(config.NewBlankConfig()))
108 out, err := exec("")
109 require.NoError(t, err)
110 assert.Contains(t, out.String(), missing)
111 assert.Contains(t, out.String(), "GLAB_ORBIT_CLI_BINARY_PATH")
112 assert.Contains(t, out.String(), "orbit_cli_binary_path")
113 assert.NotContains(t, out.String(), "is not installed")
114}
116func TestOrbitCredentialEnv_InjectsResolvedCredential(t *testing.T) {
117 t.Setenv("GITLAB_TOKEN", "glpat-remote")
119 client, err := api.NewClientFromConfig("gitlab.com", config.NewBlankConfig(), false, "test-agent")
120 require.NoError(t, err)
122 f := cmdtest.NewTestFactory(nil, cmdtest.WithApiClient(client))
124 env := orbitCredentialEnv(t.Context(), f)
125 assert.Contains(t, env, "ORBIT_API_BASE_URL=https://gitlab.com")
126 assert.Contains(t, env, "ORBIT_AUTH_HEADER_NAME=Private-Token")
127 assert.Contains(t, env, "ORBIT_AUTH_HEADER_VALUE=glpat-remote")
128}
130func TestOrbitCredentialEnv_SkipsWhenUnauthenticated(t *testing.T) {
131 t.Setenv("GITLAB_TOKEN", "")
132 keyring.MockInit()
134 client, err := api.NewClientFromConfig("gitlab.com", config.NewBlankConfig(), false, "test-agent")
135 require.NoError(t, err)
137 ios, _, _, stderr := cmdtest.TestIOStreams()
138 f := cmdtest.NewTestFactory(ios, cmdtest.WithApiClient(client))
140 assert.Nil(t, orbitCredentialEnv(t.Context(), f))
141 assert.Empty(t, stderr.String())
142}
144func TestRunWithCustomPath_Validation(t *testing.T) {
145 if _, err := binarymgr.ManagedBinaryPath(binaries.Orbit()); errors.Is(err, binarymgr.ErrUnsupportedPlatform) {
146 t.Skipf("skipping on unsupported platform: %v", err)
147 }
149 t.Run("non-existent path returns clear error", func(t *testing.T) {
150 ios, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
151 factory := cmdtest.NewTestFactory(ios)
153 t.Setenv("GLAB_ORBIT_CLI_BINARY_PATH", "/nonexistent/path/to/orbit")
154 runner := binarymgr.NewRunner(factory.IO(), factory.Config(), binaries.Orbit())
155 err := runner.Run(t.Context())
157 require.Error(t, err)
158 assert.Contains(t, err.Error(), "GLAB_ORBIT_CLI_BINARY_PATH")
159 assert.Contains(t, err.Error(), "orbit_cli_binary_path")
160 assert.Contains(t, err.Error(), "/nonexistent/path/to/orbit")
161 assert.Contains(t, err.Error(), "was not found")
162 })
164 t.Run("non-executable file returns clear error", func(t *testing.T) {
165 ios, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
166 factory := cmdtest.NewTestFactory(ios)
168 dir := t.TempDir()
169 nonExecFile := filepath.Join(dir, "orbit")
170 require.NoError(t, os.WriteFile(nonExecFile, []byte("#!/bin/sh\n"), 0o644))
172 t.Setenv("GLAB_ORBIT_CLI_BINARY_PATH", nonExecFile)
173 runner := binarymgr.NewRunner(factory.IO(), factory.Config(), binaries.Orbit())
174 err := runner.Run(t.Context())
176 require.Error(t, err)
177 assert.Contains(t, err.Error(), "GLAB_ORBIT_CLI_BINARY_PATH")
178 assert.Contains(t, err.Error(), "is not executable")
179 })
180}
182func TestHandleInstall_CustomPath(t *testing.T) {
183 if _, err := binarymgr.ManagedBinaryPath(binaries.Orbit()); errors.Is(err, binarymgr.ErrUnsupportedPlatform) {
184 t.Skipf("skipping on unsupported platform: %v", err)
185 }
187 ios, _, stderr, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
188 factory := cmdtest.NewTestFactory(ios)
190 dir := t.TempDir()
191 execFile := filepath.Join(dir, "orbit")
192 require.NoError(t, os.WriteFile(execFile, []byte("#!/bin/sh\n"), 0o755))
194 t.Setenv("GLAB_ORBIT_CLI_BINARY_PATH", execFile)
195 runner := binarymgr.NewRunner(factory.IO(), factory.Config(), binaries.Orbit())
196 err := runner.HandleInstall(t.Context())
198 require.NoError(t, err)
199 assert.Contains(t, stderr.String(), "Using custom GitLab Orbit CLI binary:")
200 assert.Contains(t, stderr.String(), execFile)
201}
203func TestNewCmd_UpdateRouting(t *testing.T) {
204 t.Parallel()
206 tests := []struct {
207 name string
208 args []string
209 want string
210 }{
211 {name: "update is glab's subcommand", args: []string{"update"}, want: "update"},
212 {name: "update after --yes is glab's subcommand", args: []string{"--yes", "update"}, want: "update"},
213 {name: "--update stays on the pass-through command", args: []string{"--update"}, want: "orbit"},
214 {name: "update after another command passes through", args: []string{"grep", "update"}, want: "orbit"},
215 }
216 for _, tc := range tests {
217 t.Run(tc.name, func(t *testing.T) {
218 t.Parallel()
219 ios, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
220 cmd := NewCmd(cmdtest.NewTestFactory(ios))
222 found, _, err := cmd.Find(tc.args)
223 require.NoError(t, err)
224 assert.Equal(t, tc.want, found.Name())
225 })
226 }
227}
Diagnostics7 git calls · 229 ms
CallStatusSizeTime0229 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=1 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.8 KiB · hit1 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=1857 git.response.cache=HIT git.response.duration_ms=1 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:0200352 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=218 git.response.bytes=352 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=60b25b923f1eb09ba37bc303fcc6997e8c86e24f 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=352 git.pkt.payload.max=325 git.sideband.band1.bytes=325 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=325 git.pack.trailer=42eb8867905de3be627920ec4bcafefa8fa8d088Open pack map
fetch2002.1 KiB218 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2186 git.response.cache=MISS git.response.duration_ms=218 git.fetch.wants=c8219ed900bc7d1fe79b81ee962330e7a3e526a4 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=2186 git.pkt.payload.max=2159 git.sideband.band1.bytes=2159 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
- a5e244360d2bcf56
- Request
- 6c797c27…
- 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