Added unit-tests in Makefile

* This commit will add unit-tests and coverprofile for the tests.
* Adds ability to run make unit-tests command.
* This has been referenced from the Airshipctl project.

Change-Id: Ida6bfe64b7fc166072ae6357940d283fa3d60349
This commit is contained in:
bijayasharma 2020-06-30 16:00:16 -04:00
parent 3fefc40632
commit 99235e2aec
2 changed files with 20 additions and 7 deletions

View File

@ -34,6 +34,13 @@ COVERAGE_OUTPUT := coverage.out
TESTFLAGS ?= -count=1
# go options
PKG ?= ./...
TESTS ?= .
COVER_FLAGS ?=
COVER_PROFILE ?= cover.out
COVER_EXCLUDE ?= (zz_generated)
# Override the value of the version variable in main.go
LD_FLAGS= '-X opendev.org/airship/airshipui/internal/commands.version=$(GIT_VERSION)'
GO_FLAGS := -ldflags=$(LD_FLAGS)
@ -82,14 +89,20 @@ install-npm-modules: $(NPX)
cd $(WEBDIR) && (PATH="$(PATH):$(JSLINTER_BIN)"; $(NPM) install) && cd ..
.PHONY: test
test: lint
test: cover
test: check-copyright
test:
go test $(RECURSIVE_DIRS) -v $(TESTFLAGS)
.PHONY: unit-tests
unit-tests:
@echo "Performing unit test step..."
@go test -run $(TESTS) $(PKG) $(TESTFLAGS) $(COVER_FLAGS)
@echo "All unit tests passed"
.PHONY: cover
cover: TESTFLAGS += -coverprofile=$(COVERAGE_OUTPUT)
cover: test
go tool cover -html=$(COVERAGE_OUTPUT)
cover: TESTFLAGS = -covermode=atomic -coverprofile=fullcover.out
cover: unit-tests
@grep -vE "$(COVER_EXCLUDE)" fullcover.out > $(COVER_PROFILE)
.PHONY: images
images: docker-image
@ -180,4 +193,4 @@ add-copyright:
# check-copyright is a utility to check if copyright header is present on all files
.PHONY: check-copyright
check-copyright:
@./tools/check_copyright
@./tools/check_copyright

View File

@ -12,7 +12,7 @@
---
- hosts: all
name: Run tests
name: Run unit-tests
tasks:
- name: make docker-image-unit-tests
make: