Skip to content

Commit

Permalink
UID2-3858 verify no of tests run (#141)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cYKatherine authored Dec 24, 2024
1 parent 9305ba2 commit a844855
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/shared-build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}
Expand All @@ -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' }}
Expand Down
11 changes: 10 additions & 1 deletion .github/workflows/shared-publish-to-maven-versioned.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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' }}
Expand Down
70 changes: 70 additions & 0 deletions scripts/compile_java_test_and_verify.sh
Original file line number Diff line number Diff line change
@@ -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!"

0 comments on commit a844855

Please sign in to comment.