Skip to content

Commit 51734c2

Browse files
committed
Add Makefile and go tools
1 parent bf99f83 commit 51734c2

7 files changed

Lines changed: 160 additions & 0 deletions

File tree

Makefile

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright (c) 2021 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
2+
3+
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
4+
.DEFAULT_GOAL := help
5+
.PHONY: help
6+
help: ## Display this help message.
7+
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
8+
9+
TOP=$(CURDIR)
10+
11+
# Go build related variables
12+
GOSRC=$(TOP)
13+
14+
# Set GOBIN to where binaries get picked up while creating RPM/ISO.
15+
GOBIN?=$(TOP)/bin
16+
GOCOVER=$(GOSRC)/cover
17+
GOTOOLSBIN=$(TOP)/tools/go/
18+
19+
.SILENT:
20+
21+
.PHONY: all
22+
all: clean analyze build test
23+
24+
.PHONY: clean
25+
clean: ## Clean Plugin Manager go build & test artifacts
26+
@echo "Cleaning Plugin Manager Go binaries...";
27+
export GOBIN=$(GOBIN); \
28+
cd $(GOSRC); \
29+
go clean -i -mod=vendor ./...;
30+
@echo "Cleaning Go test artifacts... ";
31+
-@rm $(GOSRC)/{,.}*{dot,html,log,svg,xml};
32+
-@rm $(GOSRC)/cmd/pm/{,.}*{dot,log,svg};
33+
-@rm -rf $(GOSRC)/plugins/
34+
-@rm -rf $(GOCOVER);
35+
36+
.PHONY: build
37+
build: ## Build source code
38+
# Since go build determines and build only updated sources, no need to run clean all go binaries
39+
@echo "Building Plugin Manager Go binaries...";
40+
export GOBIN=$(GOBIN); \
41+
cd $(GOSRC); \
42+
go install -ldflags "-X main.buildDate=`date -u +%Y%m%d.%H%M%S`" -mod=vendor -v ./...; \
43+
ret=$$?; \
44+
if [ $${ret} -ne 0 ]; then \
45+
@echo "Failed to build Plugin Manager Go binaries."; \
46+
exit 1; \
47+
fi
48+
49+
50+
.PHONY: analyze
51+
analyze: gofmt golint govet go-race ## Analyze source code for different errors through gofmt, golint, govet, go-race
52+
53+
.PHONY: golint
54+
golint: ## Run golint
55+
@echo Checking Plugin Manager Go code for lint errors...
56+
$(GOTOOLSBIN)/golint -set_exit_status `cd $(GOSRC); go list -mod=vendor -f '{{.Dir}}' ./...`
57+
58+
.PHONY: gofmt
59+
gofmt: ## Run gofmt
60+
@echo Checking Go code for format errors...
61+
fmterrs=`gofmt -l . | grep -v vendor/ 2>&1`; \
62+
if [ "$$fmterrs" ]; then \
63+
echo "gofmt must be run on the following files:"; \
64+
echo "$$fmterrs"; \
65+
exit 1; \
66+
fi
67+
68+
.PHONY: govet
69+
govet: ## Run go vet
70+
@echo Vetting Plugin Manager Go code for errors...
71+
cd $(GOSRC); \
72+
go vet -mod=vendor -all ./...
73+
74+
.PHONY: test
75+
test: ## Run all tests
76+
echo "Running Plugin Manager Go Unit Tests..."; \
77+
mkdir -p $(GOCOVER);
78+
export INTEG_TEST_BIN=$(GOSRC); \
79+
export PM_CONF_FILE=$(GOSRC)/docs/sample/pm.config.yaml; \
80+
export INTEGRATION_TEST=START; \
81+
cd $(GOSRC); \
82+
test_failed=0; \
83+
d=pm; \
84+
go test -mod=vendor -v --cover -covermode=count -coverprofile=$(GOCOVER)/$${d}.out ./... | \
85+
$(GOTOOLSBIN)/go-junit-report > TEST-$${d}.xml; \
86+
ret=$${PIPESTATUS[0]}; \
87+
if [ $${ret} -ne 0 ]; then \
88+
echo "Go unit test failed for $${d}."; \
89+
test_failed=1; \
90+
fi ; \
91+
awk -f $(TOP)/tools/gocoverage-collate.awk $(GOCOVER)/* > $(GOCOVER)/cover.out; \
92+
go tool cover -html=$(GOCOVER)/cover.out -o go-coverage-$${d}.html; \
93+
$(GOTOOLSBIN)/gocov convert $(GOCOVER)/cover.out | $(GOTOOLSBIN)/gocov-xml > go-coverage-$${d}.xml; \
94+
rm -rf $(GOCOVER)/*; \
95+
export INTEGRATION_TEST=DONE; \
96+
if [ $${test_failed} -ne 0 ]; then \
97+
echo "Go unit tests failed."; \
98+
exit 1; \
99+
fi
100+
101+
.PHONY: go-race
102+
go-race: ## Run Go tests with race detector enabled
103+
echo "Checking Go code for race conditions...";
104+
# NOTE: SUMCOVER directory should be present, along with INTEGRATION_TEST
105+
# value being set to "START" for integ_test.go to succeed.
106+
mkdir -p $(GOCOVER);
107+
export INTEGRATION_TEST=START; \
108+
export INTEG_TEST_BIN=$(GOSRC); \
109+
cd $(GOSRC); \
110+
export PM_CONF_FILE=$(GOSRC)/docs/sample/pm.config.yaml; \
111+
go test -mod=vendor -v -race ./...;
112+
113+
.NOTPARALLEL:

tools/go/go-junit-report

2.59 MB
Binary file not shown.

tools/go/gocov

4.45 MB
Binary file not shown.

tools/go/gocov-html

2.65 MB
Binary file not shown.

tools/go/gocov-xml

2.72 MB
Binary file not shown.

tools/go/golint

6.14 MB
Binary file not shown.

tools/gocoverage-collate.awk

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/awk -f
2+
# Copyright (C) 2016, 2017 SUSE LLC.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# collate.awk allows you to collate a bunch of Go coverprofiles for a given
17+
# binary (generated with -test.coverprofile), so that the statistics actually
18+
# make sense. The input to this function is just the concatenated versions of
19+
# the coverage reports, and the output is the combined coverage report.
20+
#
21+
# NOTE: This will _only_ work on coverage binaries compiles with
22+
# -covermode=count. The other modes aren't supported.
23+
24+
{
25+
# Every coverage file in the set will start with a "mode:" header. Just make
26+
# sure they're all set to "count".
27+
if ($1 == "mode:") {
28+
if ($0 != "mode: count") {
29+
print "Invalid coverage mode", $2 > "/dev/stderr"
30+
exit 1
31+
}
32+
next
33+
}
34+
35+
# The format of all other lines is as follows.
36+
# <file>:<startline>.<startcol>,<endline>.<endcol> <numstmt> <count>
37+
# We only care about the first field and the count.
38+
statements[$1] = $2
39+
counts[$1] += $3
40+
}
41+
42+
END {
43+
print "mode: count"
44+
for (block in statements) {
45+
print block, statements[block], counts[block]
46+
}
47+
}

0 commit comments

Comments
 (0)