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..4e190f0 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,32 +119,50 @@ 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 }}') - code_coverage_actual=$(go tool cover -func profile.cov |\ + ${GITHUB_ACTION_PATH}/src/go-test.sh \ + "./..." \ + "profile.cov" \ + "${{ inputs.golang-number-of-tests-in-parallel }}" \ + "${{ inputs.golang-unit-tests-exclusions }}" \ + "integration" + + code_coverage_output=$(go tool cover -func profile.cov) + code_coverage_actual=$(echo ${code_coverage_output} |\ grep total: |\ awk '{print $3}' |\ sed 's/%//') + + echo "Code coverage overview:" + echo "${code_coverage_output}" + if (( $(echo "${{ inputs.code_coverage_expected }} > ${code_coverage_actual}" | bc -l) )); then echo "The actual code coverage: '${code_coverage_actual}' is too low. Expected: '${{ inputs.code_coverage_expected }}'." exit 1 + elif (( $(echo "${code_coverage_actual} > ${{ inputs.code_coverage_expected }}" | bc -l) )); then + echo "The actual code coverage: '${code_coverage_actual}' exceeds the expected coverage. Please adjust the threshold to align with the 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..71741d2 --- /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 \ + $COVERPKG \ + $COVERPROFILE \ + $TAGS \ + $(go list $TAGS ./... | grep -v ${GOLANG_TEST_EXCLUSIONS}) +} + +main() { + variables + run_go_tests +} + +main