GitLabgitlab-org
cli
main
1//go:build !integration
3package commands
5import (
6 "path"
7 "regexp"
8 "strings"
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.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/commands/skills/bundled"
19 "gitlab.com/gitlab-org/cli/internal/config"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23// Checks that every glab command and flag in the bundled SKILL.md files
24// exists. Agents copy these commands exactly, so a renamed command or flag
25// leaves them with instructions that fail.
26//
27// It lives in this package rather than in bundled because it needs the full
28// command tree. Being here also means make test-changed runs it whenever a
29// command changes.
30func TestBundledSkillsUseRealCommandsAndFlags(t *testing.T) {
31 ios, _, _, _ := cmdtest.TestIOStreams()
32 root := NewCmdRoot(cmdutils.NewFactory(ios, false, config.NewBlankConfig(), api.BuildInfo{Version: "v1.0.0", Commit: "abcdefgh"}))
34 skills, err := bundled.All()
35 require.NoError(t, err)
36 require.NotEmpty(t, skills)
38 checked := 0
39 for _, s := range skills {
40 for name, contents := range s.Files {
41 if path.Ext(name) != ".md" {
42 continue
43 }
44 for _, inv := range glabInvocations(string(contents)) {
45 checked++
46 for _, problem := range checkInvocation(root, inv) {
47 t.Errorf("%s/%s: %s:\n\t%s", s.Name, name, problem, inv)
48 }
49 }
50 }
51 }
52 require.NotZero(t, checked, "no glab invocations found in bundled skills")
53}
55var codeSpanRE = regexp.MustCompile("`(glab(?:\\s[^`]*)?)`")
57// glabInvocations returns the glab commands in fenced blocks and inline code
58// spans. Fenced lines ending in a backslash are joined with the next line.
59func glabInvocations(md string) []string {
60 var out, prose []string
61 inFence := false
62 pending := ""
63 for line := range strings.SplitSeq(md, "\n") {
64 trimmed := strings.TrimSpace(line)
65 if strings.HasPrefix(trimmed, "```") {
66 inFence = !inFence
67 pending = ""
68 continue
69 }
70 if !inFence {
71 prose = append(prose, line)
72 continue
73 }
74 if pending != "" {
75 trimmed = pending + " " + trimmed
76 pending = ""
77 }
78 if !strings.HasPrefix(trimmed, "glab ") {
79 continue
80 }
81 if cont, ok := strings.CutSuffix(trimmed, "\\"); ok {
82 pending = strings.TrimSpace(cont)
83 continue
84 }
85 out = append(out, trimmed)
86 }
87 for _, m := range codeSpanRE.FindAllStringSubmatch(strings.Join(prose, "\n"), -1) {
88 out = append(out, strings.Join(strings.Fields(m[1]), " "))
89 }
90 return out
91}
93func checkInvocation(root *cobra.Command, inv string) []string {
94 words, err := shlex.Split(inv)
95 if err != nil {
96 return []string{"cannot parse: " + err.Error()}
97 }
98 words = words[1:]
99 for i, w := range words {
100 if isShellOperator(w) {
101 words = words[:i]
102 break
103 }
104 }
106 // Skills write arguments as placeholders like <iid> or <branch>...,
107 // which root.Find would reject as unknown commands.
108 pathEnd := len(words)
109 for i, w := range words {
110 if strings.HasPrefix(w, "-") || strings.HasPrefix(w, "<") {
111 pathEnd = i
112 break
113 }
114 }
115 cmd, args, err := root.Find(words[:pathEnd])
116 if err != nil {
117 return []string{err.Error()}
118 }
119 if len(args) > 0 && cmd.HasSubCommands() {
120 return []string{"unknown subcommand " + args[0] + " for " + cmd.CommandPath()}
121 }
122 cmd.InitDefaultHelpFlag()
124 var problems []string
125 for _, w := range words[pathEnd:] {
126 if len(w) < 2 || w[0] != '-' || (w[1] >= '0' && w[1] <= '9') {
127 continue
128 }
129 if long, ok := strings.CutPrefix(w, "--"); ok {
130 long, _, _ = strings.Cut(long, "=")
131 if cmd.Flag(long) == nil {
132 problems = append(problems, "unknown flag --"+long+" for "+cmd.CommandPath())
133 }
134 continue
135 }
136 short, _, _ := strings.Cut(w[1:], "=")
137 for _, r := range short {
138 if !hasShorthand(cmd, string(r)) {
139 problems = append(problems, "unknown flag -"+string(r)+" for "+cmd.CommandPath())
140 }
141 }
142 }
143 return problems
144}
146// isShellOperator reports whether w ends the glab part of a command line.
147// A placeholder like <iid> also starts with "<", so an input redirect is only
148// "<", a heredoc "<<...", or "<file" with no closing ">".
149func isShellOperator(w string) bool {
150 switch {
151 case w == "|", w == ";", w == "&&":
152 return true
153 case strings.HasPrefix(w, ">"), strings.HasPrefix(w, "#"), strings.HasPrefix(w, "<<"):
154 return true
155 case strings.HasPrefix(w, "<"):
156 return !strings.Contains(w, ">")
157 }
158 return false
159}
161func hasShorthand(cmd *cobra.Command, s string) bool {
162 return cmd.Flags().ShorthandLookup(s) != nil || cmd.InheritedFlags().ShorthandLookup(s) != nil
163}
165func TestGlabInvocations(t *testing.T) {
166 t.Parallel()
168 md := strings.Join([]string{
169 "Run `glab mr view <iid>` first.",
170 "Prose mentioning glab outside a span is ignored.",
171 "```shell",
172 "# a comment",
173 "glab api projects/:id \\",
174 " -X POST",
175 "cat file | glab ignored",
176 "```",
177 "Wrapped `glab issue",
178 "note` span.",
179 }, "\n")
181 assert.Equal(t, []string{
182 "glab api projects/:id -X POST",
183 "glab mr view <iid>",
184 "glab issue note",
185 }, glabInvocations(md))
186}
188// newTestRoot builds a fresh tree per call: checkInvocation lazily fills
189// cobra's flag sets, so parallel subtests can't share one.
190func newTestRoot() *cobra.Command {
191 root := &cobra.Command{Use: "glab"}
192 root.PersistentFlags().StringP("repo", "R", "", "")
193 mr := &cobra.Command{Use: "mr", Run: func(*cobra.Command, []string) {}}
194 view := &cobra.Command{Use: "view", Run: func(*cobra.Command, []string) {}}
195 view.Flags().StringP("message", "m", "", "")
196 view.Flags().String("reply", "", "")
197 view.Flags().BoolP("all", "a", false, "")
198 mr.AddCommand(view)
199 root.AddCommand(mr)
200 return root
201}
203func TestCheckInvocation(t *testing.T) {
204 t.Parallel()
206 tests := []struct {
207 name string
208 inv string
209 want []string
210 }{
211 {"valid", `glab mr view <iid> --reply <id> -m "hi"`, nil},
212 {"inherited and --flag=value", "glab mr view -R g/p --reply=1", nil},
213 {"combined shorthands", "glab mr view -am x", nil},
214 {"stdin dash and negative number skipped", "glab mr view - -1", nil},
215 {"help flag", "glab mr view --help", nil},
216 {"unknown flag after placeholder", "glab mr view <iid> --bogus", []string{"unknown flag --bogus for glab mr view"}},
217 {"unknown shorthand", "glab mr view -q", []string{"unknown flag -q for glab mr view"}},
218 {"unknown subcommand under runnable parent", "glab mr veiw", []string{"unknown subcommand veiw for glab mr"}},
219 {"stops at pipe", "glab mr view | jq --bogus", nil},
220 {"stops at heredoc", "glab mr view <<'EOF' --bogus", nil},
221 {"stops at input redirect", "glab mr view < /tmp/x --bogus", nil},
222 {"stops at comment", "glab mr view # --bogus", nil},
223 {"unclosed quote", `glab mr view -m "x`, []string{"cannot parse: EOF found when expecting closing quote"}},
224 }
225 for _, tc := range tests {
226 t.Run(tc.name, func(t *testing.T) {
227 t.Parallel()
228 assert.Equal(t, tc.want, checkInvocation(newTestRoot(), tc.inv))
229 })
230 }
231}
Diagnostics6 git calls · 256 ms
CallStatusSizeTime0256 ms
discover200185 B3 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 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
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=4 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
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=3 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=246 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 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
fetch2002.7 KiB246 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2759 git.response.cache=MISS git.response.duration_ms=246 git.fetch.wants=a9e3f20acb5a8b401ee1cd2ffc1edca20ca28c01 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=2759 git.pkt.payload.max=2732 git.sideband.band1.bytes=2732 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
- a5e2d52e3d64689d
- Request
- 97494457…
- 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