code-intelligence.com/cifuzz@v0.40.0/internal/cmd/coverage/summary/coverage.go (about)

     1  package summary
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/pterm/pterm"
     8  
     9  	"code-intelligence.com/cifuzz/pkg/log"
    10  	"code-intelligence.com/cifuzz/util/fileutil"
    11  )
    12  
    13  type CoverageSummary struct {
    14  	Total *Coverage
    15  	Files []*FileCoverage
    16  }
    17  
    18  type FileCoverage struct {
    19  	Filename string
    20  	Coverage *Coverage
    21  }
    22  
    23  type Coverage struct {
    24  	FunctionsFound int
    25  	FunctionsHit   int
    26  	BranchesFound  int
    27  	BranchesHit    int
    28  	LinesFound     int
    29  	LinesHit       int
    30  }
    31  
    32  func (cs *CoverageSummary) PrintTable(writer io.Writer) {
    33  	formatCell := func(hit, found int) string {
    34  		percent := 100.0
    35  		if found != 0 {
    36  			percent = (float64(hit) * 100) / float64(found)
    37  		}
    38  		return fmt.Sprintf("%d / %d %8s", hit, found, fmt.Sprintf("(%.1f%%)", percent))
    39  	}
    40  
    41  	// create table data for pterm table
    42  	tableData := pterm.TableData{{"File", "Functions Hit/Found", "Lines Hit/Found", "Branches Hit/Found"}}
    43  	for _, file := range cs.Files {
    44  		tableData = append(tableData, []string{
    45  			fileutil.PrettifyPath(file.Filename),
    46  			formatCell(file.Coverage.FunctionsHit, file.Coverage.FunctionsFound),
    47  			formatCell(file.Coverage.LinesHit, file.Coverage.LinesFound),
    48  			formatCell(file.Coverage.BranchesHit, file.Coverage.BranchesFound),
    49  		},
    50  		)
    51  	}
    52  	tableData = append(tableData, []string{"", "", "", ""})
    53  	// repeat the header for the case that the original header scrolled
    54  	// of the terminal window
    55  	tableData = append(tableData, []string{"", "Functions Hit/Found", "Lines Hit/Found", "Branches Hit/Found"})
    56  	tableData = append(tableData, []string{
    57  		"Total",
    58  		fmt.Sprintf("%d / %d", cs.Total.FunctionsHit, cs.Total.FunctionsFound),
    59  		fmt.Sprintf("%d / %d", cs.Total.LinesHit, cs.Total.LinesFound),
    60  		fmt.Sprintf("%d / %d", cs.Total.BranchesHit, cs.Total.BranchesFound),
    61  	},
    62  	)
    63  	table := pterm.DefaultTable.WithWriter(writer).WithHasHeader().WithData(tableData).WithRightAlignment()
    64  
    65  	log.Print("\n")
    66  	log.Successf("Coverage Report:\n")
    67  	if err := table.Render(); err != nil {
    68  		log.Error(err, "Unable to print coverage table")
    69  	}
    70  	log.Print("\n")
    71  }