GitLabgitlab-org
cli
main
1//go:build !integration
3package lint
5import (
6 "fmt"
7 "net/http"
8 "net/http/httptest"
9 "path"
10 "runtime"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "go.uber.org/mock/gomock"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23func Test_lintRun(t *testing.T) {
24 t.Parallel()
26 type testCase struct {
27 name string
28 testFile string
29 cliArgs string
30 StdOut string
31 wantErr bool
32 errMsg string
33 showHaveBaseRepo bool
34 setupMock func(tc *gitlabtesting.TestClient)
35 }
37 tests := []testCase{
38 {
39 name: "with invalid path specified",
40 testFile: "WRONG_PATH",
41 StdOut: "",
42 wantErr: true,
43 errMsg: "WRONG_PATH: no such file or directory",
44 showHaveBaseRepo: true,
45 setupMock: func(tc *gitlabtesting.TestClient) {
46 tc.MockProjects.EXPECT().
47 GetProject("OWNER/REPO", gomock.Any()).
48 Return(&gitlab.Project{
49 ID: 123,
50 }, nil, nil)
51 },
52 },
53 {
54 name: "without base repo",
55 testFile: ".gitlab.ci.yaml",
56 StdOut: "",
57 wantErr: true,
58 errMsg: "you must be in a GitLab project repository for this action: no base repo present",
59 showHaveBaseRepo: false,
60 setupMock: func(tc *gitlabtesting.TestClient) {
61 // No mock needed - fails before API call
62 },
63 },
64 {
65 name: "when a valid path is specified and yaml is valid",
66 testFile: ".gitlab-ci.yaml",
67 StdOut: "Validating...\n✓ CI/CD YAML is valid!\n",
68 wantErr: false,
69 errMsg: "",
70 showHaveBaseRepo: true,
71 setupMock: func(tc *gitlabtesting.TestClient) {
72 tc.MockProjects.EXPECT().
73 GetProject("OWNER/REPO", gomock.Any()).
74 Return(&gitlab.Project{
75 ID: 123,
76 }, nil, nil)
77 tc.MockValidate.EXPECT().
78 ProjectNamespaceLint(int64(123), gomock.Any(), gomock.Any()).
79 Return(&gitlab.ProjectLintResult{
80 Valid: true,
81 }, nil, nil)
82 },
83 },
84 {
85 name: "when --dry-run is used without --ref",
86 testFile: ".gitlab-ci.yaml",
87 cliArgs: "--dry-run",
88 StdOut: "Validating...\n✓ CI/CD YAML is valid!\n",
89 wantErr: false,
90 errMsg: "",
91 showHaveBaseRepo: true,
92 setupMock: func(tc *gitlabtesting.TestClient) {
93 tc.MockProjects.EXPECT().
94 GetProject("OWNER/REPO", gomock.Any()).
95 Return(&gitlab.Project{
96 ID: 123,
97 }, nil, nil)
98 tc.MockValidate.EXPECT().
99 ProjectNamespaceLint(int64(123), gomock.Any(), gomock.Any()).
100 Return(&gitlab.ProjectLintResult{
101 Valid: true,
102 }, nil, nil)
103 },
104 },
105 {
106 name: "when --dry-run is used with --ref",
107 testFile: ".gitlab-ci.yaml",
108 cliArgs: "--dry-run --ref=main",
109 StdOut: "Validating...\n✓ CI/CD YAML is valid!\n",
110 wantErr: false,
111 errMsg: "",
112 showHaveBaseRepo: true,
113 setupMock: func(tc *gitlabtesting.TestClient) {
114 tc.MockProjects.EXPECT().
115 GetProject("OWNER/REPO", gomock.Any()).
116 Return(&gitlab.Project{
117 ID: 123,
118 }, nil, nil)
119 tc.MockValidate.EXPECT().
120 ProjectNamespaceLint(int64(123), gomock.Any(), gomock.Any()).
121 Return(&gitlab.ProjectLintResult{
122 Valid: true,
123 }, nil, nil)
124 },
125 },
126 }
127 for _, tt := range tests {
128 t.Run(tt.name, func(t *testing.T) {
129 t.Parallel()
131 // GIVEN
132 testClient := gitlabtesting.NewTestClient(t)
133 tt.setupMock(testClient)
135 _, filename, _, _ := runtime.Caller(0)
136 args := path.Join(path.Dir(filename), "testdata", tt.testFile)
137 if tt.cliArgs != "" {
138 args += " " + tt.cliArgs
139 }
141 opts := []cmdtest.FactoryOption{
142 cmdtest.WithGitLabClient(testClient.Client),
143 }
144 if !tt.showHaveBaseRepo {
145 opts = append(opts, cmdtest.WithBaseRepoError(fmt.Errorf("no base repo present")))
146 }
148 exec := cmdtest.SetupCmdForTest(t, NewCmdLint, false, opts...)
150 // WHEN
151 result, err := exec(args)
153 // THEN
154 if tt.wantErr {
155 require.Error(t, err)
156 require.Contains(t, err.Error(), tt.errMsg)
157 return
158 }
159 require.NoError(t, err)
161 assert.Equal(t, tt.StdOut, result.String())
162 })
163 }
164}
166func Test_lintRun_remoteURL(t *testing.T) {
167 t.Parallel()
169 t.Run("a failing response is not linted", func(t *testing.T) {
170 t.Parallel()
172 // GIVEN a URL that does not serve a CI configuration.
173 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
174 http.Error(w, "not found", http.StatusNotFound)
175 }))
176 t.Cleanup(srv.Close)
178 testClient := gitlabtesting.NewTestClient(t)
179 testClient.MockProjects.EXPECT().
180 GetProject("OWNER/REPO", gomock.Any()).
181 Return(&gitlab.Project{ID: 123}, nil, nil)
182 // No ProjectNamespaceLint call is expected: the error body must not
183 // reach the lint API.
185 exec := cmdtest.SetupCmdForTest(t, NewCmdLint, false, cmdtest.WithGitLabClient(testClient.Client))
187 // WHEN
188 _, err := exec(srv.URL + "/missing.yml")
190 // THEN
191 require.Error(t, err)
192 assert.Contains(t, err.Error(), "404 Not Found")
193 })
195 t.Run("a successful response is linted", func(t *testing.T) {
196 t.Parallel()
198 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
199 fmt.Fprint(w, "job:\n script: echo hello\n")
200 }))
201 t.Cleanup(srv.Close)
203 testClient := gitlabtesting.NewTestClient(t)
204 testClient.MockProjects.EXPECT().
205 GetProject("OWNER/REPO", gomock.Any()).
206 Return(&gitlab.Project{ID: 123}, nil, nil)
207 testClient.MockValidate.EXPECT().
208 ProjectNamespaceLint(int64(123), gomock.Any(), gomock.Any()).
209 Return(&gitlab.ProjectLintResult{Valid: true}, nil, nil)
211 exec := cmdtest.SetupCmdForTest(t, NewCmdLint, false, cmdtest.WithGitLabClient(testClient.Client))
213 result, err := exec(srv.URL + "/.gitlab-ci.yml")
215 require.NoError(t, err)
216 assert.Equal(t, "Validating...\n✓ CI/CD YAML is valid!\n", result.String())
217 })
218}
Diagnostics8 git calls · 297 ms
CallStatusSizeTime0297 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=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
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: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=1 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=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=3 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:0200635 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=635 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=517b073a0d5c32bc4e93f86de74b31d60ff8cf03 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=635 git.pkt.payload.max=608 git.sideband.band1.bytes=608 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=608 git.pack.trailer=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaaOpen pack map
fetchtree:0200206 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=285 git.response.bytes=206 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=cb4b42d99ca4351b5bc7d4917ed3d8ae0dc5408d 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=206 git.pkt.payload.max=179 git.sideband.band1.bytes=179 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=179 git.pack.trailer=a90388371338c04e45a95e8062f0a7c706f2c6bbOpen pack map
fetch2001.6 KiB285 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1634 git.response.cache=MISS git.response.duration_ms=285 git.fetch.wants=f861c1a1835b98c92fefa47efc33773782f6fcf1 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=1634 git.pkt.payload.max=1607 git.sideband.band1.bytes=1607 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
- a5e2e8c4ef48ecfa
- Request
- 39c3b6d6…
- 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