From e2df0e6b36919d6b3cccfd2188dc8b764e92347e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Alberto=20Monta=C3=B1o=20Fetecua?= Date: Mon, 2 Dec 2024 09:26:59 -0500 Subject: [PATCH] feature: implement a new command to support gotestsum (#93) --- .circleci/test-deploy.yml | 44 +++++++++++++++++++++++++++++++++++ src/commands/gotestsum.yml | 36 ++++++++++++++++++++++++++++ src/commands/test.yml | 10 +++++++- src/scripts/gotestsum.sh | 36 ++++++++++++++++++++++++++++ src/scripts/test.sh | 6 +++++ tests/go.mod | 4 ++-- tests/main.go | 13 +++++++++-- tests/numbers/numbers.go | 5 ++++ tests/numbers/numbers_test.go | 10 ++++++++ tests/words/words.go | 5 ++++ tests/words/words_test.go | 10 ++++++++ 11 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 src/commands/gotestsum.yml create mode 100644 src/scripts/gotestsum.sh create mode 100644 tests/numbers/numbers.go create mode 100644 tests/numbers/numbers_test.go create mode 100644 tests/words/words.go create mode 100644 tests/words/words_test.go diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index aa03715..a3c9c43 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -137,6 +137,42 @@ jobs: - go/install: version: 1.16.4 - run: go version && go build ./... + go-test-default: + docker: + - image: cimg/go:1.23.3 + steps: + - checkout + - go/test: + project-path: tests + int-test-gotestsum-1-test: + docker: + - image: cimg/go:1.23.3 + steps: + - checkout + - go/gotestsum: + project-path: tests + packages: circleci.com/go-orb/numbers + - store_test_results: + path: /home/circleci/project/tests + int-test-gotestsum-2-test: + docker: + - image: cimg/go:1.23.3 + steps: + - checkout + - go/gotestsum: + project-path: tests + packages: circleci.com/go-orb/numbers circleci.com/go-orb/words + - store_test_results: + path: /home/circleci/project/tests + int-test-gotestsum-all-test: + docker: + - image: cimg/go:1.23.3 + steps: + - checkout + - go/gotestsum: + project-path: tests + - store_test_results: + path: /home/circleci/project/tests workflows: test-deploy: jobs: @@ -164,6 +200,14 @@ workflows: filters: *filters - int-test-vm-arm64: filters: *filters + - go-test-default: + filters: *filters + - int-test-gotestsum-1-test: + filters: *filters + - int-test-gotestsum-2-test: + filters: *filters + - int-test-gotestsum-all-test: + filters: *filters - orb-tools/pack: filters: *release-filters - orb-tools/publish: diff --git a/src/commands/gotestsum.yml b/src/commands/gotestsum.yml new file mode 100644 index 0000000..0719187 --- /dev/null +++ b/src/commands/gotestsum.yml @@ -0,0 +1,36 @@ +description: | + Runs 'go test ./...' but includes extensive parameterization for finer tuning +parameters: + project-path: + description: | + The path to the directory containing your Go project files: + .goreleaser.yaml, go.mod, main.go. + Defaults to $HOME. + type: string + default: "" + packages: + description: import tests to run, by path glob. + type: string + default: "./..." + junitfile: + description: file to save junit format xml file + type: string + default: "unit-tests.xml" + covermode: + description: mode used to count coverage + type: string + default: "atomic" + coverprofile: + description: file to save coverage profile + type: string + default: "cover-source.out" +steps: + - run: + environment: + ORB_EVAL_PROJECT_PATH: <> + ORB_VAL_PACKAGES: <> + ORB_VAL_JUNITFILE: <> + ORB_VAL_COVERMODE: <> + ORB_EVAL_COVER_PROFILE: <> + command: <> + name: "gotestsum" diff --git a/src/commands/test.yml b/src/commands/test.yml index e285d22..2deae2f 100644 --- a/src/commands/test.yml +++ b/src/commands/test.yml @@ -60,12 +60,19 @@ parameters: (Sets -cover.) type: string default: "./..." + project-path: + description: | + The path to the directory containing your Go project files: + .goreleaser.yaml, go.mod, main.go. + Defaults to $HOME. + type: string + default: "" steps: - run: environment: ORB_VAL_RACE: <> ORB_VAL_COUNT: <> - ORB_VAL_FAIL_FAST: <> + ORB_VAL_FAILFAST: <> ORB_VAL_SHORT: <> ORB_VAL_TIMEOUT: <> ORB_VAL_PARALLEL: <> @@ -74,6 +81,7 @@ steps: ORB_VAL_PACKAGES: <> ORB_VAL_COVERPKG: <> ORB_EVAL_COVER_PROFILE: <> + ORB_EVAL_PROJECT_PATH: <> command: <> name: "go test" no_output_timeout: <> diff --git a/src/scripts/gotestsum.sh b/src/scripts/gotestsum.sh new file mode 100644 index 0000000..097c713 --- /dev/null +++ b/src/scripts/gotestsum.sh @@ -0,0 +1,36 @@ +#! /bin/bash + +if [ ! "$(which gotestsum)" ]; then + echo The gotestsum command is not available in this executor. + exit 1 +fi + +if [ -n "${ORB_EVAL_PROJECT_PATH}" ]; then + cd "${ORB_EVAL_PROJECT_PATH}" || exit +fi + +if [ -n "$ORB_VAL_PACKAGES" ];then + set -- "$@" --packages "${ORB_VAL_PACKAGES}" +fi + +if [ -n "$ORB_VAL_JUNITFILE" ];then + set -- "$@" --junitfile "${ORB_VAL_JUNITFILE}" +fi + +if [ -n "$ORB_VAL_COVERMODE" ];then + COVERMODE="-covermode=${ORB_VAL_COVERMODE}" +fi + +if [ -n "$ORB_EVAL_COVER_PROFILE" ];then + INPUT_COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE") + COVER_PROFILE="-coverprofile=${INPUT_COVER_PROFILE}" +fi + +set -x +gotestsum \ + "$@" \ + -- \ + "${COVERMODE}" \ + "${COVER_PROFILE}" \ + -coverpkg="$ORB_VAL_PACKAGES" +set +x diff --git a/src/scripts/test.sh b/src/scripts/test.sh index aef96cb..ba61a02 100644 --- a/src/scripts/test.sh +++ b/src/scripts/test.sh @@ -1,8 +1,14 @@ #!/usr/bin/env bash + +if [ -n "${ORB_EVAL_PROJECT_PATH}" ]; then + cd "${ORB_EVAL_PROJECT_PATH}" || exit +fi + COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE") if [ -n "$ORB_VAL_RACE" ]; then set -- "$@" -race + ORB_VAL_COVER_MODE=atomic fi if [ "$ORB_VAL_FAILFAST" != "false" ]; then diff --git a/tests/go.mod b/tests/go.mod index 218a61a..515c9a1 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -1,3 +1,3 @@ -module main +module circleci.com/go-orb -go 1.20 \ No newline at end of file +go 1.23.3 diff --git a/tests/main.go b/tests/main.go index cfe4953..4be563b 100644 --- a/tests/main.go +++ b/tests/main.go @@ -1,6 +1,15 @@ // main.go package main +import ( + "fmt" + + "circleci.com/go-orb/numbers" + "circleci.com/go-orb/words" +) + func main() { - println("Hello, World!") -} \ No newline at end of file + word := words.Word() + number := numbers.Number() + fmt.Printf("%s %d\n", word, number) +} diff --git a/tests/numbers/numbers.go b/tests/numbers/numbers.go new file mode 100644 index 0000000..47f6f5b --- /dev/null +++ b/tests/numbers/numbers.go @@ -0,0 +1,5 @@ +package numbers + +func Number() int { + return 1987 +} diff --git a/tests/numbers/numbers_test.go b/tests/numbers/numbers_test.go new file mode 100644 index 0000000..02ec444 --- /dev/null +++ b/tests/numbers/numbers_test.go @@ -0,0 +1,10 @@ +package numbers + +import "testing" + +func TestNumber(t *testing.T) { + result := Number() + if result != 1987 { + t.FailNow() + } +} diff --git a/tests/words/words.go b/tests/words/words.go new file mode 100644 index 0000000..1669275 --- /dev/null +++ b/tests/words/words.go @@ -0,0 +1,5 @@ +package words + +func Word() string { + return "Word" +} diff --git a/tests/words/words_test.go b/tests/words/words_test.go new file mode 100644 index 0000000..530e80d --- /dev/null +++ b/tests/words/words_test.go @@ -0,0 +1,10 @@ +package words + +import "testing" + +func TestWord(t *testing.T) { + result := Word() + if result != "Word" { + t.FailNow() + } +}