MCPcopy Index your code
hub / github.com/cli/cli / Test_readFileRun

Function Test_readFileRun

pkg/cmd/repo/read-file/read_file_test.go:163–565  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

161}
162
163func Test_readFileRun(t *testing.T) {
164 tests := []struct {
165 name string
166 tty bool
167 opts ReadFileOptions
168 httpStubs func(*httpmock.Registry)
169 jsonFields []string
170 wantOut string
171 wantStderr string
172 wantErrMsg string
173 }{
174 {
175 name: "base repo resolution error is wrapped with a hint",
176 tty: false,
177 opts: ReadFileOptions{
178 BaseRepo: func() (ghrepo.Interface, error) {
179 return nil, errors.New("some error")
180 },
181 },
182 wantErrMsg: "some error. Run this command from within a git repository, or use the `--repo` flag to specify one",
183 },
184 {
185 name: "writes file content to stdout (non-tty)",
186 tty: false,
187 httpStubs: func(reg *httpmock.Registry) {
188 reg.Register(
189 func(req *http.Request) bool {
190 return req.Method == "GET" &&
191 req.URL.EscapedPath() == "/repos/OWNER/REPO/contents/README.md" &&
192 req.Header.Get("Accept") == "application/vnd.github.object+json"
193 },
194 httpmock.JSONResponse(fileContentResponse("README.md", "hello world\n")),
195 )
196 },
197 wantOut: "hello world\n",
198 },
199 {
200 name: "writes file content through pager (tty)",
201 tty: true,
202 httpStubs: func(reg *httpmock.Registry) {
203 reg.Register(
204 httpmock.REST("GET", "repos/OWNER/REPO/contents/README.md"),
205 httpmock.JSONResponse(fileContentResponse("README.md", "hello world\n")),
206 )
207 },
208 wantOut: "hello world\n",
209 },
210 {
211 name: "reads from a ref",
212 tty: false,
213 opts: ReadFileOptions{Ref: "v1.2.3"},
214 httpStubs: func(reg *httpmock.Registry) {
215 reg.Register(
216 func(req *http.Request) bool {
217 return req.Method == "GET" && strings.HasSuffix(req.URL.String(), "/repos/OWNER/REPO/contents/README.md?ref=v1.2.3")
218 },
219 httpmock.JSONResponse(fileContentResponse("README.md", "tagged\n")),
220 )

Callers

nothing calls this directly

Calls 15

RegisterMethod · 0.95
VerifyMethod · 0.95
JSONResponseFunction · 0.92
RESTFunction · 0.92
StatusStringResponseFunction · 0.92
StringResponseFunction · 0.92
TestFunction · 0.92
NewFunction · 0.92
NewJSONExporterFunction · 0.92
fileContentResponseFunction · 0.85
fileContentResponseBytesFunction · 0.85
pngBytesFunction · 0.85

Tested by

no test coverage detected