GitLabgitlab-org
cli
main
1//go:build !integration
3package delete
5import (
6 "fmt"
7 "strings"
8 "testing"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/acarl005/stripansi"
12 "github.com/google/shlex"
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/testing/cmdtest"
21)
23// TODO: test by mocking the appropriate api function
24func TestMain(m *testing.M) {
25 cmdtest.InitTest(m, "mr_delete_test")
26}
28func Test_deleteMergeRequest(t *testing.T) {
29 io, _, stdout, stderr := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(true))
30 f := cmdtest.NewTestFactory(
31 io,
32 cmdtest.WithConfig(config.NewFromString(heredoc.Doc(`
33 hosts:
34 gitlab.com:
35 username: monalisa
36 token: OTOKEN
37 `))),
38 cmdtest.WithBranch("master"),
39 )
40 oldDeleteMR := api.DeleteMR
42 api.DeleteMR = func(client *gitlab.Client, projectID any, mrID int64) error {
43 if projectID == "" || projectID == "WRONG_REPO" || projectID == "expected_err" || mrID == 0 {
44 return fmt.Errorf("error expected")
45 }
46 return nil
47 }
49 api.GetMR = func(client *gitlab.Client, projectID any, mrID int64, opts *gitlab.GetMergeRequestsOptions) (*gitlab.MergeRequest, error) {
50 if projectID == "" || projectID == "WRONG_REPO" || projectID == "expected_err" {
51 return nil, fmt.Errorf("error expected")
52 }
53 repo, err := f.BaseRepo()
54 if err != nil {
55 return nil, err
56 }
57 return &gitlab.MergeRequest{
58 BasicMergeRequest: gitlab.BasicMergeRequest{
59 ID: mrID,
60 IID: mrID,
61 Title: "mrTitle",
62 Labels: gitlab.Labels{"test", "bug"},
63 State: "opened",
64 Description: "mrBody",
65 Author: &gitlab.BasicUser{
66 ID: mrID,
67 Name: "John Dev Wick",
68 Username: "jdwick",
69 },
70 WebURL: fmt.Sprintf("https://%s/%s/-/merge_requests/%d", repo.RepoHost(), repo.FullName(), mrID),
71 },
72 }, nil
73 }
75 api.ListMRs = func(client *gitlab.Client, projectID any, opts *gitlab.ListProjectMergeRequestsOptions, listOpts ...api.CliListMROption) ([]*gitlab.BasicMergeRequest, error) {
76 return []*gitlab.BasicMergeRequest{}, nil
77 }
79 tests := []struct {
80 name string
81 args []string
82 wantErr bool
83 errMsg string
84 assertFunc func(*testing.T, string, string, error)
85 }{
86 {
87 name: "delete",
88 args: []string{"0"},
89 wantErr: true,
91 assertFunc: func(t *testing.T, out, outErr string, err error) {
92 t.Helper()
94 assert.Equal(t, "invalid merge request ID provided", err.Error())
95 },
96 },
97 {
98 name: "id exists",
99 args: []string{"1"},
100 wantErr: false,
101 assertFunc: func(t *testing.T, out, outErr string, err error) {
102 t.Helper()
104 assert.Contains(t, out, "- Deleting merge request !1.\n")
105 assert.Contains(t, out, "✓ Merge request !1 deleted.\n")
106 },
107 },
108 {
109 name: "delete on different repo",
110 args: []string{"1", "-R", "profclems/glab"},
111 wantErr: false,
112 assertFunc: func(t *testing.T, out, outErr string, err error) {
113 t.Helper()
115 assert.Contains(t, out, "- Deleting merge request !1.\n")
116 assert.Contains(t, out, "✓ Merge request !1 deleted.\n")
117 },
118 },
119 {
120 name: "delete no args",
121 wantErr: true,
122 assertFunc: func(t *testing.T, out, outErr string, err error) {
123 t.Helper()
125 assert.Equal(t, `no open merge request available for "master"`, err.Error())
126 },
127 },
128 }
130 cmd := NewCmdDelete(f)
132 cmd.Flags().StringP("repo", "R", "", "")
134 for _, tt := range tests {
135 t.Run(tt.name, func(t *testing.T) {
136 cli := strings.Join(tt.args, " ")
137 t.Log(cli)
138 argv, err := shlex.Split(cli)
139 if err != nil {
140 t.Fatal(err)
141 }
142 cmd.SetArgs(argv)
143 _, err = cmd.ExecuteC()
144 if !tt.wantErr {
145 require.NoError(t, err)
146 } else {
147 require.Error(t, err)
148 }
150 out := stripansi.Strip(stdout.String())
151 outErr := stripansi.Strip(stderr.String())
152 t.Log(outErr)
154 tt.assertFunc(t, out, outErr, err)
155 assert.Contains(t, outErr, tt.errMsg)
156 })
157 }
159 api.DeleteMR = oldDeleteMR
160}
Diagnostics8 git calls · 231 ms
CallStatusSizeTime0231 ms
discover200185 B2 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 KiB1 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=1 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=1 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=2 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 · 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: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=8 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
fetchtree:0200746 B · hit8 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=746 git.response.cache=HIT git.response.duration_ms=8 git.fetch.filter=tree:0 git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f 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=746 git.pkt.payload.max=719 git.sideband.band1.bytes=719 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=719 git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200142 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=212 git.response.bytes=142 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=84a33366c60bf4650f74140e27fca8a15c000918 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=142 git.pkt.payload.max=115 git.sideband.band1.bytes=115 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=115 git.pack.trailer=c4a1fc30965cecbe07eaf6d47ff816bdc7fc2cdaOpen pack map
fetch2001.5 KiB212 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1539 git.response.cache=MISS git.response.duration_ms=212 git.fetch.wants=b9ddd3171718dff3c1e8aa9bb62995093a9b991d 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=1539 git.pkt.payload.max=1512 git.sideband.band1.bytes=1512 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
- a5e2df34fe532308
- Request
- 81107ff8…
- 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