|
| 1 | +package coverage |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/xml" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// New creates a new lcov parser |
| 15 | +func New(filePath string, mode CoverageMode) Parser { |
| 16 | + return Parser{ |
| 17 | + path: filePath, |
| 18 | + mode: mode, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Parse parses the lcov file |
| 23 | +func (l Parser) Parse() (Report, error) { |
| 24 | + switch l.mode { |
| 25 | + case LCOV: |
| 26 | + return l.processLcov() |
| 27 | + case COBERTURA: |
| 28 | + return l.processCobertura() |
| 29 | + } |
| 30 | + return Report{}, fmt.Errorf("coverage.parse> Unknown mode %s", l.mode) |
| 31 | +} |
| 32 | + |
| 33 | +func (l Parser) processCobertura() (Report, error) { |
| 34 | + file, errF := os.Open(l.path) |
| 35 | + if errF != nil { |
| 36 | + return Report{}, fmt.Errorf("coverage.processCobertura> Unable to open file: %v", errF) |
| 37 | + } |
| 38 | + defer file.Close() |
| 39 | + |
| 40 | + b, errR := ioutil.ReadAll(file) |
| 41 | + if errR != nil { |
| 42 | + return Report{}, fmt.Errorf("coverage.processCobertura> Unable to read file: %v", errR) |
| 43 | + } |
| 44 | + |
| 45 | + var cobReport CoberturaCoverage |
| 46 | + if err := xml.Unmarshal(b, &cobReport); err != nil { |
| 47 | + return Report{}, fmt.Errorf("coverage.processCobertura> Unable to unmarshal content: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + report := Report{ |
| 51 | + TotalLines: getInt(cobReport.LinesValid), |
| 52 | + TotalBranches: getInt(cobReport.BranchesValid), |
| 53 | + CoveredLines: getInt(cobReport.LinesCovered), |
| 54 | + CoveredBranches: getInt(cobReport.BranchesCovered), |
| 55 | + } |
| 56 | + return report, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (l Parser) processLcov() (Report, error) { |
| 60 | + file, errF := os.Open(l.path) |
| 61 | + if errF != nil { |
| 62 | + return Report{}, fmt.Errorf("coverage.processLcov> Unable to open lcov file: %v", errF) |
| 63 | + } |
| 64 | + defer file.Close() |
| 65 | + |
| 66 | + r := bufio.NewReader(file) |
| 67 | + |
| 68 | + report := Report{ |
| 69 | + Files: make([]FileReport, 0), |
| 70 | + } |
| 71 | + fileReport := FileReport{} |
| 72 | + for { |
| 73 | + line, errR := r.ReadString('\n') |
| 74 | + if errR != nil && errR != io.EOF { |
| 75 | + return report, fmt.Errorf("coverage.processLcov> Unable to read line: %v", errR) |
| 76 | + } |
| 77 | + if errR == io.EOF { |
| 78 | + break |
| 79 | + } |
| 80 | + line = strings.Replace(line, "\n", "", -1) |
| 81 | + |
| 82 | + // Test new file |
| 83 | + if strings.HasPrefix(line, "SF:") { |
| 84 | + if fileReport.Path != "" { |
| 85 | + report.Files = append(report.Files, fileReport) |
| 86 | + } |
| 87 | + fileReport = FileReport{ |
| 88 | + Path: strings.Replace(line, "SF:", "", 1), |
| 89 | + } |
| 90 | + } else { |
| 91 | + l.processLcovLine(line, &report, &fileReport) |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + return report, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (l Parser) processLcovLine(line string, report *Report, fileReport *FileReport) { |
| 99 | + switch { |
| 100 | + case strings.HasPrefix(line, "FNF:"): |
| 101 | + nb := getInt(strings.Replace(line, "FNF:", "", -1)) |
| 102 | + fileReport.TotalFunctions = nb |
| 103 | + report.TotalFunctions += nb |
| 104 | + case strings.HasPrefix(line, "FNH:"): |
| 105 | + nb := getInt(strings.Replace(line, "FNH:", "", -1)) |
| 106 | + fileReport.CoveredFunctions = nb |
| 107 | + report.CoveredFunctions += nb |
| 108 | + case strings.HasPrefix(line, "BRF:"): |
| 109 | + nb := getInt(strings.Replace(line, "BRF:", "", -1)) |
| 110 | + fileReport.TotalBranches = nb |
| 111 | + report.TotalBranches += nb |
| 112 | + case strings.HasPrefix(line, "BRH:"): |
| 113 | + nb := getInt(strings.Replace(line, "BRH:", "", -1)) |
| 114 | + fileReport.CoveredBranches = nb |
| 115 | + report.CoveredBranches += nb |
| 116 | + case strings.HasPrefix(line, "LF:"): |
| 117 | + nb := getInt(strings.Replace(line, "LF:", "", -1)) |
| 118 | + fileReport.TotalLines = nb |
| 119 | + report.TotalLines += nb |
| 120 | + case strings.HasPrefix(line, "LH:"): |
| 121 | + nb := getInt(strings.Replace(line, "LH:", "", -1)) |
| 122 | + fileReport.CoveredLines = nb |
| 123 | + report.CoveredLines += nb |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func getInt(s string) int { |
| 128 | + i := 0 |
| 129 | + i, _ = strconv.Atoi(s) |
| 130 | + return i |
| 131 | +} |
0 commit comments