forked from mainephd/sonago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (114 loc) · 3.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bufio"
"encoding/xml"
"flag"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"strings"
)
const (
// CoverageFileDelimiter - Delimiter used in Coverage Metrics
CoverageFileDelimiter = ":"
// CoverageLineAndColumnDelimiter - Delimiter used between line number and column number
CoverageLineAndColumnDelimiter = "."
// CoverageStartAndEndLineDelimiter - Delimiter used between start line number and end line number
CoverageStartAndEndLineDelimiter = ","
// CoveredAndUnCoveredLinesDelimiter - Delimiter used between covered lines and uncovered lines
CoveredAndUnCoveredLinesDelimiter = " "
// SonarSchemaVersion dictates the version of the sonar generic code coverage schema
SonarSchemaVersion = 1
)
func check(e error) {
if e != nil {
panic(e)
}
}
var outputFile string
var inputFile string
func init() {
flag.StringVar(&outputFile, "outputfile", "coverage.xml", "File name to write output to.")
flag.StringVar(&inputFile, "inputfile", "gover.coverprofile", "File name to read coverage profile from.")
}
func main() {
flag.Parse()
file, err := os.Open(inputFile)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
fileArr := processCoverageData(scanner)
cov := &coverage{}
cov.File = fileArr
cov.Version = SonarSchemaVersion
xmlForm, marshalError := xml.Marshal(cov)
check(marshalError)
writeError := ioutil.WriteFile(outputFile, xmlForm, 0644)
check(writeError)
}
func processCoverageData(scanner *bufio.Scanner) []File {
fileMap := make(map[string][]LineToCover)
for scanner.Scan() {
data := scanner.Text()
cover := strings.Split(data, CoverageFileDelimiter)
if cover[0] != "mode" {
filePath := trimFilePath(cover[0])
if fileMap[filePath] == nil { // New FilePath Entry is found
fileMap[filePath] = splitStartAndEndLineNumbers(cover[1])
} else { // Old FilePath Entry...just append covered lines
fileMap[filePath] = append(fileMap[filePath], splitStartAndEndLineNumbers(cover[1])...)
}
}
}
returnFileArrays := make([]File, 0)
// Loop over the map.
for file, linesToCover := range fileMap {
returnFileArrays = append(returnFileArrays, File{
Path: file,
LineToCover: linesToCover,
})
}
return returnFileArrays
}
func trimFilePath(filePath string) string {
pwd, err := os.Getwd()
if err != nil {
log.Printf("Error while retrieving PWD, error: %s", err)
return filePath
}
dirPath := path.Dir(filePath)
if strings.HasSuffix(pwd, dirPath) {
// filePath=github.com/houdini/app-code/util.go, returns util.go
return path.Base(filePath)
}
// filePath=github.com/houdini/app-code/data/util.go, returns data/util.go
base := path.Base(pwd)
start := strings.Index(filePath, base)
return filePath[start+len(base)+1:]
}
func splitStartAndEndLineNumbers(startAndEndLines string) []LineToCover {
lToCover := make([]LineToCover, 0)
covered := false
startAndEndLineNumbers := strings.Split(startAndEndLines, CoverageStartAndEndLineDelimiter)
coveredAndUnCoveredLines := strings.Split(startAndEndLineNumbers[1], CoveredAndUnCoveredLinesDelimiter)
startLine := fetchLineFromLineAndColumn(startAndEndLineNumbers[0])
endLine := fetchLineFromLineAndColumn(coveredAndUnCoveredLines[0])
if coveredAndUnCoveredLines[2] == "1" {
covered = true
}
for i := startLine; i <= endLine; i++ {
lToCover = append(lToCover, LineToCover{
LineNumber: i,
Covered: covered,
})
}
return lToCover
}
func fetchLineFromLineAndColumn(lineWithColumn string) (lineNumber int) {
lineAndColumn := strings.Split(lineWithColumn, CoverageLineAndColumnDelimiter)
i, err := strconv.Atoi(lineAndColumn[0])
check(err)
return i
}