Skip to content

Commit

Permalink
Filter invalid reports (#197)
Browse files Browse the repository at this point in the history
* Filter invalid reports

* Check for exact match
  • Loading branch information
tothszabi authored Mar 1, 2024
1 parent 2a49db6 commit 9e8abfd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
43 changes: 39 additions & 4 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report

import (
"fmt"
"path/filepath"
"sync"

"github.com/bitrise-io/go-utils/v2/log"
Expand Down Expand Up @@ -40,14 +41,48 @@ func (h *HTMLReportUploader) DeployReports() []error {
h.logger.Printf("- %s", report.Name)
}

var errors []error
for _, report := range reports {
validatedReports, validationErrors := h.validate(reports)
if len(validationErrors) != 0 {
h.logger.Warnf("Validation errors:\n")

for _, validationError := range validationErrors {
h.logger.Warnf("- %s\n", validationError)
}
}

var uploadErrors []error
for _, report := range validatedReports {
if err := h.uploadReport(report); err != nil {
errors = append(errors, err)
uploadErrors = append(uploadErrors, err)
}
}

return errors
return uploadErrors
}

func (h *HTMLReportUploader) validate(reports []Report) ([]Report, []error) {
var validatedReports []Report
var validationErrors []error

for _, report := range reports {
valid := false

for _, asset := range report.Assets {
if filepath.Base(asset.Path) == "index.html" {
valid = true
break
}
}

if valid {
validatedReports = append(validatedReports, report)
continue
}

validationErrors = append(validationErrors, fmt.Errorf("missing index.html file for %s", report.Name))
}

return validatedReports, validationErrors
}

func (h *HTMLReportUploader) uploadReport(report Report) error {
Expand Down
24 changes: 24 additions & 0 deletions report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
loggerV2 "github.com/bitrise-io/go-utils/v2/log"
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/report/api"
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/report/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -43,6 +44,29 @@ func TestFindsAndUploadsReports(t *testing.T) {
mockClient.AssertExpectations(t)
}

func TestInvalidReportFiltering(t *testing.T) {
reportDir, reports := createReports(t)
uploader := HTMLReportUploader{
client: nil,
logger: loggerV2.NewLogger(),
reportDir: reportDir,
concurrency: 1,
}

// Create an invalid report
reports[0].Assets[2].Path = filepath.Join(filepath.Dir(reports[0].Assets[2].Path), "my_report_index.html")
reports[0].Assets[2].TestDirRelativePath = filepath.Join(filepath.Dir(reports[0].Assets[2].TestDirRelativePath), "my_report_index.html")

validatedReports, validatedErrors := uploader.validate(reports)

expectedErrors := []error{
fmt.Errorf("missing index.html file for Test A"),
fmt.Errorf("missing index.html file for Test B"),
}
assert.Equal(t, expectedErrors, validatedErrors)
assert.Equal(t, len(validatedReports), 2)
}

// createReports creates dummy data for multiple test reports.
func createReports(t *testing.T) (string, []Report) {
tempDir := t.TempDir()
Expand Down

0 comments on commit 9e8abfd

Please sign in to comment.