|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script generates a maven command to test unit and integration tests for |
| 4 | +# the repo. The outputted maven command will be in the rough following format |
| 5 | +# `mvn verify ... -D{dependency.name}.version={dependency.version]`. The variables |
| 6 | +# ${dependency.name} and ${dependency.version} are parsed from the input to the script. |
| 7 | +# |
| 8 | +# Default invocation ./.github/scripts/test_dependency_compatibility.sh will use the default |
| 9 | +# upper-bounds dependency file at the root of the repo. |
| 10 | +# There are two potential inputs to the script: |
| 11 | +# 1. -f {file}: Custom file/path for the upper-bound dependencies to test |
| 12 | +# 2. -l {deps_list}: Comma-separated list of dependencies to test (e.g. protobuf=4.31.0,guava=33.4.8-jre) |
| 13 | +# Note: Do not include the `-D` prefix or `.version` suffix. Those values will be appended when generating |
| 14 | +# the maven command. |
| 15 | +# |
| 16 | +# If both inputs are supplied, the deps_list input has precedence. For Github Actions workflow, |
| 17 | +# the default workflow will run with the upper-bounds file. A `workflow_dispatch` option takes in |
| 18 | +# an input for the deps_list to manually run a subset of dependencies. |
| 19 | +# |
| 20 | +# The default upper-bound dependencies file is `dependencies.txt` located in the root |
| 21 | +# of sdk-platform-java. The upper-bound dependencies file will be in the format of: |
| 22 | +# ${dependency.name}=${dependency.version} |
| 23 | + |
| 24 | +set -ex |
| 25 | + |
| 26 | +function print_help() { |
| 27 | + echo "Unexpected input argument for this script." |
| 28 | + echo "Use -f {file} for the directory of the upper-bound dependencies file." |
| 29 | + echo "Use -l {deps_list} for a comma-separated list of dependencies to test (Format: dep1=1.0,dep2=2.0)" |
| 30 | +} |
| 31 | + |
| 32 | +# Function to parse a dependency string and append it to the Maven command |
| 33 | +function add_dependency_to_maven_command() { |
| 34 | + local dep_pair=$1 |
| 35 | + if [[ ! "${dep_pair}" =~ .*=.* ]]; then |
| 36 | + echo "Malformed dependency string: ${dep_pair}. Expected format: dependency=version" |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + local dependency=$(echo "${dep_pair}" | cut -d'=' -f1 | tr -d '[:space:]') |
| 40 | + local version=$(echo "${dep_pair}" | cut -d'=' -f2 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') |
| 41 | + MAVEN_COMMAND+=" -D${dependency}.version=${version}" |
| 42 | +} |
| 43 | + |
| 44 | +# Default to the upper bounds file in the root of the repo |
| 45 | +file='dependencies.txt' |
| 46 | +dependency_list='' |
| 47 | + |
| 48 | +# The colon (:) after the letter means that there is an input associated with the flag |
| 49 | +while getopts 'f:l:' flag; do |
| 50 | + case "${flag}" in |
| 51 | + f) file="${OPTARG}" ;; |
| 52 | + l) dependency_list="${OPTARG}" ;; |
| 53 | + *) print_help && exit 1 |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +# Error if both the file and deps_list inputs is empty |
| 58 | +if [[ -z "${file}" && -z "${dependency_list}" ]]; then |
| 59 | + print_help && exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +MAVEN_COMMAND="mvn verify -Penable-integration-tests -Dclirr.skip -Dcheckstyle.skip -Dfmt.skip -Denforcer.skip " |
| 63 | + |
| 64 | +# Check if a list of dependencies was provided as an argument. If the list of dependency inputted |
| 65 | +# is empty, then run with the upper-bound dependencies file |
| 66 | +if [ -z "${dependency_list}" ]; then |
| 67 | + UPPER_BOUND_DEPENDENCY_FILE=$file |
| 68 | + |
| 69 | + if [ ! -e "${UPPER_BOUND_DEPENDENCY_FILE}" ]; then |
| 70 | + echo "The inputted upper-bound dependency file '${UPPER_BOUND_DEPENDENCY_FILE}' cannot be found" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + # Read the file line by line |
| 75 | + while IFS= read -r line; do |
| 76 | + # Ignore any comments and blank lines |
| 77 | + if [[ "${line}" =~ ^[[:space:]]*# ]] || [[ -z "${line}" ]]; then |
| 78 | + continue |
| 79 | + fi |
| 80 | + add_dependency_to_maven_command "${line}" |
| 81 | + done < "${UPPER_BOUND_DEPENDENCY_FILE}" |
| 82 | +else # This else block means that a list of dependencies was inputted |
| 83 | + # Set the Internal Field Separator (IFS) to a comma. |
| 84 | + # This tells 'read' to split the string by commas into an array named DEPS. |
| 85 | + # The 'read -ra' command reads the input into an array. |
| 86 | + IFS=',' read -ra DEPS <<< "${dependency_list}" |
| 87 | + |
| 88 | + # Loop through each item in the DEPS array. |
| 89 | + for DEP_PAIR in "${DEPS[@]}"; do |
| 90 | + # Skip any empty items that might result from trailing commas. |
| 91 | + if [ -z "${DEP_PAIR}" ]; then |
| 92 | + continue |
| 93 | + fi |
| 94 | + add_dependency_to_maven_command "${DEP_PAIR}" |
| 95 | + done |
| 96 | +fi |
| 97 | + |
| 98 | +# Run the generated maven command to test with the dependency versions |
| 99 | +$MAVEN_COMMAND |
0 commit comments