MCPcopy Create free account
hub / github.com/cli/cli / Test_readFileRun

Function Test_readFileRun

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

Source from the content-addressed store, hash-verified

163}
164
165func Test_readFileRun(t *testing.T) {
166 tests := []struct {
167 name string
168 tty bool
169 opts ReadFileOptions
170 httpStubs func(*httpmock.Registry)
171 jsonFields []string
172 setup func(t *testing.T)
173 verify func(t *testing.T)
174 wantOut string
175 wantStderr string
176 wantErrMsg string
177 }{
178 {
179 name: "base repo resolution error is wrapped with a hint",
180 tty: false,
181 opts: ReadFileOptions{
182 BaseRepo: func() (ghrepo.Interface, error) {
183 return nil, errors.New("some error")
184 },
185 },
186 wantErrMsg: "some error. Run this command from within a git repository, or use the `--repo` flag to specify one",
187 },
188 {
189 name: "writes file content to stdout (non-tty)",
190 tty: false,
191 httpStubs: func(reg *httpmock.Registry) {
192 reg.Register(
193 func(req *http.Request) bool {
194 return req.Method == "GET" &&
195 req.URL.EscapedPath() == "/repos/OWNER/REPO/contents/README.md" &&
196 req.Header.Get("Accept") == "application/vnd.github.object+json"
197 },
198 httpmock.JSONResponse(fileContentResponse("README.md", "hello world\n")),
199 )
200 },
201 wantOut: "hello world\n",
202 },
203 {
204 name: "directory output saves only the remote basename",
205 opts: ReadFileOptions{Output: "out/"},
206 setup: func(t *testing.T) {
207 t.Helper()
208 t.Chdir(t.TempDir())
209 },
210 httpStubs: func(reg *httpmock.Registry) {
211 reg.Register(
212 httpmock.REST("GET", "repos/OWNER/REPO/contents/README.md"),
213 httpmock.JSONResponse(fileContentResponse(filepath.Join("nested", "notes.txt"), "hello world\n")),
214 )
215 },
216 verify: func(t *testing.T) {
217 t.Helper()
218 entries, err := os.ReadDir("out")
219 require.NoError(t, err)
220 require.Len(t, entries, 1)
221 assert.Equal(t, "notes.txt", entries[0].Name())
222 content, err := os.ReadFile(filepath.Join("out", "notes.txt"))

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