Skip to content

Commit

Permalink
[CI-2433] Send test report category (#192)
Browse files Browse the repository at this point in the history
* Send test report category

* Fix linting issues

* Fix another linter issue
  • Loading branch information
tothszabi authored Feb 1, 2024
1 parent a74fc7a commit 379ed5c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
5 changes: 3 additions & 2 deletions report/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package api

// CreateReportParameters ...
type CreateReportParameters struct {
Title string `json:"title"`
Assets []CreateReportAsset `json:"assets"`
Title string `json:"title"`
Category string `json:"category,omitempty"`
Assets []CreateReportAsset `json:"assets"`
}

// CreateReportAsset ...
Expand Down
16 changes: 15 additions & 1 deletion report/collect.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package report

import (
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
)

const (
htmlReportInfoFile = "report-info.json"
)

func collectReports(dir string) ([]Report, error) {
var reports []Report

Expand All @@ -28,7 +34,7 @@ func collectReports(dir string) ([]Report, error) {
return err
}

if d.IsDir() || d.Name() == ".DS_Store" {
if d.IsDir() || d.Name() == ".DS_Store" || d.Name() == htmlReportInfoFile {
return nil
}

Expand Down Expand Up @@ -61,8 +67,16 @@ func collectReports(dir string) ([]Report, error) {
continue
}

var reportInfo Info
if infoFileData, err := os.ReadFile(filepath.Join(testDir, htmlReportInfoFile)); err == nil {
if err := json.Unmarshal(infoFileData, &reportInfo); err != nil {
return nil, fmt.Errorf("cannot parse report info file: %w", err)
}
}

reports = append(reports, Report{
Name: entry.Name(),
Info: reportInfo,
Assets: assets,
})
}
Expand Down
6 changes: 6 additions & 0 deletions report/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package report
// Report ...
type Report struct {
Name string
Info Info
Assets []Asset
}

Expand All @@ -19,3 +20,8 @@ type ServerReport struct {
Identifier string
AssetURLs map[string]string
}

// Info ...
type Info struct {
Category string `json:"category"`
}
5 changes: 3 additions & 2 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func (h *HTMLReportUploader) createReport(report Report) (ServerReport, error) {
}

resp, err := h.client.CreateReport(api.CreateReportParameters{
Title: report.Name,
Assets: assets,
Title: report.Name,
Category: report.Info.Category,
Assets: assets,
})
if err != nil {
return ServerReport{}, err
Expand Down
32 changes: 30 additions & 2 deletions report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -27,6 +28,7 @@ func TestFindsAndUploadsReports(t *testing.T) {
mockClient := mocks.NewClientAPI(t)
setupMockingForReport(mockClient, reports[0])
setupMockingForReport(mockClient, reports[2])
setupMockingForReport(mockClient, reports[3])

uploader := HTMLReportUploader{
client: mockClient,
Expand Down Expand Up @@ -79,6 +81,19 @@ func createReports(t *testing.T) (string, []Report) {
},
},
},
{
"name": "Test D",
"info": map[string]string{
"category": "random",
},
"assets": []map[string]string{
{
"name": "index.html",
"size": "1",
"type": "text/plain; charset=utf-8",
},
},
},
}

var reports []Report
Expand Down Expand Up @@ -135,8 +150,20 @@ func createReports(t *testing.T) (string, []Report) {
})
}

var reportInfo Info
if reportInfoRaw, ok := data["info"].(map[string]string); ok {
reportInfoData, err := json.Marshal(reportInfoRaw)
require.NoError(t, err)

reportInfoFile := filepath.Join(reportPath, htmlReportInfoFile)
require.NoError(t, os.WriteFile(reportInfoFile, reportInfoData, 0755))

require.NoError(t, json.Unmarshal(reportInfoData, &reportInfo))
}

reports = append(reports, Report{
Name: reportName,
Info: reportInfo,
Assets: assets,
})
}
Expand All @@ -160,8 +187,9 @@ func setupMockingForReport(client *mocks.ClientAPI, report Report) {
})
}
requestParams := api.CreateReportParameters{
Title: report.Name,
Assets: requestAssets,
Title: report.Name,
Category: report.Info.Category,
Assets: requestAssets,
}

response := api.CreateReportResponse{
Expand Down

0 comments on commit 379ed5c

Please sign in to comment.