GitLabgitlab-org

cli

main
ply8 git calls207 ms99 KiB5 cachedprotocol v2
152 lines4.5 KiBRaw
1//go:build !integration
3package bundled
5import (
6 "path"
7 "regexp"
8 "strings"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
14 "gitlab.com/gitlab-org/cli/internal/commands/api"
15 "gitlab.com/gitlab-org/cli/internal/commands/skills/skill"
18func TestAll(t *testing.T) {
19 t.Parallel()
21 skills, err := All()
22 require.NoError(t, err)
23 require.NotEmpty(t, skills)
25 for _, s := range skills {
26 assert.NotEmpty(t, s.Name, "skill name should be set")
27 assert.NotEmpty(t, s.Description, "skill description should be set")
28 assert.Equal(t, skill.SourceBundled, s.Source, "Source should be set to bundled")
29 assert.NotEmpty(t, s.Files[skill.FileName], "skill must include %s", skill.FileName)
30 assert.NotEmpty(t, s.SkillFile(), "SkillFile() must return SKILL.md content")
31 }
34func TestGet_Known(t *testing.T) {
35 t.Parallel()
37 s, err := Get("glab")
38 require.NoError(t, err)
39 assert.Equal(t, "glab", s.Name)
40 assert.Equal(t, skill.SourceBundled, s.Source)
41 assert.NotEmpty(t, s.Description)
42 assert.Contains(t, string(s.SkillFile()), "name: glab")
45func TestGet_Unknown(t *testing.T) {
46 t.Parallel()
48 _, err := Get("does-not-exist")
49 require.Error(t, err)
50 assert.ErrorIs(t, err, ErrNotFound, "error should wrap ErrNotFound")
53func TestRelPath(t *testing.T) {
54 t.Parallel()
56 t.Run("relative under root", func(t *testing.T) {
57 t.Parallel()
58 rel, err := relPath("assets/glab", "assets/glab/SKILL.md")
59 require.NoError(t, err)
60 assert.Equal(t, "SKILL.md", rel)
61 })
63 t.Run("nested under root", func(t *testing.T) {
64 t.Parallel()
65 rel, err := relPath("assets/glab", "assets/glab/scripts/run.sh")
66 require.NoError(t, err)
67 assert.Equal(t, "scripts/run.sh", rel)
68 })
70 t.Run("rejects parent traversal", func(t *testing.T) {
71 t.Parallel()
72 // path.Clean turns "assets/glab/../other" into "assets/other"
73 // which doesn't start with "assets/glab/".
74 _, err := relPath("assets/glab", path.Clean("assets/glab/../other/SKILL.md"))
75 require.Error(t, err)
76 assert.Contains(t, err.Error(), "not under skill root")
77 })
79 t.Run("rejects sibling directory", func(t *testing.T) {
80 t.Parallel()
81 _, err := relPath("assets/glab", "assets/glab-stack/SKILL.md")
82 require.Error(t, err)
83 assert.Contains(t, err.Error(), "not under skill root")
84 })
86 t.Run("rejects path equal to root", func(t *testing.T) {
87 t.Parallel()
88 _, err := relPath("assets/glab", "assets/glab")
89 require.Error(t, err)
90 assert.Contains(t, err.Error(), "equals skill root")
91 })
93 t.Run("rejects absolute path", func(t *testing.T) {
94 t.Parallel()
95 _, err := relPath("assets/glab", "/etc/passwd")
96 require.Error(t, err)
97 assert.Contains(t, err.Error(), "not under skill root")
98 })
101// A bundled skill is instructions an agent follows literally, so an example that
102// names a placeholder `glab api` does not expand is not a typo -- the agent sends
103// it verbatim and gets HTTP 400. `:iid` shipped in five examples this way (#8531).
104//
105// The allowed set comes from api.Placeholders rather than a copy of it, so a
106// placeholder added or removed there cannot silently disagree with the docs.
107func TestBundledSkillsOnlyUseRealPlaceholders(t *testing.T) {
108 t.Parallel()
110 allowed := map[string]bool{}
111 for _, p := range api.Placeholders {
112 // Compound entries such as "group/:namespace/:repo" are matched one
113 // token at a time by the expander, so record their atoms.
114 for atom := range strings.SplitSeq(p, "/:") {
115 allowed[atom] = true
116 }
117 }
119 // A placeholder is a colon followed by a bare word. "https://", ": " in a
120 // header, and `"key":"value"` in JSON all fail to match, which is what keeps
121 // this to actual placeholder positions.
122 tokenRE := regexp.MustCompile(`:([A-Za-z_][A-Za-z0-9_]*)`)
123 declaredRE := regexp.MustCompile(`--placeholder[ =]([A-Za-z][A-Za-z0-9_-]*)=`)
125 skills, err := All()
126 require.NoError(t, err)
127 require.NotEmpty(t, skills)
129 for _, s := range skills {
130 for name, contents := range s.Files {
131 if path.Ext(name) != ".md" {
132 continue
133 }
134 lineNo := 0
135 for line := range strings.SplitSeq(string(contents), "\n") {
136 lineNo++
137 if !strings.Contains(line, "glab api") {
138 continue
139 }
140 declared := map[string]bool{}
141 for _, d := range declaredRE.FindAllStringSubmatch(line, -1) {
142 declared[d[1]] = true
143 }
144 for _, m := range tokenRE.FindAllStringSubmatch(line, -1) {
145 assert.Truef(t, allowed[m[1]] || declared[m[1]],
146 "%s:%d uses %q, which glab api does not expand and sends verbatim:\n\t%s",
147 name, lineNo, m[0], strings.TrimSpace(line))
148 }
149 }
150 }
151 }
Diagnostics8 git calls · 207 ms
CallStatusSizeTime0207 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=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 KiB2 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=2
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 · 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
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: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=4
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200370 B · 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=370
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=290ae7a62f76e23cbbd60697570f19b90ddcece6
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=370
git.pkt.payload.max=343
git.sideband.band1.bytes=343
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=343
git.pack.trailer=0795a26d2b25022c69da2ef2e68cfb92a77ef4bb
Open pack map
fetchtree:0200172 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=191
git.response.bytes=172
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=1d776c45e9ce17714f94b096cf4965ac6cec85d5
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=172
git.pkt.payload.max=145
git.sideband.band1.bytes=145
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=145
git.pack.trailer=4366d9320a915cc228b00c299374927ba2a6e519
Open pack map
fetch2001.6 KiB191 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1632
git.response.cache=MISS
git.response.duration_ms=191
git.fetch.wants=334664e185c4e284355d83e87a3cd9fb18c92118
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=1632
git.pkt.payload.max=1605
git.sideband.band1.bytes=1605
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
a5e307ec7e9815db
Request
9aeb3169…
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