@@ -8,9 +8,11 @@ import (
88 "net/http"
99 "os"
1010 "path/filepath"
11+ "strings"
1112 "testing"
1213 "time"
1314
15+ "github.com/MakeNowJust/heredoc"
1416 "github.com/cli/cli/git"
1517 "github.com/cli/cli/internal/ghrepo"
1618 "github.com/cli/cli/pkg/cmdutil"
@@ -45,6 +47,7 @@ func Test_NewCmdApi(t *testing.T) {
4547 Paginate : false ,
4648 Silent : false ,
4749 CacheTTL : 0 ,
50+ Template : "" ,
4851 },
4952 wantsErr : false ,
5053 },
@@ -64,6 +67,7 @@ func Test_NewCmdApi(t *testing.T) {
6467 Paginate : false ,
6568 Silent : false ,
6669 CacheTTL : 0 ,
70+ Template : "" ,
6771 },
6872 wantsErr : false ,
6973 },
@@ -83,6 +87,7 @@ func Test_NewCmdApi(t *testing.T) {
8387 Paginate : false ,
8488 Silent : false ,
8589 CacheTTL : 0 ,
90+ Template : "" ,
8691 },
8792 wantsErr : false ,
8893 },
@@ -102,6 +107,7 @@ func Test_NewCmdApi(t *testing.T) {
102107 Paginate : false ,
103108 Silent : false ,
104109 CacheTTL : 0 ,
110+ Template : "" ,
105111 },
106112 wantsErr : false ,
107113 },
@@ -121,6 +127,7 @@ func Test_NewCmdApi(t *testing.T) {
121127 Paginate : true ,
122128 Silent : false ,
123129 CacheTTL : 0 ,
130+ Template : "" ,
124131 },
125132 wantsErr : false ,
126133 },
@@ -140,6 +147,7 @@ func Test_NewCmdApi(t *testing.T) {
140147 Paginate : false ,
141148 Silent : true ,
142149 CacheTTL : 0 ,
150+ Template : "" ,
143151 },
144152 wantsErr : false ,
145153 },
@@ -164,6 +172,7 @@ func Test_NewCmdApi(t *testing.T) {
164172 Paginate : true ,
165173 Silent : false ,
166174 CacheTTL : 0 ,
175+ Template : "" ,
167176 },
168177 wantsErr : false ,
169178 },
@@ -188,6 +197,7 @@ func Test_NewCmdApi(t *testing.T) {
188197 Paginate : false ,
189198 Silent : false ,
190199 CacheTTL : 0 ,
200+ Template : "" ,
191201 },
192202 wantsErr : false ,
193203 },
@@ -212,6 +222,7 @@ func Test_NewCmdApi(t *testing.T) {
212222 Paginate : false ,
213223 Silent : false ,
214224 CacheTTL : 0 ,
225+ Template : "" ,
215226 },
216227 wantsErr : false ,
217228 },
@@ -231,6 +242,27 @@ func Test_NewCmdApi(t *testing.T) {
231242 Paginate : false ,
232243 Silent : false ,
233244 CacheTTL : time .Minute * 5 ,
245+ Template : "" ,
246+ },
247+ wantsErr : false ,
248+ },
249+ {
250+ name : "with template" ,
251+ cli : "user -t 'hello {{.name}}'" ,
252+ wants : ApiOptions {
253+ Hostname : "" ,
254+ RequestMethod : "GET" ,
255+ RequestMethodPassed : false ,
256+ RequestPath : "user" ,
257+ RequestInputFile : "" ,
258+ RawFields : []string (nil ),
259+ MagicFields : []string (nil ),
260+ RequestHeaders : []string (nil ),
261+ ShowResponseHeaders : false ,
262+ Paginate : false ,
263+ Silent : false ,
264+ CacheTTL : 0 ,
265+ Template : "hello {{.name}}" ,
234266 },
235267 wantsErr : false ,
236268 },
@@ -268,6 +300,7 @@ func Test_NewCmdApi(t *testing.T) {
268300 assert .Equal (t , tt .wants .Paginate , opts .Paginate )
269301 assert .Equal (t , tt .wants .Silent , opts .Silent )
270302 assert .Equal (t , tt .wants .CacheTTL , opts .CacheTTL )
303+ assert .Equal (t , tt .wants .Template , opts .Template )
271304 })
272305 }
273306}
@@ -393,6 +426,20 @@ func Test_apiRun(t *testing.T) {
393426 stdout : "HTTP/1.1 200 Okey-dokey\n Content-Type: text/plain\r \n \r \n " ,
394427 stderr : `` ,
395428 },
429+ {
430+ name : "output template" ,
431+ options : ApiOptions {
432+ Template : `{{.status}}` ,
433+ },
434+ httpResponse : & http.Response {
435+ StatusCode : 200 ,
436+ Body : ioutil .NopCloser (bytes .NewBufferString (`{"status":"not a cat"}` )),
437+ Header : http.Header {"Content-Type" : []string {"application/json" }},
438+ },
439+ err : nil ,
440+ stdout : "not a cat" ,
441+ stderr : `` ,
442+ },
396443 }
397444
398445 for _ , tt := range tests {
@@ -936,3 +983,40 @@ func Test_previewNamesToMIMETypes(t *testing.T) {
936983 })
937984 }
938985}
986+
987+ func Test_processResponse_template (t * testing.T ) {
988+ io , _ , stdout , stderr := iostreams .Test ()
989+
990+ resp := http.Response {
991+ StatusCode : 200 ,
992+ Header : map [string ][]string {
993+ "Content-Type" : {"application/json" },
994+ },
995+ Body : ioutil .NopCloser (strings .NewReader (`[
996+ {
997+ "title": "First title",
998+ "labels": [{"name":"bug"}, {"name":"help wanted"}]
999+ },
1000+ {
1001+ "title": "Second but not last"
1002+ },
1003+ {
1004+ "title": "Alas, tis' the end",
1005+ "labels": [{}, {"name":"feature"}]
1006+ }
1007+ ]` )),
1008+ }
1009+
1010+ _ , err := processResponse (& resp , & ApiOptions {
1011+ IO : io ,
1012+ Template : `{{range .}}{{.title}} ({{.labels | pluck "name" | join ", " }}){{"\n"}}{{end}}` ,
1013+ }, ioutil .Discard )
1014+ require .NoError (t , err )
1015+
1016+ assert .Equal (t , heredoc .Doc (`
1017+ First title (bug, help wanted)
1018+ Second but not last ()
1019+ Alas, tis' the end (, feature)
1020+ ` ), stdout .String ())
1021+ assert .Equal (t , "" , stderr .String ())
1022+ }
0 commit comments