This is an automated email from the ASF dual-hosted git repository.
wenjin272 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-agents.git
The following commit(s) were added to refs/heads/main by this push:
new 8f201cb0 [hotfix][tools] Report RAT validation dependencies correctly
(#996)
8f201cb0 is described below
commit 8f201cb0389ac2df30cd9393818dd5a259d4dcb6
Author: kaiwangleo <[email protected]>
AuthorDate: Fri Aug 21 16:30:25 2026 +0800
[hotfix][tools] Report RAT validation dependencies correctly (#996)
Generated-by: OpenAI Codex Desktop 26.803.41515 (GPT-5.6 Sol)
Co-authored-by: Leo Wang <[email protected]>
---
tools/check-license.sh | 88 ++++++++++++++-----
tools/test/unit/check_license.bats | 168 +++++++++++++++++++++++++++++++++++++
2 files changed, 236 insertions(+), 20 deletions(-)
diff --git a/tools/check-license.sh b/tools/check-license.sh
index 4439458c..32091b38 100755
--- a/tools/check-license.sh
+++ b/tools/check-license.sh
@@ -20,41 +20,89 @@
# NOTE: This script is adapted from the Apache Spark project.
-acquire_rat_jar () {
+validate_rat_jar() {
+ local jar_cmd
-
URL="https://repo.maven.apache.org/maven2/org/apache/rat/apache-rat/${RAT_VERSION}/apache-rat-${RAT_VERSION}.jar"
+ if command -v unzip >/dev/null 2>&1; then
+ if unzip -tq "$JAR" >/dev/null 2>&1; then
+ return 0
+ fi
+ return 1
+ elif [ -n "${JAVA_HOME:-}" ] && [ -x "$JAVA_HOME/bin/jar" ]; then
+ jar_cmd="$JAVA_HOME/bin/jar"
+ elif command -v jar >/dev/null 2>&1; then
+ jar_cmd="$(command -v jar)"
+ else
+ printf "Cannot validate Apache RAT: install a JDK with 'jar' or install
'unzip'.\n" >&2
+ return 2
+ fi
+
+ if "$jar_cmd" tf "$JAR" >/dev/null 2>&1; then
+ return 0
+ fi
+ return 1
+}
+
+acquire_rat_jar() {
+ local downloaded=false validation_status
+
URL="https://repo.maven.apache.org/maven2/org/apache/rat/apache-rat/${RAT_VERSION}/apache-rat-${RAT_VERSION}.jar"
JAR="$rat_jar"
- # Download rat launch jar if it hasn't been downloaded yet
if [ ! -f "$JAR" ]; then
- # Download
+ downloaded=true
printf "Attempting to fetch rat\n"
JAR_DL="${JAR}.part"
- if [ $(command -v curl) ]; then
- curl -L --silent "${URL}" > "$JAR_DL" && mv "$JAR_DL" "$JAR"
- elif [ $(command -v wget) ]; then
- wget --quiet ${URL} -O "$JAR_DL" && mv "$JAR_DL" "$JAR"
+ rm -f "$JAR_DL"
+ if command -v curl >/dev/null 2>&1; then
+ if ! curl --fail --silent --show-error --location --output "$JAR_DL"
"$URL"; then
+ rm -f "$JAR_DL"
+ printf "Failed to download Apache RAT from %s.\n" "$URL" >&2
+ return 1
+ fi
+ elif command -v wget >/dev/null 2>&1; then
+ if ! wget --no-verbose --output-document="$JAR_DL" "$URL"; then
+ rm -f "$JAR_DL"
+ printf "Failed to download Apache RAT from %s.\n" "$URL" >&2
+ return 1
+ fi
else
- printf "You do not have curl or wget installed, please install rat
manually.\n"
- exit -1
+ printf "Cannot download Apache RAT: install 'curl' or 'wget'.\n" >&2
+ return 1
+ fi
+ if ! mv "$JAR_DL" "$JAR"; then
+ rm -f "$JAR_DL"
+ printf "Failed to store the downloaded Apache RAT JAR at %s.\n" "$JAR"
>&2
+ return 1
fi
fi
- unzip -tq "$JAR" &> /dev/null
- if [ $? -ne 0 ]; then
- # We failed to download
- rm "$JAR"
- printf "Our attempt to download rat locally to ${JAR} failed. Please
install rat manually.\n"
- exit -1
+ validate_rat_jar
+ validation_status=$?
+ if [ "$validation_status" -eq 2 ]; then
+ if [ "$downloaded" = true ]; then
+ rm -f "$JAR"
+ printf "Cannot validate the downloaded Apache RAT JAR: install jar or
unzip.\n" >&2
+ return 1
+ fi
+ printf "Warning: cannot validate cached Apache RAT JAR at %s; install jar
or unzip.\n" "$JAR" >&2
+ return 0
+ elif [ "$validation_status" -ne 0 ]; then
+ rm -f "$JAR"
+ printf "The Apache RAT JAR at %s is invalid.\n" "$JAR" >&2
+ return 1
fi
}
+if [[ "${CHECK_LICENSE_SOURCE_ONLY:-}" == "1" ]]; then
+ return 0
+fi
+
# Go to the project root directory
FWDIR="$(cd "`dirname "$0"`"/..; pwd)"
cd "$FWDIR"
-if test -x "$JAVA_HOME/bin/java"; then
+if [ -n "${JAVA_HOME:-}" ] && test -x "$JAVA_HOME/bin/java"; then
declare java_cmd="$JAVA_HOME/bin/java"
else
declare java_cmd=java
@@ -64,8 +112,8 @@ export RAT_VERSION=0.16.1
export rat_jar="$FWDIR"/lib/apache-rat-${RAT_VERSION}.jar
mkdir -p "$FWDIR"/lib
-[[ -f "$rat_jar" ]] || acquire_rat_jar || {
- echo "Download failed. Obtain the rat jar manually and place it at
$rat_jar"
+acquire_rat_jar || {
+ echo "Unable to acquire a valid RAT JAR at $rat_jar"
exit 1
}
@@ -85,4 +133,4 @@ if test ! -z "$ERRORS"; then
exit 1
else
echo -e "RAT checks passed."
-fi
\ No newline at end of file
+fi
diff --git a/tools/test/unit/check_license.bats
b/tools/test/unit/check_license.bats
new file mode 100644
index 00000000..d4691988
--- /dev/null
+++ b/tools/test/unit/check_license.bats
@@ -0,0 +1,168 @@
+#!/usr/bin/env bats
+
+################################################################################
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+################################################################################
+
+setup() {
+ load '../helpers/shim'
+ shim_setup
+ CHECK_LICENSE_SOURCE_ONLY=1
+ source "${BATS_TEST_DIRNAME}/../../check-license.sh"
+ unset CHECK_LICENSE_SOURCE_ONLY
+
+
+ RAT_VERSION="test"
+ rat_jar="$BATS_TEST_TMPDIR/apache-rat-test.jar"
+ JAR="$rat_jar"
+ JAVA_HOME="$BATS_TEST_TMPDIR/missing-java-home"
+}
+
+@test "reports missing validation tools without deleting an existing JAR" {
+ touch "$rat_jar"
+ shim_bin_missing jar
+ shim_bin_missing unzip
+
+ run acquire_rat_jar
+
+ [ "$status" -eq 0 ]
+ [[ "$output" == *"Warning: cannot validate cached Apache RAT JAR"* ]]
+ [ -f "$rat_jar" ]
+}
+
+@test "rejects and removes an invalid existing JAR" {
+ touch "$rat_jar"
+ shim_bin_missing jar
+ shim_bin unzip 1
+
+ run acquire_rat_jar
+
+ [ "$status" -ne 0 ]
+ [[ "$output" == *"is invalid"* ]]
+ [ ! -f "$rat_jar" ]
+}
+
+@test "treats unzip status 2 as an invalid cached JAR" {
+ touch "$rat_jar"
+ shim_bin_missing jar
+ shim_bin unzip 2
+
+ run acquire_rat_jar
+
+ [ "$status" -ne 0 ]
+ [[ "$output" == *"is invalid"* ]]
+ [ ! -f "$rat_jar" ]
+}
+
+@test "does not download an existing valid JAR" {
+ touch "$rat_jar"
+ shim_bin_missing jar
+ shim_bin unzip
+ shim_bin curl
+
+ acquire_rat_jar
+
+ [ "$(shim_call_count curl)" = "0" ]
+ [ "$(shim_call_count unzip)" = "1" ]
+}
+
+@test "reports a download failure and removes the partial file" {
+ shim_bin_script curl 'prev=""; for arg in "$@"; do [[ "$prev" ==
"--output" || "$prev" == "-o" ]] && : > "$arg"; prev="$arg"; done; exit 22'
+
+ run acquire_rat_jar
+
+ [ "$status" -ne 0 ]
+ [[ "$output" == *"Failed to download Apache RAT"* ]]
+ [ ! -e "${rat_jar}.part" ]
+ [ ! -e "$rat_jar" ]
+}
+
+@test "downloads with curl safety flags and validates the result" {
+ shim_bin_script curl 'prev=""; for arg in "$@"; do if [[ "$prev" ==
"--output" || "$prev" == "-o" ]]; then touch "$arg"; exit 0; fi; prev="$arg";
done; exit 64'
+ shim_bin_missing jar
+ shim_bin unzip
+
+ acquire_rat_jar
+
+ [ -f "$rat_jar" ]
+ run cat "$SHIM_CALLS/curl.log"
+ [[ "$output" == *"--fail"* ]]
+ [[ "$output" == *"--show-error"* ]]
+ [[ "$output" == *"--location"* ]]
+ [ "$(shim_call_count unzip)" = "1" ]
+}
+
+@test "fails closed when a downloaded JAR cannot be validated" {
+ shim_bin_script curl 'prev=""; for arg in "$@"; do if [[ "$prev" ==
"--output" || "$prev" == "-o" ]]; then : > "$arg"; fi; prev="$arg"; done'
+ shim_bin_missing jar
+ shim_bin_missing unzip
+
+ run acquire_rat_jar
+
+ [ "$status" -ne 0 ]
+ [[ "$output" == *"Cannot validate the downloaded Apache RAT JAR"* ]]
+ [ ! -e "$rat_jar" ]
+ [ ! -e "${rat_jar}.part" ]
+}
+
+@test "validates a cached JAR with jar when unzip is unavailable" {
+ touch "$rat_jar"
+ shim_bin_missing unzip
+ shim_bin jar
+
+ run acquire_rat_jar
+
+ [ "$status" -eq 0 ]
+ [ "$(shim_call_count jar)" = "1" ]
+ [ -f "$rat_jar" ]
+}
+
+@test "rejects an invalid cached JAR with jar when unzip is unavailable" {
+ touch "$rat_jar"
+ shim_bin_missing unzip
+ shim_bin jar 1
+
+ run acquire_rat_jar
+
+ [ "$status" -ne 0 ]
+ [[ "$output" == *"is invalid"* ]]
+ [ ! -f "$rat_jar" ]
+}
+
+@test "treats jar status 2 as an invalid cached JAR" {
+ touch "$rat_jar"
+ shim_bin_missing unzip
+ shim_bin jar 2
+
+ run acquire_rat_jar
+
+ [ "$status" -ne 0 ]
+ [[ "$output" == *"is invalid"* ]]
+ [ ! -f "$rat_jar" ]
+}
+
+@test "prefers unzip when both validation tools are available" {
+ touch "$rat_jar"
+ shim_bin unzip
+ shim_bin jar 1
+
+ run acquire_rat_jar
+
+ [ "$status" -eq 0 ]
+ [ "$(shim_call_count unzip)" = "1" ]
+ [ "$(shim_call_count jar)" = "0" ]
+}