From b8a2e20d5267eb6f076275e06be55045935d07af Mon Sep 17 00:00:00 2001 From: Kevin Diu Date: Wed, 28 Jun 2023 15:51:57 +0900 Subject: [PATCH] Update "make gotests/gen" command (#2085) * generate pkg with gotests exported option Signed-off-by: kevindiu * gen only New() test on pkg usecase Signed-off-by: kevindiu * comment out unimplemented test Signed-off-by: kevindiu * fix Signed-off-by: kevindiu * fix Signed-off-by: kevindiu * update option tests generation for pkg Signed-off-by: kevindiu * fix Signed-off-by: kevindiu * fix Signed-off-by: kevindiu * fix Signed-off-by: kevindiu * :robot: Add automatically generated tests Signed-off-by: Vdaas CI * :robot: Update license headers / Format go codes and yaml files Signed-off-by: Vdaas CI * fix chatopts Signed-off-by: kevindiu * Revert ":robot: Add automatically generated tests" This reverts commit d58682f05f441d14edac1db907c128eb62b1bd48. * Update .github/workflows/chatops.yml --------- Signed-off-by: kevindiu Signed-off-by: Vdaas CI Co-authored-by: Vdaas CI --- .github/workflows/chatops.yml | 4 ++-- Makefile.d/functions.mk | 29 +++++++++++++++++++++++++++-- Makefile.d/test.mk | 25 +++++++++++++++++++------ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/.github/workflows/chatops.yml b/.github/workflows/chatops.yml index 4e124a611d..0ff61480d1 100644 --- a/.github/workflows/chatops.yml +++ b/.github/workflows/chatops.yml @@ -345,8 +345,8 @@ jobs: git checkout ${HEAD_BRANCH} make gotests/install - ERR_LOG=$(make gotests/gen 2>&1 >/dev/null) - echo "ERR_LOG=$ERR_LOG" >> $GITHUB_OUTPUT + echo -n "ERR_LOG=" >> $GITHUB_OUTPUT + make gotests/gen 2>> $GITHUB_OUTPUT git add cmd hack internal pkg git commit -S --signoff -m ":robot: Add automatically generated tests" diff --git a/Makefile.d/functions.mk b/Makefile.d/functions.mk index bd95879c81..099541741e 100644 --- a/Makefile.d/functions.mk +++ b/Makefile.d/functions.mk @@ -183,10 +183,24 @@ define run-e2e-sidecar-test -kubeconfig=$(KUBECONFIG) endef +# This function generate only implementation tests, with the following conditions: +# - Generate all go tests on `./cmd`, `./hack` and `./internal` packages with some exclusion (see $GO_SOURCES) +# - Skip generating go tests under './pkg/*/router/*' and './pkg/*/handler/test/*' package +# - Generate only 'New()' test on './pkg/*/usecase' +# - Generate only exported function tests on `./pkg` package define gen-go-test-sources @for f in $(GO_SOURCES); do \ - echo "Generating go test file: $$f"; \ - gotests -w -template_dir $(ROOTDIR)/assets/test/templates/common -all $(patsubst %_test.go,%.go,$$f); \ + GOTESTS_OPTION=" -all "; \ + if [[ $$f =~ \.\/pkg\/.*\/router\/.* || $$f =~ \.\/pkg\/.*\/handler\/rest\/.* ]]; then \ + echo "Skip generating go test file: $$f"; \ + continue; \ + elif [[ $$f =~ \.\/pkg\/.*\/usecase\/.* ]]; then \ + GOTESTS_OPTION=" -only New "; \ + elif [[ $$f =~ \.\/pkg\/.* ]]; then \ + GOTESTS_OPTION=" -exported "; \ + fi; \ + echo "Generating go test file: $$f" with option $$GOTESTS_OPTION; \ + gotests -w -template_dir $(ROOTDIR)/assets/test/templates/common $$GOTESTS_OPTION $(patsubst %_test.go,%.go,$$f); \ RESULT=$$?; \ if [ ! $$RESULT -eq 0 ]; then \ echo $$RESULT; \ @@ -195,8 +209,19 @@ define gen-go-test-sources done endef +# This function generate only option tests, with the following conditions: +# - Generate all go tests on `./cmd`, `./hack` and `./internal` packages with exclusion (see $GO_SOURCES) +# - Skip generating go tests under './pkg/*/router' and './pkg/*/handler/test' and './pkg/*/usecase' package +# - Generate only exported function tests on './pkg` package define gen-go-option-test-sources @for f in $(GO_OPTION_SOURCES); do \ + GOTESTS_OPTION=" -all "; \ + if [[ $$f =~ \.\/pkg\/.*\/router\/.* || $$f =~ \.\/pkg\/.*\/handler\/rest\/.* || $$f =~ \.\/pkg\/.*\/usecase\/.* ]]; then \ + echo "Skip generating go option test file: $$f"; \ + continue; \ + elif [[ $$f =~ \.\/pkg\/.* ]]; then \ + GOTESTS_OPTION=" -exported "; \ + fi; \ echo "Generating go option test file: $$f"; \ gotests -w -template_dir $(ROOTDIR)/assets/test/templates/common -all $(patsubst %_test.go,%.go,$$f); \ RESULT=$$?; \ diff --git a/Makefile.d/test.mk b/Makefile.d/test.mk index af08940a40..b85e6401dd 100644 --- a/Makefile.d/test.mk +++ b/Makefile.d/test.mk @@ -195,8 +195,9 @@ test/all/gotestfmt: \ ## create empty test file if not exists test/create-empty: @$(call green, "create empty test file if not exists...") - for f in $(GO_ALL_TEST_SOURCES) ; do \ + @for f in $(GO_ALL_TEST_SOURCES) ; do \ if [ ! -f "$$f" ]; then \ + echo "Creating empty test file $$f"; \ package="$$(dirname $$f)" ; \ package="$$(basename $$package)" ; \ echo "package $$package" >> "$$f"; \ @@ -207,8 +208,9 @@ test/create-empty: ## remove empty test files test/remove-empty: @$(call green, "remove empty test files...") - for f in $(GO_ALL_TEST_SOURCES) ; do \ + @for f in $(GO_ALL_TEST_SOURCES) ; do \ if ! grep -q "func Test" "$$f"; then \ + echo "Removing empty test file $$f"; \ rm "$$f"; \ fi; \ done @@ -262,10 +264,11 @@ coverage: ## generate missing go test files gotests/gen: \ test/create-empty \ - gotests/patch-placeholder \ + test/patch-placeholder \ gotests/gen-test \ test/remove-empty \ gotests/patch \ + test/comment-unimplemented \ format/go/test .PHONY: gotests/gen-test @@ -290,13 +293,23 @@ gotests/patch: find $(ROOTDIR)/internal/test/goleak -name '*_test.go' | xargs sed -i -E "s%\"github.com/vdaas/vald/internal/test/goleak\"%%g" find $(ROOTDIR)/internal/test/goleak -name '*_test.go' | xargs sed -i -E "s/goleak\.//g" -.PHONY: gotests/patch-placeholder +.PHONY: test/patch-placeholder ## apply patches to the placeholder of the generated go test files -gotests/patch-placeholder: +test/patch-placeholder: @$(call green, "apply placeholder patches to go test files...") - for f in $(GO_ALL_TEST_SOURCES) ; do \ + @for f in $(GO_ALL_TEST_SOURCES) ; do \ if [ ! -f "$$f" ] ; then continue; fi; \ sed -i -e '/\/\/ $(TEST_NOT_IMPL_PLACEHOLDER)/,$$d' $$f; \ if [ "$$(tail -1 $$f)" != "" ]; then echo "" >> "$$f"; fi; \ echo "// $(TEST_NOT_IMPL_PLACEHOLDER)" >>"$$f"; \ done + +.PHONY: test/comment-unimplemented +## comment out unimplemented tests +test/comment-unimplemented: + @$(call green, "comment out unimplemented test...") + @for f in $(GO_ALL_TEST_SOURCES) ; do \ + if [ ! -f "$$f" ] ; then continue; fi; \ + sed -r -i -e '/\/\/ $(TEST_NOT_IMPL_PLACEHOLDER)/,+9999999 s/^/\/\/ /' $$f; \ + sed -i 's/\/\/ \/\/ $(TEST_NOT_IMPL_PLACEHOLDER)/\/\/ $(TEST_NOT_IMPL_PLACEHOLDER)/g' $$f; \ + done