From 63f1cf45389e1fab837cc1a9471bc553d9e269b5 Mon Sep 17 00:00:00 2001 From: tkrop Date: Tue, 29 Nov 2022 13:16:52 +0100 Subject: [PATCH] feat: update makefile and apply parallel pattern (#11) --- Makefile | 50 ++++++++++++++++++++++------------------- gock/README.md | 2 +- gock/controller_test.go | 1 + gock/store_test.go | 1 + mock/mock_test.go | 10 +++++++++ perm/perm_test.go | 1 + sync/waitgroup_test.go | 6 ++++- test/testing_test.go | 2 ++ 8 files changed, 48 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 0a44488..1715fa9 100644 --- a/Makefile +++ b/Makefile @@ -324,8 +324,8 @@ test: test-all test-all: test-clean init-sources $(TEST_ALL) test-unit: test-clean init-sources $(TEST_UNIT) test-clean: - @if [ -f "$(TEST_ALL)" ]; then rm -v $(TEST_ALL); fi; \ - if [ -f "$(TEST_UNIT)" ]; then rm -v $(TEST_UNIT); fi; + @if [ -f "$(TEST_ALL)" ]; then rm -vf $(TEST_ALL); fi; \ + if [ -f "$(TEST_UNIT)" ]; then rm -vf $(TEST_UNIT); fi; test-upload: test-cover: @if [ "$(TEST_ALL)" -nt "$(TEST_UNIT)" ]; then \ @@ -335,30 +335,34 @@ test-cover: fi; \ -define testcase - for ARG in $(RUNARGS); do \ - if [ -z "$${PACKAGES}" ]; then PACKAGES="$${ARG%%/*}"; \ - else PACKAGES="$${PACKAGES}\n$${ARG%%/*}"; fi; \ - if [ -z "$${TESTCASE}" ]; then TESTCASES="-run $${ARG#*/}"; \ - else TESTCASES="$${TESTCASES} -run $${ARG#*/}"; fi; \ - done; \ - echo -en "$${PACKAGES}" | sort -u | sed "s/^/.\//"; \ - echo "$${TESTCASES}" +define testargs + if [ -n "$(RUNARGS)" ]; then ARGS=($(RUNARGS)); \ + if [[ -f "$${ARGS[0]}" && ! -d "$${ARGS[0]}" && \ + "$${ARGS[0]}" == *_test.go ]]; then \ + find $$(dirname $(RUNARGS) | sort -u) \ + -maxdepth 1 -a -name "*.go" -a ! -name "*_test.go" \ + -o -name "common_test.go" -o -name "mock_*_test.go" | \ + sed -e "s/^/.\//"; \ + echo $(addprefix ./,$(RUNARGS)); \ + elif [[ -d "$${ARGS[0]}" && ! -f "$${ARGS[0]}" ]]; then \ + echo $(addprefix ./,$(RUNARGS)); \ + elif [[ ! -f "$${ARGS[0]}" && ! -d "$${ARGS[0]}" ]]; then \ + for ARG in $${ARGS[0]}; do \ + if [ -z "$${PACKAGES}" ]; then PACKAGES="$${ARG%/*}"; \ + else PACKAGES="$${PACKAGES}\n$${ARG%/*}"; fi; \ + if [ -z "$${TESTCASE}" ]; then TESTCASES="-run $${ARG##*/}"; \ + else TESTCASES="$${TESTCASES} -run $${ARG##*/}"; fi; \ + done; \ + echo -en "$${PACKAGES}" | sort -u | sed "s/^/.\//"; \ + echo "$${TESTCASES}"; \ + else + echo "warning: invalid test parameters [$${ARGS[@]}]" > /dev/stderr; + fi;\ + else echo "./..."; fi endef TESTFLAGS ?= -race -mod=readonly -count=1 -TESTARGS ?= $(shell \ - if [[ "$(RUNARGS)" == *_test.go ]]; then \ - find $$(dirname $(RUNARGS) | sort -u) -maxdepth 1 -a -name "*.go" -a \ - ! -name "*_test.go" -o -name "common_test.go" -o -name "mock_*_test.go" | \ - sed -e "s/^/.\//"; \ - echo $(addprefix ./,$(RUNARGS)); \ - elif [[ "$(RUNARGS)" == [a-z_-/]*/* ]]; then \ - echo "$(shell $(testcase))"; \ - elif [ -n "$(RUNARGS)" ]; then \ - echo $(addprefix ./,$(RUNARGS)); \ - else echo "./..."; fi \ -) +TESTARGS ?= $(shell $(testargs)) $(TEST_ALL): $(SOURCES) init-sources $(TEST_DEPS) @if [ ! -d "$(BUILDIR)" ]; then mkdir -p $(BUILDIR); fi; diff --git a/gock/README.md b/gock/README.md index bd0ccd8..236d5a6 100644 --- a/gock/README.md +++ b/gock/README.md @@ -7,7 +7,7 @@ on testing it does not support the networking and observation features of is mainly compatible with. -## Migration from Gock to Tock +## Migration from Gock Migration from [Gock][gock] to this package is straigt forward. You can just add the controller creation as at the begin of your test giving it the name diff --git a/gock/controller_test.go b/gock/controller_test.go index 5cee2a1..00f0336 100644 --- a/gock/controller_test.go +++ b/gock/controller_test.go @@ -40,6 +40,7 @@ var testControllerParams = map[string]struct { func TestController(t *testing.T) { t.Parallel() + for message, param := range testControllerParams { message, param := message, param t.Run(message, func(t *testing.T) { diff --git a/gock/store_test.go b/gock/store_test.go index 6230011..136d6e8 100644 --- a/gock/store_test.go +++ b/gock/store_test.go @@ -203,6 +203,7 @@ var testMatchParams = map[string]struct { func TestMatch(t *testing.T) { t.Parallel() + for message, param := range testMatchParams { message, param := message, param t.Run(message, func(t *testing.T) { diff --git a/mock/mock_test.go b/mock/mock_test.go index ad44c5d..3d20b56 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -126,6 +126,7 @@ var testSetupParams = perm.ExpectMap{ func TestSetup(t *testing.T) { t.Parallel() + for message, expect := range testSetupParams.Remain(test.ExpectSuccess) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { @@ -158,6 +159,7 @@ var testChainParams = perm.ExpectMap{ func TestChain(t *testing.T) { t.Parallel() + for message, expect := range testChainParams.Remain(test.ExpectFailure) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { @@ -195,6 +197,7 @@ var testSetupChainParams = perm.ExpectMap{ func TestSetupChain(t *testing.T) { t.Parallel() + for message, expect := range testSetupChainParams.Remain(test.ExpectFailure) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { @@ -227,6 +230,7 @@ func TestSetupChain(t *testing.T) { func TestChainSetup(t *testing.T) { t.Parallel() + for message, expect := range testSetupChainParams.Remain(test.ExpectFailure) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { @@ -274,6 +278,7 @@ var testParallelChainParams = perm.ExpectMap{ func TestParallelChain(t *testing.T) { t.Parallel() + for message, expect := range testParallelChainParams.Remain(test.ExpectFailure) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { @@ -327,6 +332,7 @@ var testChainSubParams = perm.ExpectMap{ func TestChainSub(t *testing.T) { t.Parallel() + perms := testChainSubParams // perms := PermRemain(testChainSubParams, test.ExpectFailure) for message, expect := range perms { @@ -370,6 +376,7 @@ var testDetachParams = perm.ExpectMap{ func TestDetach(t *testing.T) { t.Parallel() + for message, expect := range testDetachParams.Remain(test.ExpectFailure) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { @@ -432,6 +439,7 @@ var testPanicParams = map[string]struct { func TestPanic(t *testing.T) { t.Parallel() + for message, param := range testPanicParams { message, param := message, param t.Run(message, func(t *testing.T) { @@ -500,6 +508,7 @@ var testGetSubSliceParams = map[string]struct { func TestGetSubSlice(t *testing.T) { t.Parallel() + for message, param := range testGetSubSliceParams { message, param := message, param t.Run(message, func(t *testing.T) { @@ -532,6 +541,7 @@ var testGetDoneParams = map[string]struct { func TestGetDone(t *testing.T) { t.Parallel() + for message, param := range testGetDoneParams { message, param := message, param t.Run(message, func(t *testing.T) { diff --git a/perm/perm_test.go b/perm/perm_test.go index 1b2ba9b..abb3b23 100644 --- a/perm/perm_test.go +++ b/perm/perm_test.go @@ -47,6 +47,7 @@ var testTestParams = perm.ExpectMap{ func TestTest(t *testing.T) { t.Parallel() + for message, expect := range testTestParams.Remain(test.ExpectFailure) { message, expect := message, expect t.Run(message, test.Run(expect, func(t *test.TestingT) { diff --git a/sync/waitgroup_test.go b/sync/waitgroup_test.go index af26254..00678a6 100644 --- a/sync/waitgroup_test.go +++ b/sync/waitgroup_test.go @@ -8,8 +8,10 @@ import ( ) func TestWaitGroup(t *testing.T) { - defer func() { recover() }() + t.Parallel() + // Given + defer func() { recover() }() wg := NewWaitGroup() // When @@ -23,6 +25,8 @@ func TestWaitGroup(t *testing.T) { } func TestLenientWaitGroup(t *testing.T) { + t.Parallel() + // Given wg := NewLenientWaitGroup() diff --git a/test/testing_test.go b/test/testing_test.go index a93c581..119f52a 100644 --- a/test/testing_test.go +++ b/test/testing_test.go @@ -144,6 +144,7 @@ func Call(t *TestingT, mocks *mock.Mocks, expect Expect, test func(*TestingT)) { func TestRun(t *testing.T) { t.Parallel() + for message, param := range testRunParams { message, param := message, param t.Run(message, Run(param.expect, func(t *TestingT) { @@ -161,6 +162,7 @@ func TestRun(t *testing.T) { func TestOther(t *testing.T) { t.Parallel() + for message, param := range testRunParams { message, param := message, param switch param.expect {