|
| 1 | +package reportmaker |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" // for embedding report template |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "html/template" |
| 9 | + "math" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/avito-tech/go-mutesting/internal/models" |
| 14 | +) |
| 15 | + |
| 16 | +//go:embed templates/report.html.gotpl |
| 17 | +var reportTmpl string |
| 18 | + |
| 19 | +var funcMap = template.FuncMap{ |
| 20 | + "splitDiff": func(diff string) []string { |
| 21 | + return strings.Split(diff, "\n") |
| 22 | + }, |
| 23 | + "hasPrefix": strings.HasPrefix, |
| 24 | +} |
| 25 | + |
| 26 | +// MakeHTMLReport is a function for creating an HTML report based on a stripped-down version of the models.Report model (not all fields are used) |
| 27 | +func MakeHTMLReport(report models.Report) error { |
| 28 | + // MSI in percent |
| 29 | + report.Stats.Msi = math.Round(report.Stats.Msi*10_000) / 100 |
| 30 | + groupedMutants := groupEscapedMutants(report.Escaped) |
| 31 | + |
| 32 | + t, err := template.New(models.ReportHTMLFileName).Funcs(funcMap).Parse(reportTmpl) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("Error while parse template: %w ", err) |
| 35 | + } |
| 36 | + |
| 37 | + file, err := createOrTruncateReportFile(models.ReportHTMLFileName) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("Error while open/create .html report file from template: %w ", err) |
| 40 | + } |
| 41 | + defer closeReportFile(file, models.ReportHTMLFileName) |
| 42 | + |
| 43 | + data := struct { |
| 44 | + Stats models.Stats |
| 45 | + GroupedMutants map[string][]models.Mutant |
| 46 | + }{ |
| 47 | + Stats: report.Stats, |
| 48 | + GroupedMutants: groupedMutants, |
| 49 | + } |
| 50 | + |
| 51 | + err = t.Execute(file, data) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("Error while execute template for .html report: %w ", err) |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// MakeJSONReport is a function for creating json report, which is based on models.Report |
| 60 | +func MakeJSONReport(report models.Report) error { |
| 61 | + jsonContent, err := json.Marshal(report) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + file, err := createOrTruncateReportFile(models.ReportFileName) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("Error while open/create .json report file from template: %w ", err) |
| 69 | + } |
| 70 | + defer closeReportFile(file, models.ReportFileName) |
| 71 | + |
| 72 | + if file == nil { |
| 73 | + return errors.New("cannot create file for .json report") |
| 74 | + } |
| 75 | + |
| 76 | + _, err = file.WriteString(string(jsonContent)) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func createOrTruncateReportFile(filename string) (*os.File, error) { |
| 85 | + return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) |
| 86 | +} |
| 87 | + |
| 88 | +func closeReportFile(file *os.File, filename string) { |
| 89 | + if err := file.Close(); err != nil { |
| 90 | + fmt.Printf("Error while closing %s: %v\n", filename, err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func groupEscapedMutants(escaped []models.Mutant) map[string][]models.Mutant { |
| 95 | + if len(escaped) == 0 { |
| 96 | + return make(map[string][]models.Mutant) |
| 97 | + } |
| 98 | + |
| 99 | + mutantCount := make(map[string]int) |
| 100 | + for _, mutant := range escaped { |
| 101 | + filePath := mutant.Mutator.OriginalFilePath |
| 102 | + mutantCount[filePath]++ |
| 103 | + } |
| 104 | + |
| 105 | + groupedMutants := make(map[string][]models.Mutant, len(mutantCount)) |
| 106 | + for filePath, count := range mutantCount { |
| 107 | + groupedMutants[filePath] = make([]models.Mutant, 0, count) |
| 108 | + } |
| 109 | + |
| 110 | + for _, mutant := range escaped { |
| 111 | + filePath := mutant.Mutator.OriginalFilePath |
| 112 | + groupedMutants[filePath] = append(groupedMutants[filePath], mutant) |
| 113 | + } |
| 114 | + |
| 115 | + return groupedMutants |
| 116 | +} |
0 commit comments