From a844855d4223ddc77d8dcbb4ccfde395358a86ab Mon Sep 17 00:00:00 2001 From: Katherine Chen Date: Tue, 24 Dec 2024 16:01:14 +1100 Subject: [PATCH] UID2-3858 verify no of tests run (#141) * Add check for the number of tests run * Add tests for number of tests run * Extract scripts * Add checkout uid2-shared-actions step * Exit when tests_run doesn't have value * Use parameter to pass in values * Change kcc-UID2-3858-verify-no-of-tests-run to v3 --- .github/workflows/shared-build-and-test.yaml | 10 ++- .../shared-publish-to-maven-versioned.yaml | 11 ++- scripts/compile_java_test_and_verify.sh | 70 +++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 scripts/compile_java_test_and_verify.sh diff --git a/.github/workflows/shared-build-and-test.yaml b/.github/workflows/shared-build-and-test.yaml index 414eadcb..44a6b3d0 100644 --- a/.github/workflows/shared-build-and-test.yaml +++ b/.github/workflows/shared-build-and-test.yaml @@ -28,6 +28,13 @@ jobs: steps: - name: Checkout repo uses: actions/checkout@v4 + + - name: Checkout uid2-shared-actions repo + uses: actions/checkout@v4 + with: + ref: v3 + repository: IABTechLab/uid2-shared-actions + path: uid2-shared-actions - name: Set up JDK if: ${{ inputs.vulnerability_scan_only == 'false' }} @@ -38,8 +45,9 @@ jobs: - name: Build and run unit tests if: ${{ inputs.vulnerability_scan_only == 'false' }} - run: mvn -B clean compile test working-directory: ${{ inputs.working_dir }} + run: | + bash uid2-shared-actions/scripts/compile_java_test_and_verify.sh - name: Generate code coverage if: ${{ inputs.vulnerability_scan_only == 'false' }} diff --git a/.github/workflows/shared-publish-to-maven-versioned.yaml b/.github/workflows/shared-publish-to-maven-versioned.yaml index 634ab010..abd2cf1d 100644 --- a/.github/workflows/shared-publish-to-maven-versioned.yaml +++ b/.github/workflows/shared-publish-to-maven-versioned.yaml @@ -59,6 +59,13 @@ jobs: with: fetch-depth: 0 + - name: Checkout uid2-shared-actions repo + uses: actions/checkout@v4 + with: + ref: v3 + repository: IABTechLab/uid2-shared-actions + path: uid2-shared-actions + - name: Set up JDK uses: actions/setup-java@v4 with: @@ -113,9 +120,11 @@ jobs: - name: Compile if: ${{ inputs.publish_to_maven != true }} + env: + EXTRA_FLAGS: "-s settings.xml -Dgpg.passphrase=\"${{ secrets.GPG_PASSPHRASE }}\"" run: | cd ./${{ inputs.working_dir }} - mvn -B -s settings.xml -Dgpg.passphrase="${{ secrets.GPG_PASSPHRASE }}" clean compile test + bash uid2-shared-actions/scripts/compile_java_test_and_verify.sh -s settings.xml -D gpg.passphrase="${{ secrets.GPG_PASSPHRASE }}" - name: Commit pom.xml and version.json if: ${{ steps.checkRelease.outputs.is_release != 'true' }} diff --git a/scripts/compile_java_test_and_verify.sh b/scripts/compile_java_test_and_verify.sh new file mode 100644 index 00000000..4f1713a5 --- /dev/null +++ b/scripts/compile_java_test_and_verify.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +# Function to display usage instructions +usage() { + echo "Usage: $0 [-s SETTINGS_FILE] [-D NAME=VALUE]" + echo " -s SETTINGS_FILE Specify Maven settings file" + echo " -D NAME=VALUE Set a system property (can be used multiple times)" + echo "Any remaining arguments will be passed directly to Maven." + exit 1 +} + +# Initialize variables +settings_file="" +system_properties=() # Use an array for multiple -D options + +# Parse command-line options +while getopts "s:D:" opt; do + case $opt in + s) + settings_file="-s $OPTARG" + ;; + D) + system_properties+=("-D$OPTARG") + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + usage + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + usage + ;; + esac +done + +# Shift off the processed options, leaving only the remaining arguments +shift "$((OPTIND-1))" + +# Construct Maven command +mvn_command="mvn -B ${settings_file}" + +# Add system properties to the command +for prop in "${system_properties[@]}"; do + mvn_command+=" ${prop}" +done + +# Append any remaining arguments to the Maven command +mvn_command+=" $@" + +# Add the fixed part of the Maven command +mvn_command+=" clean compile test" + +echo "DEBUG: Executing Maven command: ${mvn_command}" +${mvn_command} | tee build.log + +tests_run=$(cat build.log | grep "Tests run:" | tail -n 1 | sed 's/.*Tests run: \([0-9]*\).*/\1/') + +echo "DEBUG: tests_run = $tests_run" + +if [ -z "$tests_run" ]; then + echo "WARNING: Could not determine the number of tests run." + exit 1 +fi + +if [ "$tests_run" -eq 0 ]; then + echo "ERROR: No tests were run!" + exit 1 +fi + +echo "INFO: $tests_run tests were run!" \ No newline at end of file