From 99235e2aec83200990573662e744061d25fcc48a Mon Sep 17 00:00:00 2001 From: bijayasharma Date: Tue, 30 Jun 2020 16:00:16 -0400 Subject: [PATCH] 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 --- Makefile | 25 +++++++++++++++++++------ playbooks/airshipui-test/run.yaml | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index fca0140..8bda3bb 100755 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + @./tools/check_copyright diff --git a/playbooks/airshipui-test/run.yaml b/playbooks/airshipui-test/run.yaml index 1b57c9e..8d51709 100644 --- a/playbooks/airshipui-test/run.yaml +++ b/playbooks/airshipui-test/run.yaml @@ -12,7 +12,7 @@ --- - hosts: all - name: Run tests + name: Run unit-tests tasks: - name: make docker-image-unit-tests make: