atharvalade commented on code in PR #2920:
URL: https://github.com/apache/iggy/pull/2920#discussion_r2926638781


##########
scripts/run-examples-from-readme.sh:
##########
@@ -0,0 +1,545 @@
+#!/bin/bash
+
+# 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.
+
+set -euo pipefail
+
+# Unified script to run SDK examples from README.md files.
+# Usage: ./scripts/run-examples-from-readme.sh [OPTIONS]
+#
+#   --language LANG   Language to test: rust|go|node|python|java|csharp 
(default: all)
+#   --target TARGET   Cargo target architecture for the server binary
+#   --skip-tls        Skip TLS example tests
+#   --goos GOOS       Go OS target
+#   --goarch GOARCH   Go architecture target
+#   --csharpos OS     C# --os flag
+#   --csharparch ARCH C# --arch flag
+#
+# The script sources shared utilities from scripts/utils.sh, starts the iggy
+# server (built from source), parses example commands from each language's
+# README.md, and executes them. Non-TLS examples run first; then the server
+# is restarted with TLS for TLS-specific examples.
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+# shellcheck source=scripts/utils.sh
+source "${SCRIPT_DIR}/utils.sh"
+
+ROOT_WORKDIR="$(pwd)"
+
+# ---------------------------------------------------------------------------
+# Argument parsing
+# ---------------------------------------------------------------------------
+LANGUAGE="all"
+TARGET=""
+SKIP_TLS=false
+GOOS=""
+GOARCH=""
+CSOS=""
+CSARCH=""
+
+while [[ $# -gt 0 ]]; do
+    case "$1" in
+        --language)   LANGUAGE="$2";  shift 2 ;;
+        --target)     TARGET="$2";    shift 2 ;;
+        --skip-tls)   SKIP_TLS=true;  shift   ;;
+        --goos)       GOOS="$2";      shift 2 ;;
+        --goarch)     GOARCH="$2";    shift 2 ;;
+        --csharpos)   CSOS="$2";      shift 2 ;;
+        --csharparch) CSARCH="$2";    shift 2 ;;
+        *)
+            echo "Unknown option: $1"
+            echo "Usage: $0 [--language LANG] [--target TARGET] [--skip-tls] 
[--goos GOOS] [--goarch GOARCH] [--csharpos OS] [--csharparch ARCH]"
+            exit 1
+            ;;
+    esac
+done
+
+# ---------------------------------------------------------------------------
+# Helpers
+# ---------------------------------------------------------------------------
+
+# Run a full non-TLS + TLS cycle for one language.
+# Arguments:
+#   $1 - language label (for logging)
+#   $2 - working directory (relative to repo root, or "." for root)
+#   $3 - space-separated list of README files (relative to workdir)
+#   $4 - grep pattern for non-TLS commands
+#   $5 - grep exclude pattern for non-TLS commands (empty = no exclude)
+#   $6 - grep pattern for TLS commands (empty = no TLS examples)
+#   $7 - per-command timeout in seconds (0 = no timeout)
+#   $8 - extra server args (e.g. "--fresh")
+# shellcheck disable=SC2329
+run_language_examples() {
+    local lang="$1"
+    local workdir="$2"
+    local readme_files="$3"
+    local grep_pattern="$4"
+    local grep_exclude="$5"
+    local tls_grep_pattern="$6"
+    local cmd_timeout="$7"
+    local server_extra_args="$8"
+
+    echo ""
+    echo "============================================================"
+    echo "  Running ${lang} examples"
+    echo "============================================================"
+    echo ""
+
+    EXAMPLES_EXIT_CODE=0
+
+    # --- Non-TLS pass ---
+    cleanup_server_state
+    # shellcheck disable=SC2086
+    start_plain_server ${server_extra_args}
+    wait_for_server_ready "${lang}"
+
+    if [ "${workdir}" != "." ]; then
+        cd "${workdir}"
+    fi
+
+    for readme in ${readme_files}; do
+        if [ "${EXAMPLES_EXIT_CODE}" -ne 0 ]; then
+            break
+        fi
+        if [ -n "${grep_exclude}" ]; then
+            # Build a combined pattern that filters out the exclude
+            local filtered
+            filtered=$(grep -E "${grep_pattern}" "${readme}" 2>/dev/null | 
grep -v "${grep_exclude}" || true)
+            if [ -n "${filtered}" ]; then
+                while IFS= read -r command; do
+                    command=$(echo "${command}" | tr -d '`' | sed 's/^#.*//')
+                    if [ -z "${command}" ]; then
+                        continue
+                    fi
+                    if declare -f TRANSFORM_COMMAND >/dev/null 2>&1; then
+                        command=$(TRANSFORM_COMMAND "${command}")
+                    fi
+                    echo -e "\e[33mChecking command from ${readme}:\e[0m 
${command}"
+                    echo ""
+                    set +e
+                    if [ "${cmd_timeout}" -gt 0 ] 2>/dev/null; then
+                        eval "portable_timeout ${cmd_timeout} ${command}"
+                        local test_exit_code=$?
+                        if [[ ${test_exit_code} -ne 0 && ${test_exit_code} -ne 
124 ]]; then
+                            EXAMPLES_EXIT_CODE=${test_exit_code}
+                        fi
+                    else
+                        eval "${command}"
+                        EXAMPLES_EXIT_CODE=$?
+                    fi
+                    set -e
+                    if [ "${EXAMPLES_EXIT_CODE}" -ne 0 ]; then
+                        echo ""
+                        echo -e "\e[31mCommand failed:\e[0m ${command}"
+                        echo ""
+                        break
+                    fi
+                    sleep 2
+                done <<< "${filtered}"
+            fi
+        else
+            run_readme_commands "${readme}" "${grep_pattern}" "${cmd_timeout}"
+        fi
+    done
+
+    cd "${ROOT_WORKDIR}"
+    stop_server
+
+    # --- TLS pass ---
+    if [ "${EXAMPLES_EXIT_CODE}" -eq 0 ] && [ "${SKIP_TLS}" = false ] && [ -n 
"${tls_grep_pattern}" ]; then
+        local has_tls=false
+        for readme in ${readme_files}; do
+            local readme_path="${readme}"
+            if [ "${workdir}" != "." ]; then
+                readme_path="${workdir}/${readme}"
+            fi
+            if [ -f "${readme_path}" ] && grep -qE "${tls_grep_pattern}" 
"${readme_path}" 2>/dev/null; then
+                has_tls=true
+                break
+            fi
+        done
+
+        if [ "${has_tls}" = true ]; then
+            echo ""
+            echo "=== Running ${lang} TLS examples ==="
+            echo ""
+
+            cleanup_server_state
+            # shellcheck disable=SC2086
+            start_tls_server ${server_extra_args}
+            wait_for_server_ready "${lang} TLS"
+
+            if [ "${workdir}" != "." ]; then
+                cd "${workdir}"
+            fi
+
+            for readme in ${readme_files}; do
+                if [ "${EXAMPLES_EXIT_CODE}" -ne 0 ]; then
+                    break
+                fi
+                run_readme_commands "${readme}" "${tls_grep_pattern}" 
"${cmd_timeout}"
+            done
+
+            cd "${ROOT_WORKDIR}"
+            stop_server
+        fi
+    fi
+
+    report_result "${EXAMPLES_EXIT_CODE}"
+    return "${EXAMPLES_EXIT_CODE}"
+}
+
+# ---------------------------------------------------------------------------
+# Per-language runners
+# ---------------------------------------------------------------------------
+
+# shellcheck disable=SC2329
+run_rust_examples() {
+    resolve_server_binary "${TARGET}"
+    resolve_cli_binary "${TARGET}"
+
+    # Transform: inject --target flag when cross-compiling
+    if [ -n "${TARGET}" ]; then
+        TRANSFORM_COMMAND() {
+            echo "$1" | sed "s|cargo r |cargo r --target ${TARGET} |g" | sed 
"s|cargo run |cargo run --target ${TARGET} |g"
+        }
+    else
+        unset -f TRANSFORM_COMMAND 2>/dev/null || true
+    fi
+
+    # First run CLI commands from root README.md (these match `cargo r --bin 
iggy -- `)
+    echo ""
+    echo "============================================================"
+    echo "  Running Rust CLI + example tests"
+    echo "============================================================"
+    echo ""
+
+    EXAMPLES_EXIT_CODE=0
+    cleanup_server_state
+    start_plain_server
+    wait_for_server_ready "Rust"
+
+    # CLI commands from root README (abbreviated `cargo r`, not `cargo run`)
+    run_readme_commands "README.md" '^\`cargo r --bin iggy -- '
+    if [ "${EXAMPLES_EXIT_CODE}" -ne 0 ]; then
+        cd "${ROOT_WORKDIR}"
+        stop_server
+        report_result "${EXAMPLES_EXIT_CODE}"
+        return "${EXAMPLES_EXIT_CODE}"

Review Comment:
   added a pre-flight callback parameter to `run_language_examples`. Both Rust 
and C# now use it for CLI commands instead of duplicating the loop



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to