GitLabgitlab-org

cli

main
ply8 git calls212 ms99 KiB6 cachedprotocol v2
247 lines8.0 KiBRaw
1//go:build !integration
3package save
5import (
6 "strings"
7 "testing"
9 "github.com/spf13/cobra"
10 "github.com/stretchr/testify/require"
11 "go.uber.org/mock/gomock"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/git"
15 git_testing "gitlab.com/gitlab-org/cli/internal/git/testing"
16 "gitlab.com/gitlab-org/cli/internal/run"
17 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20func Test_stackAmendCmd(t *testing.T) {
21 tests := []struct {
22 desc string
23 args []string
24 files []string
25 amendedFiles []string
26 description string
27 expected string
28 wantErr bool
29 editorMessage string
30 }{
31 {
32 desc: "amending regular files",
33 args: []string{"testfile", "randomfile"},
34 files: []string{"testfile", "randomfile"},
35 amendedFiles: []string{"otherfile"},
36 description: "this is a commit message",
37 expected: "Amended diff with description: \"this is a commit message\".\n",
38 },
39 {
40 desc: "amending regular files with --no-verify",
41 args: []string{"testfile", "randomfile", "--no-verify"},
42 files: []string{"testfile", "randomfile"},
43 amendedFiles: []string{"otherfile"},
44 description: "amend with no verify",
45 expected: "Amended diff with description: \"amend with no verify\".\n",
46 },
47 {
48 desc: "with no message",
49 args: []string{"testfile", "randomfile"},
50 files: []string{"testfile", "randomfile"},
51 amendedFiles: []string{"otherfile"},
52 description: "",
53 editorMessage: "amended description",
54 expected: "Amended diff with description: \"amended description\".\n",
55 },
56 {
57 desc: "with no amended changes",
58 args: []string{"."},
59 files: []string{"oldfile"},
60 amendedFiles: []string{},
61 description: "this is a commit message",
62 expected: "no changes to save",
63 wantErr: true,
64 },
65 {
66 desc: "not on a stack branch",
67 args: []string{"asdf"},
68 files: []string{"asdf"},
69 amendedFiles: []string{"otherfile"},
70 description: "this is a commit message",
71 expected: "Could not find stack ref for branch",
72 wantErr: true,
73 },
74 }
76 for _, tc := range tests {
77 t.Run(tc.desc, func(t *testing.T) {
78 dir := git.InitGitRepoWithCommit(t)
79 err := git.SetCurrentStackTitle("cool-test-feature")
80 require.NoError(t, err)
82 createTemporaryFiles(t, dir, tc.files)
84 var saveArgs []string
85 saveArgs = append(saveArgs, "-m")
86 saveArgs = append(saveArgs, "\"original save message\"")
87 saveArgs = append(saveArgs, tc.args...)
89 getText := getMockEditor(tc.editorMessage, &[]string{})
91 ctrl := gomock.NewController(t)
92 mockCmd := git_testing.NewMockGitRunner(ctrl)
94 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
95 return NewCmdSaveStack(f, mockCmd, getText)
96 }, true,
97 cmdtest.WithGitLabClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com").Lab()),
98 )
99 _, err = exec(strings.Join(saveArgs, " "))
100 require.NoError(t, err)
102 createTemporaryFiles(t, dir, tc.amendedFiles)
103 if tc.desc == "not on a stack branch" {
104 checkout := git.GitCommand("checkout", "-b", "randobranch")
105 _, err := run.PrepareCmd(checkout).Output()
107 require.NoError(t, err)
108 }
110 amendArgs := append([]string{}, tc.args...)
111 if tc.description != "" {
112 amendArgs = append(amendArgs, "-m", "\""+tc.description+"\"")
113 }
115 execAmend := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
116 return NewCmdAmendStack(f, mockCmd, getText)
117 }, true,
118 cmdtest.WithGitLabClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com").Lab()),
119 )
121 output, err := execAmend(strings.Join(amendArgs, " "))
123 if tc.wantErr {
124 require.ErrorContains(t, err, tc.expected)
125 } else {
126 require.NoError(t, err)
127 require.Equal(t, tc.expected, output.String())
128 }
129 })
130 }
133func Test_stackAmendReword(t *testing.T) {
134 t.Run("reword with message flag and clean working tree", func(t *testing.T) {
135 description = ""
136 ios, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(true))
137 f := cmdtest.NewTestFactory(ios)
139 dir := git.InitGitRepoWithCommit(t)
140 err := git.SetCurrentStackTitle("cool-test-feature")
141 require.NoError(t, err)
143 // Create initial stack entry via save
144 createTemporaryFiles(t, dir, []string{"initialfile"})
146 getText := getMockEditor("", &[]string{})
148 ctrl := gomock.NewController(t)
149 mockCmd := git_testing.NewMockGitRunner(ctrl)
151 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
152 return NewCmdSaveStack(f, mockCmd, getText)
153 }, true,
154 cmdtest.WithGitLabClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com").Lab()),
155 )
156 _, err = exec("-m \"original message\" initialfile")
157 require.NoError(t, err)
159 // Now reword with NO file changes (clean working tree)
160 output, err := amendFunc(t.Context(), f, []string{}, getText, "reworded message", false, false, true)
161 require.NoError(t, err)
162 require.Equal(t, "Amended diff with description: \"reworded message\".\n", output)
164 // Verify the stack ref description was updated
165 ref, err := git.CurrentStackRefFromCurrentBranch("cool-test-feature")
166 require.NoError(t, err)
167 require.Equal(t, "reworded message", ref.Description)
168 })
170 t.Run("reword with --all is rejected", func(t *testing.T) {
171 description = ""
173 getText := getMockEditor("", &[]string{})
175 ctrl := gomock.NewController(t)
176 mockCmd := git_testing.NewMockGitRunner(ctrl)
178 git.InitGitRepoWithCommit(t)
180 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
181 return NewCmdAmendStack(f, mockCmd, getText)
182 }, true,
183 cmdtest.WithGitLabClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com").Lab()),
184 )
185 _, err := exec("--reword --all -m \"some message\"")
186 require.ErrorContains(t, err, "if any flags in the group [all reword] are set none of the others can be")
187 })
189 t.Run("reword with file args is rejected", func(t *testing.T) {
190 description = ""
191 ios, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(true))
193 dir := git.InitGitRepoWithCommit(t)
194 err := git.SetCurrentStackTitle("cool-test-feature")
195 require.NoError(t, err)
197 createTemporaryFiles(t, dir, []string{"initialfile"})
199 getText := getMockEditor("", &[]string{})
201 ctrl := gomock.NewController(t)
202 mockCmd := git_testing.NewMockGitRunner(ctrl)
204 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
205 return NewCmdSaveStack(f, mockCmd, getText)
206 }, true,
207 cmdtest.WithGitLabClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com").Lab()),
208 )
209 _, err = exec("-m \"original message\" initialfile")
210 require.NoError(t, err)
212 // Reword with file args should fail
213 _, err = amendFunc(t.Context(), cmdtest.NewTestFactory(ios), []string{"somefile"}, getText, "reworded", false, false, true)
214 require.ErrorContains(t, err, "--reword cannot be used with file arguments")
215 })
217 t.Run("reword without message opens editor with existing description", func(t *testing.T) {
218 description = ""
219 ios, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(true))
220 f := cmdtest.NewTestFactory(ios)
222 dir := git.InitGitRepoWithCommit(t)
223 err := git.SetCurrentStackTitle("cool-test-feature")
224 require.NoError(t, err)
226 createTemporaryFiles(t, dir, []string{"initialfile"})
228 // Editor returns a new message when invoked
229 getText := getMockEditor("editor reworded message", &[]string{})
231 ctrl := gomock.NewController(t)
232 mockCmd := git_testing.NewMockGitRunner(ctrl)
234 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
235 return NewCmdSaveStack(f, mockCmd, getText)
236 }, true,
237 cmdtest.WithGitLabClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com").Lab()),
238 )
239 _, err = exec("-m \"original message\" initialfile")
240 require.NoError(t, err)
242 // Reword with no -m flag — should open editor and use its output
243 output, err := amendFunc(t.Context(), f, []string{}, getText, "", false, false, true)
244 require.NoError(t, err)
245 require.Equal(t, "Amended diff with description: \"editor reworded message\".\n", output)
246 })
Diagnostics8 git calls · 212 ms
CallStatusSizeTime0212 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 · 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=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 · hit3 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=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:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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:0200432 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=432
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=ff2d1e7abc74964e5679ebdb51f1ac0d0dc3aff0
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=432
git.pkt.payload.max=405
git.sideband.band1.bytes=405
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=405
git.pack.trailer=8f3029a269217d94d12d0671e0490fe3d5506303
Open pack map
fetchtree:0200254 B · 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=254
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=1bddb68dc447e75c7fc2f3cd02e93e572448d630
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=254
git.pkt.payload.max=227
git.sideband.band1.bytes=227
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=227
git.pack.trailer=5113828f2359f89a3269ef1119ad801c584c57a8
Open pack map
fetch2001.8 KiB200 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=200
git.response.bytes=1827
git.response.cache=MISS
git.response.duration_ms=200
git.fetch.wants=7e93ac7969285d5424a785c02af9d9d4c56dfca4
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=1827
git.pkt.payload.max=1800
git.sideband.band1.bytes=1800
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
a5e1f8971ec3efe9
Request
190f6e5f…
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