@@ -80,9 +80,10 @@ func httpCall(apiToken, method, url string, input io.Reader, output interface{})
8080 if resp .StatusCode < 200 || 299 < resp .StatusCode {
8181 bodyData , err := ioutil .ReadAll (resp .Body )
8282 if err != nil {
83- return fmt .Errorf ("unsuccessful response code: %d and failed to read body, error: %s" , resp .StatusCode , err )
83+ log .Warnf ("Failed to read response: %s" , err )
84+ return fmt .Errorf ("unsuccessful status code: %d" , resp .StatusCode )
8485 }
85- return fmt .Errorf ("unsuccessful response code: %d\n body: \n %s" , resp .StatusCode , bodyData )
86+ return fmt .Errorf ("unsuccessful status code: %d, response: %s" , resp .StatusCode , bodyData )
8687 }
8788
8889 if output != nil {
@@ -103,7 +104,29 @@ func findImages(testDir string) (imageFilePaths []string) {
103104 return
104105}
105106
106- // ParseTestResults ...
107+ /*
108+ ParseTestResults walks through the Test Deploy directory and parses all the Steps' test results.
109+
110+ The Test Deploy directory has the following directory structure:
111+
112+ test_results ($BITRISE_TEST_DEPLOY_DIR)
113+ ├── step_1_test_results ($BITRISE_TEST_RESULT_DIR)
114+ │ ├── step-info.json
115+ │ ├── test_run_1
116+ │ │ ├── UnitTest.xml
117+ │ │ └── test-info.json
118+ │ └── test_run_2
119+ │ ├── UITest.xml
120+ │ └── test-info.json
121+ └── step_2_test_results ($BITRISE_TEST_RESULT_DIR)
122+ ├── step-info.json
123+ └── test_run
124+ ├── results.xml
125+ ├── screenshot_1.jpg
126+ ├── screenshot_2.jpeg
127+ ├── screenshot_3.png
128+ └── test-info.json
129+ */
107130func ParseTestResults (testsRootDir string ) (results Results , err error ) {
108131 // read dirs in base tests dir
109132 // <root_tests_dir>
@@ -219,15 +242,7 @@ func ParseTestResults(testsRootDir string) (results Results, err error) {
219242// Upload ...
220243func (results Results ) Upload (apiToken , endpointBaseURL , appSlug , buildSlug string ) error {
221244 for _ , result := range results {
222- if len (result .ImagePaths ) > 0 {
223- log .Printf ("Uploading: %s with attachments:" , result .Name )
224- for _ , pth := range result .ImagePaths {
225- log .Printf ("- %s" , pth )
226- }
227- log .Printf ("" )
228- } else {
229- log .Printf ("Uploading: %s" , result .Name )
230- }
245+ log .Printf ("Uploading: %s" , result .Name )
231246
232247 uploadReq := UploadRequest {
233248 FileInfo : FileInfo {
@@ -240,7 +255,7 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
240255 for _ , asset := range result .ImagePaths {
241256 fi , err := os .Stat (asset )
242257 if err != nil {
243- return err
258+ return fmt . Errorf ( "failed to get file info for %s: %w" , asset , err )
244259 }
245260 uploadReq .Assets = append (uploadReq .Assets , FileInfo {
246261 FileName : filepath .Base (asset ),
@@ -250,30 +265,30 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
250265
251266 uploadRequestBodyData , err := json .Marshal (uploadReq )
252267 if err != nil {
253- return err
268+ return fmt . Errorf ( "failed to json encode upload request: %w" , err )
254269 }
255270
256271 var (
257272 uploadResponse UploadResponse
258273 uploadRequestURL = fmt .Sprintf ("%s/apps/%s/builds/%s/test_reports" , endpointBaseURL , appSlug , buildSlug )
259274 )
260275 if err := httpCall (apiToken , http .MethodPost , uploadRequestURL , bytes .NewReader (uploadRequestBodyData ), & uploadResponse ); err != nil {
261- return err
276+ return fmt . Errorf ( "failed to initialise test result: %w" , err )
262277 }
263278
264279 if err := httpCall ("" , http .MethodPut , uploadResponse .URL , bytes .NewReader (result .XMLContent ), nil ); err != nil {
265- return err
280+ return fmt . Errorf ( "failed to upload test result xml: %w" , err )
266281 }
267282
268283 for _ , upload := range uploadResponse .Assets {
269284 for _ , file := range result .ImagePaths {
270285 if filepath .Base (file ) == upload .FileName {
271286 fi , err := os .Open (file )
272287 if err != nil {
273- return err
288+ return fmt . Errorf ( "failed to open test result attachment (%s): %w" , file , err )
274289 }
275290 if err := httpCall ("" , http .MethodPut , upload .URL , fi , nil ); err != nil {
276- return err
291+ return fmt . Errorf ( "failed to upload test result attachment (%s): %w" , file , err )
277292 }
278293 break
279294 }
@@ -282,7 +297,7 @@ func (results Results) Upload(apiToken, endpointBaseURL, appSlug, buildSlug stri
282297
283298 var uploadPatchURL = fmt .Sprintf ("%s/apps/%s/builds/%s/test_reports/%s" , endpointBaseURL , appSlug , buildSlug , uploadResponse .ID )
284299 if err := httpCall (apiToken , http .MethodPatch , uploadPatchURL , strings .NewReader (`{"uploaded":true}` ), nil ); err != nil {
285- return err
300+ return fmt . Errorf ( "failed to finalise test result: %w" , err )
286301 }
287302 }
288303
0 commit comments