Skip to content

Commit

Permalink
Update "make gotests/gen" command (#2085)
Browse files Browse the repository at this point in the history
* generate pkg with gotests exported option

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* gen only New() test on pkg usecase

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* comment out unimplemented test

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* fix

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* fix

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* update option tests generation for pkg

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* fix

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* fix

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* fix

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* 🤖 Add automatically generated tests

Signed-off-by: Vdaas CI <vald@vdaas.org>

* 🤖 Update license headers / Format go codes and yaml files

Signed-off-by: Vdaas CI <vald@vdaas.org>

* fix chatopts

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>

* Revert "🤖 Add automatically generated tests"

This reverts commit d58682f.

* Update .github/workflows/chatops.yml

---------

Signed-off-by: kevindiu <kevin_diu@yahoo.com.hk>
Signed-off-by: Vdaas CI <vald@vdaas.org>
Co-authored-by: Vdaas CI <vald@vdaas.org>
  • Loading branch information
kevindiu and vdaas-ci authored Jun 28, 2023
1 parent 7689577 commit b8a2e20
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/chatops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 27 additions & 2 deletions Makefile.d/functions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand All @@ -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=$$?; \
Expand Down
25 changes: 19 additions & 6 deletions Makefile.d/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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"; \
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit b8a2e20

Please sign in to comment.