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 d72417f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 17 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.
42 changes: 26 additions & 16 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,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}' |\
Expand All @@ -149,3 +145,17 @@ runs:
echo "The actual code coverage: '${code_coverage_actual}' is too low. Expected: '${{ inputs.code_coverage_expected }}'."
exit 1
fi
echo "Code coverage overview:"
cat profile.cov
#
# 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"
53 changes: 53 additions & 0 deletions src/go-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/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 \
$(go list $TAGS ./... | grep -v '${GOLANG_TEST_EXCLUSIONS}')
}

main() {
variables
run_go_tests
}

main

0 comments on commit d72417f

Please sign in to comment.