Passed
Push — master ( 5f603a...137765 )
by Patrick D
02:58
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 environment. 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 dependencies
52
	cmd := exec.Command("go", "get", "-t", "./...")
53
	output, 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.Println(output)
60
		log.Fatal("go get -t ./...", err)
61
	}
62
63
	// Run metalinter
64
	metalinter()
65
66
	// Run tests and coverage
67
	testAndCoverage()
68
}
69
70
func metalinter() {
71
	goMetaLinterCmd := gopathEnv + "/bin/gometalinter"
72
73
	// Install gometalinter -- no-op if already installed
74
	cmd := exec.Command("go", "get", "github.com/alecthomas/gometalinter")
75
	_, err := cmd.Output()
76
	if err != nil {
77
		exitErr, ok := err.(*exec.ExitError)
78
		if ok && len(exitErr.Stderr) != 0 {
79
			log.Println(string(exitErr.Stderr))
80
		}
81
		log.Fatal("go get github.com/alecthomas/gometalinter", err)
82
	}
83
84
	// Install all gometalinter dependencies -- no-op if already installed
85
	cmd = exec.Command(goMetaLinterCmd, "--install")
86
	_, err = cmd.Output()
87
	if err != nil {
88
		exitErr, ok := err.(*exec.ExitError)
89
		if ok && len(exitErr.Stderr) != 0 {
90
			log.Println(string(exitErr.Stderr))
91
		}
92
		log.Fatal(goMetaLinterCmd, "--install", err)
93
	}
94
95
	// Configure the metalinter
96
	if _, err := os.Stat("go-scrutinize.config"); os.IsNotExist(err) {
0 ignored issues
show
declaration of "err" shadows declaration at main.go:75
Loading history...
97
		cmd = exec.Command(goMetaLinterCmd, "./...", "--checkstyle")
98
	} else {
99
		cmd = exec.Command(goMetaLinterCmd, "./...", "--checkstyle", "--config=go-scrutinize.config")
100
	}
101
102
	// Run the metalinter -- note that will return non-zero exit status
103
	out, err := cmd.Output()
104
	if err != nil {
105
		exitErr := err.(*exec.ExitError)
106
		if len(exitErr.Stderr) != 0 {
107
			log.Println(string(exitErr.Stderr))
108
		}
109
	}
110
111
	// Write the output from the metalinter
112
	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...
113
}
114
115
func testAndCoverage() {
116
	goConvCmd := gopathEnv + "/bin/gocov"
117
	goConvXMLCmd := gopathEnv + "/bin/gocov-xml"
118
119
	// Install the coverage tool covov
120
	cmd := exec.Command("go", "get", "github.com/axw/gocov/...")
121
	_, err := cmd.Output()
122
	if err != nil {
123
		exitErr := err.(*exec.ExitError)
124
		if len(exitErr.Stderr) != 0 {
125
			log.Println(string(exitErr.Stderr))
126
		}
127
		log.Fatal("go", "get", "github.com/axw/gocov/...", err)
128
	}
129
130
	// Install the coverage file translation tool gocov-xml
131
	cmd = exec.Command("go", "get", "github.com/AlekSi/gocov-xml")
132
	_, err = cmd.Output()
133
	if err != nil {
134
		exitErr := err.(*exec.ExitError)
135
		if len(exitErr.Stderr) != 0 {
136
			log.Println(string(exitErr.Stderr))
137
		}
138
		log.Fatal("go", "get", "github.com/AlekSi/gocov-xml", err)
139
	}
140
141
	// Run tests with coverage
142
	cmd = exec.Command(goConvCmd, "test", "./...", "-race")
143
	gocovout, err := cmd.Output()
144
	if err != nil {
145
		exitErr := err.(*exec.ExitError)
146
		if len(exitErr.Stderr) != 0 {
147
			log.Println(string(exitErr.Stderr))
148
		}
149
		log.Fatal(goConvCmd, "test", "./...", "-race", err)
150
	}
151
152
	// Convert to clover format
153
	cmd = exec.Command(goConvXMLCmd)
154
	cmd.Stdin = bytes.NewReader(gocovout)
155
	xmlout, err := cmd.Output()
156
	if err != nil {
157
		exitErr := err.(*exec.ExitError)
158
		if len(exitErr.Stderr) != 0 {
159
			log.Println(string(exitErr.Stderr))
160
		}
161
		log.Fatal(goConvXMLCmd, err)
162
	}
163
164
	// Rewrite all filenames to use /home/scrutinizer/build paths
165
	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...
166
167
	// Write the output from the metalinter
168
	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...
169
170
}
171