Skip to content

Commit

Permalink
feat: [#67] Run component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-bvanb committed Oct 17, 2024
1 parent 6b0f6a7 commit 34cd0f2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ and a [.golangci.yml](https://golangci-lint.run/usage/configuration/).
<!-- markdownlint-enable MD013 -->
## 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`.
Expand All @@ -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.
51 changes: 34 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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"
54 changes: 54 additions & 0 deletions src/go-test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 34cd0f2

Please sign in to comment.