-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
120 lines (100 loc) · 2.54 KB
/
profile.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
// Copyright (c) 2016, Hotolab. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cov
import (
"bytes"
"errors"
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"sync"
log "github.com/Sirupsen/logrus"
)
const (
coverMode = "count"
)
// processPackage executes go test command with coverage and outputs
// errors and output into channels so they are combined later in a single
// file and passed to cov for getting the expected JSON output
func processPackage(rel string) (string, error) {
// Create temporary file to output the file coverage
// this file is trashed after processing
tmp, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
defer os.Remove(tmp.Name())
log.Debugf("go test -covermode=%s -coverprofile=%s %s", coverMode, tmp.Name(), rel)
_, err = exec.Command("go", "test", "-covermode="+coverMode, "-coverprofile="+tmp.Name(), rel).CombinedOutput()
if err != nil {
return "", nil
}
// Get file contents
b, err := ioutil.ReadFile(tmp.Name())
if err != nil {
return "", err
}
return string(b), nil
}
// lookupTestFiles crawls the filesystem from the repository path
// and finds test files using glob, if a package doesn't have tests
// it is automatically skipped.
func createProfile() (*os.File, error) {
pkgs, err := packageList("ImportPath")
if err != nil {
return nil, err
}
file, err := ioutil.TempFile("", "hotolab-coverage")
if err != nil {
return nil, err
}
// Bufferize channel
tasks := make(chan string, 64)
var (
wg sync.WaitGroup
errBuff bytes.Buffer
outBuff bytes.Buffer
)
// Create as much threads as we have CPUs
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go func() {
for pkg := range tasks {
res, err := processPackage(pkg)
if err != nil {
errBuff.WriteString(err.Error())
return
}
outBuff.WriteString(res)
}
wg.Done()
}()
}
for _, pkg := range pkgs {
tasks <- pkg
}
// Close worker channel
close(tasks)
// Wait for the workers to finish
wg.Wait()
// Get errors (if any) and convert them to a runner error
errs := errBuff.String()
if errs != "" {
return nil, errors.New(errs)
}
// Get content of the buffer and write it
// to the temp file attached to the runner
out := outBuff.String()
out = regexp.MustCompile("mode: [a-z]+\n").ReplaceAllString(out, "")
out = "mode: " + coverMode + "\n" + out
log.Debug(out)
err = ioutil.WriteFile(file.Name(), []byte(out), 0644)
if err != nil {
return nil, err
}
return file, nil
}