Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 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 | |
| 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | # tool containers |
David K. Bainbridge | adf422d | 2021-04-09 16:06:41 +0000 | [diff] [blame^] | 24 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 25 | |
| 26 | GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go |
| 27 | GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report |
| 28 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 29 | GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt |
| 30 | GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint |
| 31 | |
| 32 | # This should to be the first and default target in this Makefile |
| 33 | help: |
| 34 | @echo "Usage: make [<target>]" |
| 35 | @echo "where available targets are:" |
| 36 | @echo |
| 37 | @echo "build : Build the library" |
| 38 | @echo "clean : Remove files created by the build" |
| 39 | @echo "distclean : Remove build and testing artifacts and reports" |
| 40 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 41 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
| 42 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
| 43 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 44 | @echo "mod-update : Update go.mod and the vendor directory" |
| 45 | @echo "test : Generate reports for all go tests" |
| 46 | @echo |
| 47 | |
| 48 | ## build the library |
| 49 | build: |
| 50 | ${GO} build -mod=vendor ./... |
| 51 | |
| 52 | ## lint and unit tests |
| 53 | |
| 54 | lint-style: |
| 55 | @echo "Running style check..." |
| 56 | @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 57 | if [ ! -z "$$gofmt_out" ]; then \ |
| 58 | echo "$$gofmt_out" ;\ |
| 59 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 60 | exit 1 ;\ |
| 61 | fi |
| 62 | @echo "Style check OK" |
| 63 | |
| 64 | lint-sanity: |
| 65 | @echo "Running sanity check..." |
| 66 | @${GO} vet -mod=vendor ./... |
| 67 | @echo "Sanity check OK" |
| 68 | |
| 69 | lint-mod: |
| 70 | @echo "Running dependency check..." |
| 71 | @${GO} mod verify |
| 72 | @echo "Dependency check OK. Running vendor check..." |
| 73 | @git status > /dev/null |
| 74 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1) |
| 75 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1) |
| 76 | ${GO} mod tidy |
| 77 | ${GO} mod vendor |
| 78 | @git status > /dev/null |
| 79 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 80 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 81 | @echo "Vendor check OK." |
| 82 | |
| 83 | lint: lint-style lint-sanity lint-mod |
| 84 | |
| 85 | sca: |
| 86 | rm -rf ./sca-report |
| 87 | @mkdir -p ./sca-report |
| 88 | ${GOLANGCI_LINT} run --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 89 | |
| 90 | test: |
| 91 | @mkdir -p ./tests/results |
| 92 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 93 | RETURN=$$? ;\ |
| 94 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 95 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 96 | exit $$RETURN |
| 97 | |
| 98 | clean: distclean |
| 99 | |
| 100 | distclean: |
| 101 | rm -rf ./sca-report ./tests |
| 102 | |
| 103 | mod-update: |
| 104 | ${GO} mod tidy |
David K. Bainbridge | adf422d | 2021-04-09 16:06:41 +0000 | [diff] [blame^] | 105 | ${GO} mod vendor |