From 1dbaae5e93e24df4bb1c8a498a394ce913f2b129 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 14 Nov 2024 14:23:29 +0000 Subject: [PATCH] add option to exclude directories from go test --- .github/workflows/go-common.yml | 2 ++ go-code-tester/README.md | 4 ++++ go-code-tester/action.yml | 5 +++++ go-code-tester/entrypoint.sh | 12 +++++++++--- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/go-common.yml b/.github/workflows/go-common.yml index 9a374a7..258c33c 100644 --- a/.github/workflows/go-common.yml +++ b/.github/workflows/go-common.yml @@ -21,6 +21,7 @@ env: RACE_DETECTOR: ${{ vars.RACE_DETECTOR || true }} SKIP_TEST: ${{ vars.SKIP_TEST || '' }} RUN_TEST: ${{ vars.RUN_TEST || '' }} + EXCLUDE_DIRECTORIES: ${{ vars.EXCLUDE_DIRECTORIES || '' }} # gosec action GOSEC_EXCLUDES: ${{ vars.GOSEC_EXCLUDES || '' }} @@ -44,6 +45,7 @@ jobs: race-detector: ${{ env.RACE_DETECTOR }} skip-test: ${{ env.SKIP_TEST }} run-test: ${{ env.RUN_TEST }} + exclude-directory: ${{ env.EXCLUDE_DIRECTORIES }} # Check sources for security vulnerabilities security: diff --git a/go-code-tester/README.md b/go-code-tester/README.md index 38904b8..f4d67a8 100644 --- a/go-code-tester/README.md +++ b/go-code-tester/README.md @@ -35,6 +35,8 @@ jobs: skip-test: "TestToSkip" # Optional parameter to specify regex for tests to run run-test: "TestToRun" + # Optional paramter to exlude certain directories from go test. Ex. intregration test folders. + exclude-directory: "DirectoryToExclude|DirectoryToExclude2" ``` The `threshold` for the Action is a coverage percentage threshold that every package must meet. The default `threshold` is 90. @@ -48,3 +50,5 @@ The `race-detector` is an optional boolean parameter to enable or disable the ra The `skip-test` is a regex and passed directly as the -skip option to the `go test` command. The `run-test` is a regex and passed directly as the -run option to the `go test` command. + +The `exclude-directory` is an optional parameter to filter out directories you want to exclude from the `go test` command. diff --git a/go-code-tester/action.yml b/go-code-tester/action.yml index 0821b35..64b8072 100644 --- a/go-code-tester/action.yml +++ b/go-code-tester/action.yml @@ -32,6 +32,10 @@ inputs: description: 'Regex to specify tests to run' required: false default: "" + exclude-directory: + description: 'Name of directory to be excluded from go test' + required: false + default: "" runs: using: 'docker' image: 'Dockerfile' @@ -42,6 +46,7 @@ runs: - ${{ inputs.race-detector }} - ${{ inputs.skip-test }} - ${{ inputs.run-test }} + - ${{ inputs.exclude-directory }} branding: icon: 'shield' color: 'blue' diff --git a/go-code-tester/entrypoint.sh b/go-code-tester/entrypoint.sh index e486094..e46ab67 100755 --- a/go-code-tester/entrypoint.sh +++ b/go-code-tester/entrypoint.sh @@ -14,6 +14,7 @@ SKIP_LIST=$3 RACE_DETECTOR=$4 SKIP_TEST=$5 RUN_TEST=$6 +EXCLUDE_DIRECTORIES=$7 skip_options="" run_options="" @@ -31,10 +32,15 @@ fi go clean -testcache cd ${TEST_FOLDER} -if [[ -z $RACE_DETECTOR ]] || [[ $RACE_DETECTOR == "true" ]]; then - GOEXPERIMENT=nocoverageredesign go test $skip_options -v -short -race -count=1 -cover $run_options ./... > ~/run.log +if [[ -n $EXCLUDE_DIRECTORIES ]]; then + echo "excluding the following directories: $EXCLUDE_DIRECTORIES" + if [[ -z $RACE_DETECTOR ]] || [[ $RACE_DETECTOR == "true" ]]; then + GOEXPERIMENT=nocoverageredesign go test $skip_options -v $(go list ./... | grep -vE $EXCLUDE_DIRECTORIES) -short -race -count=1 -cover $run_options ./... > ~/run.log + else + # Run without the race flag + GOEXPERIMENT=nocoverageredesign go test $skip_options -v $(go list ./... | grep -vE $EXCLUDE_DIRECTORIES) -short -count=1 -cover $run_options ./... > ~/run.log + fi else - # Run without the race flag GOEXPERIMENT=nocoverageredesign go test $skip_options -v -short -count=1 -cover $run_options ./... > ~/run.log fi