File: code_test.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (342 lines) | stat: -rw-r--r-- 8,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package code

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/cli/cli/v2/internal/browser"
	"github.com/cli/cli/v2/pkg/cmdutil"
	"github.com/cli/cli/v2/pkg/iostreams"
	"github.com/cli/cli/v2/pkg/search"
	"github.com/google/shlex"
	"github.com/stretchr/testify/assert"
)

func TestNewCmdCode(t *testing.T) {
	tests := []struct {
		name    string
		input   string
		output  CodeOptions
		wantErr bool
		errMsg  string
	}{
		{
			name:    "no arguments",
			input:   "",
			wantErr: true,
			errMsg:  "specify search keywords or flags",
		},
		{
			name:  "keyword arguments",
			input: "react lifecycle",
			output: CodeOptions{
				Query: search.Query{
					Keywords: []string{"react", "lifecycle"},
					Kind:     "code",
					Limit:    30,
				},
			},
		},
		{
			name:  "web flag",
			input: "--web",
			output: CodeOptions{
				Query: search.Query{
					Keywords: []string{},
					Kind:     "code",
					Limit:    30,
				},
				WebMode: true,
			},
		},
		{
			name:  "limit flag",
			input: "--limit 10",
			output: CodeOptions{
				Query: search.Query{
					Keywords: []string{},
					Kind:     "code",
					Limit:    10,
				},
			},
		},
		{
			name:    "invalid limit flag",
			input:   "--limit 1001",
			wantErr: true,
			errMsg:  "`--limit` must be between 1 and 1000",
		},
		{
			name: "qualifier flags",
			input: `
			--extension=extension
			--filename=filename
			--match=path,file
			--language=language
			--repo=owner/repo
			--size=5
			--owner=owner
			`,
			output: CodeOptions{
				Query: search.Query{
					Keywords: []string{},
					Kind:     "code",
					Limit:    30,
					Qualifiers: search.Qualifiers{
						Extension: "extension",
						Filename:  "filename",
						In:        []string{"path", "file"},
						Language:  "language",
						Repo:      []string{"owner/repo"},
						Size:      "5",
						User:      []string{"owner"},
					},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ios, _, _, _ := iostreams.Test()
			f := &cmdutil.Factory{
				IOStreams: ios,
			}
			argv, err := shlex.Split(tt.input)
			assert.NoError(t, err)
			var gotOpts *CodeOptions
			cmd := NewCmdCode(f, func(opts *CodeOptions) error {
				gotOpts = opts
				return nil
			})
			cmd.SetArgs(argv)
			cmd.SetIn(&bytes.Buffer{})
			cmd.SetOut(&bytes.Buffer{})
			cmd.SetErr(&bytes.Buffer{})

			_, err = cmd.ExecuteC()
			if tt.wantErr {
				assert.EqualError(t, err, tt.errMsg)
				return
			}

			assert.NoError(t, err)
			assert.Equal(t, tt.output.Query, gotOpts.Query)
			assert.Equal(t, tt.output.WebMode, gotOpts.WebMode)
		})
	}
}

func TestCodeRun(t *testing.T) {
	var query = search.Query{
		Keywords: []string{"map"},
		Kind:     "code",
		Limit:    30,
		Qualifiers: search.Qualifiers{
			Language: "go",
			Repo:     []string{"cli/cli"},
		},
	}
	tests := []struct {
		errMsg     string
		name       string
		opts       *CodeOptions
		tty        bool
		wantErr    bool
		wantStderr string
		wantStdout string
	}{
		{
			name: "displays results tty",
			opts: &CodeOptions{
				Query: query,
				Searcher: &search.SearcherMock{
					CodeFunc: func(query search.Query) (search.CodeResult, error) {
						return search.CodeResult{
							IncompleteResults: false,
							Items: []search.Code{
								{
									Name: "context.go",
									Path: "context/context.go",
									Repository: search.Repository{
										FullName: "cli/cli",
									},
									TextMatches: []search.TextMatch{
										{
											Fragment: "\t}\n\n\tvar repos []*api.Repository\n\trepoMap := map[string]bool{}\n\n\tadd := func(r *api.Repository) {\n\t\tfn := ghrepo.FullName(r)",
											Matches: []search.Match{
												{
													Text:    "Map",
													Indices: []int{38, 41},
												},
												{
													Text:    "map",
													Indices: []int{45, 48},
												},
											},
										},
									},
								},
								{
									Name: "pr.go",
									Path: "pkg/cmd/pr/pr.go",
									Repository: search.Repository{
										FullName: "cli/cli",
									},
									TextMatches: []search.TextMatch{
										{

											Fragment: "\t\t\t$ gh pr create --fill\n\t\t\t$ gh pr view --web\n\t\t`),\n\t\tAnnotations: map[string]string{\n\t\t\t\"help:arguments\": heredoc.Doc(`\n\t\t\t\tA pull request can be supplied as argument in any of the following formats:\n\t\t\t\t- by number, e.g. \"123\";",
											Matches: []search.Match{
												{
													Text:    "map",
													Indices: []int{68, 71},
												},
											},
										},
									},
								},
							},
							Total: 2,
						}, nil
					},
				},
			},
			tty:        true,
			wantStdout: "\nShowing 2 of 2 results\n\ncli/cli context/context.go\n\trepoMap := map[string]bool{}\n\ncli/cli pkg/cmd/pr/pr.go\n\tAnnotations: map[string]string{\n",
		},
		{
			name: "displays results notty",
			opts: &CodeOptions{
				Query: query,
				Searcher: &search.SearcherMock{
					CodeFunc: func(query search.Query) (search.CodeResult, error) {
						return search.CodeResult{
							IncompleteResults: false,
							Items: []search.Code{
								{
									Name: "context.go",
									Path: "context/context.go",
									Repository: search.Repository{
										FullName: "cli/cli",
									},
									TextMatches: []search.TextMatch{
										{
											Fragment: "\t}\n\n\tvar repos []*api.Repository\n\trepoMap := map[string]bool{}\n\n\tadd := func(r *api.Repository) {\n\t\tfn := ghrepo.FullName(r)",
											Matches: []search.Match{
												{
													Text:    "Map",
													Indices: []int{38, 41},
												},
												{
													Text:    "map",
													Indices: []int{45, 48},
												},
											},
										},
									},
								},
								{
									Name: "pr.go",
									Path: "pkg/cmd/pr/pr.go",
									Repository: search.Repository{
										FullName: "cli/cli",
									},
									TextMatches: []search.TextMatch{
										{

											Fragment: "\t\t\t$ gh pr create --fill\n\t\t\t$ gh pr view --web\n\t\t`),\n\t\tAnnotations: map[string]string{\n\t\t\t\"help:arguments\": heredoc.Doc(`\n\t\t\t\tA pull request can be supplied as argument in any of the following formats:\n\t\t\t\t- by number, e.g. \"123\";",
											Matches: []search.Match{
												{
													Text:    "map",
													Indices: []int{68, 71},
												},
											},
										},
									},
								},
							},
							Total: 2,
						}, nil
					},
				},
			},
			tty:        false,
			wantStdout: "cli/cli:context/context.go: repoMap := map[string]bool{}\ncli/cli:pkg/cmd/pr/pr.go: Annotations: map[string]string{\n",
		},
		{
			name: "displays no results",
			opts: &CodeOptions{
				Query: query,
				Searcher: &search.SearcherMock{
					CodeFunc: func(query search.Query) (search.CodeResult, error) {
						return search.CodeResult{}, nil
					},
				},
			},
			wantErr: true,
			errMsg:  "no code results matched your search",
		},
		{
			name: "displays search error",
			opts: &CodeOptions{
				Query: query,
				Searcher: &search.SearcherMock{
					CodeFunc: func(query search.Query) (search.CodeResult, error) {
						return search.CodeResult{}, fmt.Errorf("error with query")
					},
				},
			},
			wantErr: true,
			errMsg:  "error with query",
		},
		{
			name: "opens browser for web mode tty",
			opts: &CodeOptions{
				Browser: &browser.Stub{},
				Query:   query,
				Searcher: &search.SearcherMock{
					URLFunc: func(query search.Query) string {
						return "https://github.com/search?type=code&q=map+repo%3Acli%2Fcli"
					},
				},
				WebMode: true,
			},
			tty:        true,
			wantStderr: "Opening github.com/search in your browser.\n",
		},
		{
			name: "opens browser for web mode notty",
			opts: &CodeOptions{
				Browser: &browser.Stub{},
				Query:   query,
				Searcher: &search.SearcherMock{
					URLFunc: func(query search.Query) string {
						return "https://github.com/search?type=code&q=map+repo%3Acli%2Fcli"
					},
				},
				WebMode: true,
			},
		},
	}

	for _, tt := range tests {
		ios, _, stdout, stderr := iostreams.Test()
		ios.SetStdinTTY(tt.tty)
		ios.SetStdoutTTY(tt.tty)
		ios.SetStderrTTY(tt.tty)
		tt.opts.IO = ios
		t.Run(tt.name, func(t *testing.T) {
			err := codeRun(tt.opts)
			if tt.wantErr {
				assert.EqualError(t, err, tt.errMsg)
				return
			} else if err != nil {
				t.Fatalf("codeRun unexpected error: %v", err)
			}
			assert.Equal(t, tt.wantStdout, stdout.String())
			assert.Equal(t, tt.wantStderr, stderr.String())
		})
	}
}