From 3da1dc36dbd2a43fdec1a6007d1c9e8e613e79b7 Mon Sep 17 00:00:00 2001 From: sbp-bvanb Date: Thu, 17 Oct 2024 08:52:26 +0200 Subject: [PATCH] feat: [#67] Run component tests --- README.md | 7 ++++++- action.yml | 40 ++++++++++++++++++++++--------------- src/go-test.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 17 deletions(-) create mode 100755 src/go-test.sh diff --git a/README.md b/README.md index b15d906..1637b63 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ and a [.golangci.yml](https://golangci-lint.run/usage/configuration/). -## Integration +### Integration To execute integration tests, make sure that the code is located in a file with a `_integration_test.go` postfix, such as `some_integration_test.go`. @@ -73,3 +73,8 @@ Additionally, include the following header in the file: After adding this header, issue the command `go test ./... --tags=integration` as demonstrated in this example. This action will run both unit and integration tests. If the `--tags` step is omitted, only unit tests will be executed. + +### Component + +See the integration paragraph for the steps and replace `integration` with +`component` to run them. diff --git a/action.yml b/action.yml index 5f269a8..9adf612 100644 --- a/action.yml +++ b/action.yml @@ -18,7 +18,6 @@ inputs: golang-number-of-tests-in-parallel: description: | Number of test in parallel. - default: "4" trivy-action-db: default: "ghcr.io/aquasecurity/trivy-db:2" description: | @@ -120,27 +119,24 @@ runs: - name: unit tests shell: bash run: | - go test \ - -p ${{ inputs.golang-number-of-tests-in-parallel }} \ - -race \ - -short \ - -v \ - $(go list ./... | grep -v '${{ inputs.golang-unit-tests-exclusions }}') + ${GITHUB_ACTION_PATH}/src/go-test.sh \ + "" \ + "" \ + ${{ inputs.golang-number-of-tests-in-parallel }} \ + ${{ inputs.golang-unit-tests-exclusions }} \ + "" # # Unit & integration tests including code coverage. # - name: unit & integrations tests and code coverage shell: bash run: | - go test \ - -coverpkg=./... \ - -coverprofile=profile.cov \ - -p ${{ inputs.golang-number-of-tests-in-parallel }} \ - -race \ - -short \ - --tags=integration \ - -v \ - $(go list ./... | grep -v '${{ inputs.golang-unit-tests-exclusions }}') + ${GITHUB_ACTION_PATH}/src/go-test.sh \ + "./..." \ + "profile.cov" \ + ${{ inputs.golang-number-of-tests-in-parallel }} \ + ${{ inputs.golang-unit-tests-exclusions }} \ + "integration" code_coverage_actual=$(go tool cover -func profile.cov |\ grep total: |\ awk '{print $3}' |\ @@ -149,3 +145,15 @@ runs: echo "The actual code coverage: '${code_coverage_actual}' is too low. Expected: '${{ inputs.code_coverage_expected }}'." exit 1 fi + # + # Component tests. + # + - name: component tests + shell: bash + run: | + ${GITHUB_ACTION_PATH}/src/go-test.sh \ + "" \ + "" \ + ${{ inputs.golang-number-of-tests-in-parallel }} \ + ${{ inputs.golang-unit-tests-exclusions }} \ + "component" diff --git a/src/go-test.sh b/src/go-test.sh new file mode 100755 index 0000000..c609746 --- /dev/null +++ b/src/go-test.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +COVERPKG=$1 +COVERPROFILE=$2 +GOLANG_PARALLEL_TESTS=$3 +GOLANG_TEST_EXCLUSIONS=$4 +TAGS=$5 + +variables() { + if [ -z "${GOLANG_PARALLEL_TESTS}" ]; then + if [ "$(uname -s)" = "Darwin" ]; then + GOLANG_PARALLEL_TESTS=$(sysctl -n hw.ncpu) + else + GOLANG_PARALLEL_TESTS=$(nproc) + fi + fi + + if [ -n "${COVERPKG}" ]; then + COVERPKG="-coverpkg=${COVERPKG}" + fi + + if [ -n "${COVERPROFILE}" ]; then + COVERPROFILE="-coverprofile=${COVERPROFILE}" + fi + + if [ -n "${TAGS}" ]; then + TAGS="--tags=${TAGS}" + fi + + echo "COVERPKG: ${COVERPKG}" + echo "COVERPROFILE: ${COVERPROFILE}" + echo "GOLANG_PARALLEL_TESTS: ${GOLANG_PARALLEL_TESTS}" + echo "GOLANG_TEST_EXCLUSIONS: ${GOLANG_TEST_EXCLUSIONS}" + echo "TAGS: ${TAGS}" +} + +run_go_tests() { + go test \ + -p "${GOLANG_PARALLEL_TESTS}" \ + -race \ + -short \ + -v \ + $TAGS \ + $COVERPKG \ + $COVERPROFILE \ + $(go list ./... | grep -v '${GOLANG_TEST_EXCLUSIONS}') +} + +main() { + variables + run_go_tests +} + +main