Passed
Push — master ( 11082a...ac362d )
by Patrick D
09:46 queued 04:35
created

main.go (11 issues)

Severity
1
package main
2
3
import (
4
	"bytes"
5
	"io/ioutil"
6
	"log"
7
	"os"
8
	"os/exec"
9
	"regexp"
10
	"strings"
11
12
	"github.com/phayes/checkstyle"
13
)
14
15
var (
1 ignored issue
show
regexGitURI is unused
Loading history...
16
	regexGithub    = regexp.MustCompile("^g")
17
	regexBitbucket = regexp.MustCompile("^b")
18
	regexGitURI    = regexp.MustCompile("((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.@\\:/\\-~]+)(\\.git)(/)?")
1 ignored issue
show
should use raw string (...) with regexp.MustCompile to avoid having to escape twice (S1007)
Loading history...
var regexGitURI is unused (U1000)
Loading history...
unused variable or constant regexGitURI
Loading history...
19
)
20
21
var (
0 ignored issues
show
cstyle is unused
Loading history...
22
	cstyle                                                   = checkstyle.New()
2 ignored issues
show
var cstyle is unused (U1000)
Loading history...
unused variable or constant cstyle
Loading history...
23
	homeEnv                                                  = os.Getenv("HOME")
24
	gopathEnv                                                = os.Getenv("GOPATH")
25
	scrutinizerProjectEnv                                    = os.Getenv("SCRUTINIZER_PROJECT")
26
	projectFull, projectDomain, projectOwner, projectProject string
27
)
28
29
func main() {
30
	// Set up environment variables
31
	if gopathEnv == "" {
32
		gopathEnv = homeEnv + "/go"
33
	}
34
35
	// Set up project
36
	if len(scrutinizerProjectEnv) == 0 {
37
		log.Fatal("Not running without scrutinizer environemnt. SCRUTINIZER_PROJECT environment variable not found")
38
	}
39
40
	projectFull = regexGithub.ReplaceAllString(scrutinizerProjectEnv, "github.com")
41
	projectFull = regexBitbucket.ReplaceAllString(projectFull, "bitbucket.com")
42
43
	projectParts := strings.Split(projectFull, "/")
44
	if len(projectParts) != 3 {
45
		log.Fatal("Malformed SCRUTINIZER_PROJECT environment variable.")
46
	}
47
	projectDomain = projectParts[0]
48
	projectOwner = projectParts[1]
49
	projectProject = projectParts[2]
50
51
	// Install all dependancies
52
	cmd := exec.Command("go", "get", "-t", "./...")
53
	_, err := cmd.Output()
54
	if err != nil {
55
		exitErr, ok := err.(*exec.ExitError)
56
		if ok && len(exitErr.Stderr) != 0 {
57
			log.Println(string(exitErr.Stderr))
58
		}
59
		log.Fatal("go get -t ./...", err)
60
	}
61
62
	// Run metalinter
63
	metalinter()
64
65
	// Run tests and coverage
66
	testAndCoverage()
67
}
68
69
func metalinter() {
70
	goMetaLinterCmd := gopathEnv + "/bin/gometalinter"
71
72
	// Install gometalinter -- no-op if already installed
73
	cmd := exec.Command("go", "get", "github.com/alecthomas/gometalinter")
74
	_, err := cmd.Output()
75
	if err != nil {
76
		exitErr, ok := err.(*exec.ExitError)
77
		if ok && len(exitErr.Stderr) != 0 {
78
			log.Println(string(exitErr.Stderr))
79
		}
80
		log.Fatal("go get github.com/alecthomas/gometalinter", err)
81
	}
82
83
	// Install all gometalinter dependancies -- no-op if already installed
84
	cmd = exec.Command(goMetaLinterCmd, "--install")
85
	_, err = cmd.Output()
86
	if err != nil {
87
		exitErr, ok := err.(*exec.ExitError)
88
		if ok && len(exitErr.Stderr) != 0 {
89
			log.Println(string(exitErr.Stderr))
90
		}
91
		log.Fatal(goMetaLinterCmd, "--install", err)
92
	}
93
94
	// Configure the metalinter
95
	if _, err := os.Stat("go-scrutinize.config"); os.IsNotExist(err) {
0 ignored issues
show
declaration of "err" shadows declaration at main.go:74
Loading history...
96
		cmd = exec.Command(goMetaLinterCmd, "./...", "--checkstyle")
97
	} else {
98
		cmd = exec.Command(goMetaLinterCmd, "./...", "--checkstyle", "--config=go-scrutinize.config")
99
	}
100
101
	// Run the metalinter -- note that will return non-zero exit status
102
	out, err := cmd.Output()
103
	if err != nil {
104
		exitErr := err.(*exec.ExitError)
105
		if len(exitErr.Stderr) != 0 {
106
			log.Println(string(exitErr.Stderr))
107
		}
108
	}
109
110
	// Write the output from the metalinter
111
	ioutil.WriteFile("checkstyle_report.xml", out, os.ModePerm)
0 ignored issues
show
error return value not checked (ioutil.WriteFile("checkstyle_report.xml", out, os.ModePerm))
Loading history...
112
}
113
114
func testAndCoverage() {
115
	goConvCmd := gopathEnv + "/bin/gocov"
116
	goConvXMLCmd := gopathEnv + "/bin/gocov-xml"
117
118
	// Install the coverage tool covov
119
	cmd := exec.Command("go", "get", "github.com/axw/gocov/...")
120
	_, err := cmd.Output()
121
	if err != nil {
122
		exitErr := err.(*exec.ExitError)
123
		if len(exitErr.Stderr) != 0 {
124
			log.Println(string(exitErr.Stderr))
125
		}
126
		log.Fatal("go", "get", "github.com/axw/gocov/...", err)
127
	}
128
129
	// Install the coverage file translation tool gocov-xml
130
	cmd = exec.Command("go", "get", "github.com/AlekSi/gocov-xml")
131
	_, err = cmd.Output()
132
	if err != nil {
133
		exitErr := err.(*exec.ExitError)
134
		if len(exitErr.Stderr) != 0 {
135
			log.Println(string(exitErr.Stderr))
136
		}
137
		log.Fatal("go", "get", "github.com/AlekSi/gocov-xml", err)
138
	}
139
140
	// Run tests with coverage
141
	cmd = exec.Command(goConvCmd, "test", "./...", "-race")
142
	gocovout, err := cmd.Output()
143
	if err != nil {
144
		exitErr := err.(*exec.ExitError)
145
		if len(exitErr.Stderr) != 0 {
146
			log.Println(string(exitErr.Stderr))
147
		}
148
		log.Fatal(goConvCmd, "test", "./...", "-race", err)
149
	}
150
151
	// Convert to clover format
152
	cmd = exec.Command(goConvXMLCmd)
153
	cmd.Stdin = bytes.NewReader(gocovout)
154
	xmlout, err := cmd.Output()
155
	if err != nil {
156
		exitErr := err.(*exec.ExitError)
157
		if len(exitErr.Stderr) != 0 {
158
			log.Println(string(exitErr.Stderr))
159
		}
160
		log.Fatal(goConvXMLCmd, err)
161
	}
162
163
	// Rewrite all filenames to use /home/scrutinizer/build paths
164
	coveragexml := strings.Replace(string(xmlout), gopathEnv+"/src/"+projectFull, "/home/scrutinizer/build", 0)
0 ignored issues
show
calling strings.Replace with n == 0 will return no results, did you mean -1? (SA1018)
Loading history...
165
166
	// Write the output from the metalinter
167
	ioutil.WriteFile("coverage.xml", []byte(coveragexml), os.ModePerm)
0 ignored issues
show
error return value not checked (ioutil.WriteFile("coverage.xml", []byte(coveragexml), os.ModePerm))
Loading history...
168
169
}
170