11package cmd
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "fmt"
57 "io/ioutil"
68 "net/http"
@@ -24,6 +26,8 @@ func TestDownloadWithoutToken(t *testing.T) {
2426 err := runDownload (cfg , pflag .NewFlagSet ("fake" , pflag .PanicOnError ), []string {})
2527 if assert .Error (t , err ) {
2628 assert .Regexp (t , "Welcome to Exercism" , err .Error ())
29+ // It uses the default base API url to infer the host
30+ assert .Regexp (t , "exercism.io/my/settings" , err .Error ())
2731 }
2832}
2933
@@ -84,17 +88,30 @@ func TestDownload(t *testing.T) {
8488 }()
8589
8690 testCases := []struct {
87- requestor string
88- expectedDir string
89- flag , flagValue string
91+ requestor string
92+ expectedDir string
93+ flags map [ string ] string
9094 }{
91- {requestorSelf , "" , "exercise" , "bogus-exercise" },
92- {requestorSelf , "" , "uuid" , "bogus-id" },
93- {requestorOther , filepath .Join ("users" , "alice" ), "uuid" , "bogus-id" },
95+ {
96+ requestor : requestorSelf ,
97+ expectedDir : "" ,
98+ flags : map [string ]string {"exercise" : "bogus-exercise" },
99+ },
100+ {
101+ requestor : requestorSelf ,
102+ expectedDir : "" ,
103+ flags : map [string ]string {"uuid" : "bogus-id" },
104+ },
105+ {
106+ requestor : requestorOther ,
107+ expectedDir : filepath .Join ("users" , "alice" ),
108+ flags : map [string ]string {"uuid" : "bogus-id" },
109+ },
94110 }
95111
96112 for _ , tc := range testCases {
97113 tmpDir , err := ioutil .TempDir ("" , "download-cmd" )
114+ defer os .RemoveAll (tmpDir )
98115 assert .NoError (t , err )
99116
100117 ts := fakeDownloadServer (tc .requestor )
@@ -110,12 +127,32 @@ func TestDownload(t *testing.T) {
110127 }
111128 flags := pflag .NewFlagSet ("fake" , pflag .PanicOnError )
112129 setupDownloadFlags (flags )
113- flags .Set (tc .flag , tc .flagValue )
130+ for name , value := range tc .flags {
131+ flags .Set (name , value )
132+ }
114133
115134 err = runDownload (cfg , flags , []string {})
116135 assert .NoError (t , err )
117136
118- assertDownloadedCorrectFiles (t , filepath .Join (tmpDir , tc .expectedDir ), tc .requestor )
137+ targetDir := filepath .Join (tmpDir , tc .expectedDir )
138+ assertDownloadedCorrectFiles (t , targetDir , tc .requestor )
139+
140+ metadata := `{
141+ "track": "bogus-track",
142+ "exercise":"bogus-exercise",
143+ "id":"bogus-id",
144+ "url":"",
145+ "handle":"alice",
146+ "is_requester":%s,
147+ "auto_approve":false
148+ }`
149+ metadata = fmt .Sprintf (metadata , tc .requestor )
150+ metadata = compact (t , metadata )
151+
152+ path := filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , ws .SolutionMetadataFilepath ())
153+ b , err := ioutil .ReadFile (path )
154+ assert .NoError (t , err )
155+ assert .Equal (t , metadata , string (b ), "the solution metadata file" )
119156 }
120157}
121158
@@ -150,7 +187,6 @@ func fakeDownloadServer(requestor string) *httptest.Server {
150187}
151188
152189func assertDownloadedCorrectFiles (t * testing.T , targetDir , requestor string ) {
153- metadata := `{"track":"bogus-track","exercise":"bogus-exercise","id":"bogus-id","url":"","handle":"alice","is_requester":%s,"auto_approve":false}`
154190 expectedFiles := []struct {
155191 desc string
156192 path string
@@ -166,11 +202,6 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir, requestor string) {
166202 path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "subdir" , "file-2.txt" ),
167203 contents : "this is file 2" ,
168204 },
169- {
170- desc : "the solution metadata file" ,
171- path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , ws .SolutionMetadataFilepath ()),
172- contents : fmt .Sprintf (metadata , requestor ),
173- },
174205 }
175206
176207 for _ , file := range expectedFiles {
@@ -218,3 +249,10 @@ const payloadTemplate = `
218249 }
219250}
220251`
252+
253+ func compact (t * testing.T , s string ) string {
254+ buffer := new (bytes.Buffer )
255+ err := json .Compact (buffer , []byte (s ))
256+ assert .NoError (t , err )
257+ return buffer .String ()
258+ }
0 commit comments