From e52a682b5349e6a52744d0858dad07b32bc12ff6 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 11 Dec 2024 11:52:02 +0100 Subject: [PATCH 01/14] Add scripts to simplify local testing --- composer.json | 26 +++++---- scripts/run-phpunit.sh | 21 +++++++ scripts/run-tests-with-server.sh | 17 ++++++ scripts/start-test-environment-wrapper.sh | 11 ++++ scripts/start-test-environment.sh | 71 +++++++++++++++++++++++ scripts/stop-test-environment.sh | 41 +++++++++++++ 6 files changed, 176 insertions(+), 11 deletions(-) create mode 100755 scripts/run-phpunit.sh create mode 100755 scripts/run-tests-with-server.sh create mode 100755 scripts/start-test-environment-wrapper.sh create mode 100755 scripts/start-test-environment.sh create mode 100755 scripts/stop-test-environment.sh diff --git a/composer.json b/composer.json index 98477ced9..3e0d6af06 100644 --- a/composer.json +++ b/composer.json @@ -87,25 +87,29 @@ "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf" ], "test": [ - "@php ./vendor/phpunit/phpunit/phpunit --no-coverage" - ], - "test10": [ - "@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist --no-coverage" + "./scripts/run-phpunit.sh --no-coverage" ], "coverage": [ - "@php ./vendor/phpunit/phpunit/phpunit" + "./scripts/run-phpunit.sh" + ], + "testserver:start": [ + "./scripts/start-test-environment.sh" + ], + "testserver:stop": [ + "./scripts/stop-test-environment.sh" ], - "coverage10": [ - "@php ./vendor/phpunit/phpunit/phpunit -c phpunit10.xml.dist" + "test:withserver": [ + "./scripts/run-tests-with-server.sh" ] }, "scripts-descriptions": { "lint": "Lint PHP files to find parse errors.", "checkcs": "Check the entire codebase for code-style issues.", "fixcs": "Fix all auto-fixable code-style issues in the entire codebase.", - "test": "Run the unit tests on PHPUnit 5.x - 9.x without code coverage.", - "test10": "Run the unit tests on PHPUnit 10.x without code coverage.", - "coverage": "Run the unit tests on PHPUnit 5.x - 9.x with code coverage.", - "coverage10": "Run the unit tests on PHPUnit 10.x with code coverage." + "test": "Run the unit tests without code coverage (auto-detects PHPUnit version)", + "coverage": "Run the unit tests with code coverage (auto-detects PHPUnit version)", + "testserver:start": "Start the test environment including test server and proxy servers (Note: environment variables will only be available in a sourced shell)", + "testserver:stop": "Stop all running test environment servers", + "test:withserver": "Start test servers, run PHPUnit tests with appropriate config, then stop servers" } } diff --git a/scripts/run-phpunit.sh b/scripts/run-phpunit.sh new file mode 100755 index 000000000..d704f864b --- /dev/null +++ b/scripts/run-phpunit.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Get the directory of this script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" + +# Detect PHPUnit version +PHPUNIT_VERSION=$("${PROJECT_ROOT}/vendor/bin/phpunit" --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+') + +# Determine if we should include coverage based on argument +COVERAGE_ARG="" +if [ "$1" != "--no-coverage" ]; then + COVERAGE_ARG="--coverage-html build/coverage" +fi + +# Run the tests with the appropriate config +if printf '%s\n%s\n' "10.0" "$PHPUNIT_VERSION" | sort -V -C; then + "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist $COVERAGE_ARG +else + "${PROJECT_ROOT}/vendor/bin/phpunit" $COVERAGE_ARG +fi \ No newline at end of file diff --git a/scripts/run-tests-with-server.sh b/scripts/run-tests-with-server.sh new file mode 100755 index 000000000..f128439a5 --- /dev/null +++ b/scripts/run-tests-with-server.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Get the directory of this script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" + +# Source the environment setup +source "${SCRIPT_DIR}/start-test-environment.sh" || exit 1 + +# Run the tests +"${SCRIPT_DIR}/run-phpunit.sh" --no-coverage +TEST_EXIT_CODE=$? + +# Stop the test environment +source "${SCRIPT_DIR}/stop-test-environment.sh" + +exit $TEST_EXIT_CODE \ No newline at end of file diff --git a/scripts/start-test-environment-wrapper.sh b/scripts/start-test-environment-wrapper.sh new file mode 100755 index 000000000..18d3f94e6 --- /dev/null +++ b/scripts/start-test-environment-wrapper.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Get the directory of this script +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Source the main script +if source "${SCRIPT_DIR}/start-test-environment.sh"; then + exit 0 +else + exit 1 +fi \ No newline at end of file diff --git a/scripts/start-test-environment.sh b/scripts/start-test-environment.sh new file mode 100755 index 000000000..82580c336 --- /dev/null +++ b/scripts/start-test-environment.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +# Ensure the script is being sourced +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + echo "Error: This script must be sourced to set environment variables" + echo "Usage: source scripts/start-test-environment.sh" + exit 1 +fi + +# Store script dir for relative paths +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +PID_DIR="${PROJECT_ROOT}/.test-pids" + +# Check if mitmproxy is installed +if ! command -v mitmdump &> /dev/null; then + echo "Error: mitmproxy is not installed. Please install it with: pip3 install mitmproxy" + return 1 +fi + +# Get mitmproxy version and compare with minimum required (using 9.0.0 as minimum) +MITM_VERSION=$(mitmdump --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1) +MINIMUM_VERSION="11.0.2" + +if ! printf '%s\n%s\n' "$MINIMUM_VERSION" "$MITM_VERSION" | sort -V -C; then + echo "Error: mitmproxy version $MITM_VERSION is too old" + echo "Please upgrade to version $MINIMUM_VERSION or newer with: pip3 install --upgrade mitmproxy" + return 1 +fi + +echo "Found mitmproxy version $MITM_VERSION" + +# Create directory for PID files if it doesn't exist +mkdir -p "$PID_DIR" + +# Start the test server +echo "Starting test server..." +PORT=8080 "${PROJECT_ROOT}/vendor/bin/start.sh" +echo $! > "${PID_DIR}/test-server.pid" +export REQUESTS_TEST_HOST_HTTP=localhost:8080 + +# Start proxy servers +echo "Starting proxy servers..." +PORT=9002 "${PROJECT_ROOT}/tests/utils/proxy/start.sh" +echo $! > "${PID_DIR}/proxy-server.pid" + +PORT=9003 AUTH="test:pass" "${PROJECT_ROOT}/tests/utils/proxy/start.sh" +echo $! > "${PID_DIR}/proxy-auth-server.pid" + +# Set environment variables +export REQUESTS_HTTP_PROXY=localhost:9002 +export REQUESTS_HTTP_PROXY_AUTH=localhost:9003 +export REQUESTS_HTTP_PROXY_AUTH_USER=test +export REQUESTS_HTTP_PROXY_AUTH_PASS=pass + +# Wait for servers to be ready +echo "Waiting for servers to be ready..." +sleep 2 + +# Test server connections +echo "Testing server connections..." +curl -s -I http://localhost:8080 > /dev/null || { echo "Test server not responding"; return 1; } +curl -s -I http://localhost:9002 > /dev/null || { echo "Proxy server not responding"; return 1; } + +echo "Test environment is ready!" +echo "Environment variables set:" +echo "REQUESTS_TEST_HOST_HTTP=localhost:8080" +echo "REQUESTS_HTTP_PROXY=localhost:9002" +echo "REQUESTS_HTTP_PROXY_AUTH=localhost:9003" + +return 0 \ No newline at end of file diff --git a/scripts/stop-test-environment.sh b/scripts/stop-test-environment.sh new file mode 100755 index 000000000..86518d17b --- /dev/null +++ b/scripts/stop-test-environment.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Store script dir for relative paths +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +PID_DIR="${PROJECT_ROOT}/.test-pids" + +# Function to safely kill a process +kill_process() { + local pid_file="$1" + if [ -f "$pid_file" ]; then + pid=$(cat "$pid_file") + if kill -0 "$pid" 2>/dev/null; then + echo "Stopping process $pid" + kill "$pid" + rm "$pid_file" + else + echo "Process $pid not running" + rm "$pid_file" + fi + fi +} + +# Stop all servers +echo "Stopping test environment..." + +# Stop test server +kill_process "${PID_DIR}/test-server.pid" +"${PROJECT_ROOT}/vendor/bin/stop.sh" + +# Stop proxy servers +PORT=9002 "${PROJECT_ROOT}/tests/utils/proxy/stop.sh" +kill_process "${PID_DIR}/proxy-server.pid" + +PORT=9003 "${PROJECT_ROOT}/tests/utils/proxy/stop.sh" +kill_process "${PID_DIR}/proxy-auth-server.pid" + +# Clean up PID directory if empty +rmdir "${PID_DIR}" 2>/dev/null || true + +echo "Test environment stopped" \ No newline at end of file From 8308e638cde67ae4108cd4e2f9d50bc2ff82b925 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 11 Dec 2024 11:57:48 +0100 Subject: [PATCH 02/14] Adapt the CONTRIBUTING.md file with the testing flow --- .github/CONTRIBUTING.md | 65 ++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index f1ac8782f..e6b15b8eb 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -109,28 +109,65 @@ Code coverage is monitored for every PR and for the code base as a whole using [ ### Running the Tests +There are several ways to run the tests locally: + +#### 1. Using Composer Scripts (Recommended) + +The simplest way to run tests is using the Composer scripts: + +```bash +# Run tests without coverage +composer test + +# Run tests with coverage +composer coverage + +# Run tests with automatic test server management +composer test:withserver +``` + +#### 2. Manual Test Environment Management + +If you need more control over the test environment, you can manage it manually: + +```bash +# Start all test servers and set environment variables +# We need to source the script to properly set environment variables in our shell +source scripts/start-test-environment.sh + +# Now run your tests +composer test + +# When done, stop all servers +./scripts/stop-test-environment.sh +``` + +Note: The environment scripts must be sourced (using `source` or `.`) to properly set environment variables in your shell. + +#### 3. Individual Component Control + +For debugging or development, you might want to manage individual components: + ```bash -# Start the test server +# Just the test server PORT=8080 vendor/bin/start.sh -export "REQUESTS_TEST_HOST_HTTP=localhost:8080" +export REQUESTS_TEST_HOST_HTTP=localhost:8080 -# Start the proxy server +# Just the proxy servers PORT=9002 tests/utils/proxy/start.sh PORT=9003 AUTH="test:pass" tests/utils/proxy/start.sh -export "REQUESTS_HTTP_PROXY=localhost:9002" -export "REQUESTS_HTTP_PROXY_AUTH=localhost:9003" -export "REQUESTS_HTTP_PROXY_AUTH_USER=test" -export "REQUESTS_HTTP_PROXY_AUTH_PASS=pass" +export REQUESTS_HTTP_PROXY=localhost:9002 +export REQUESTS_HTTP_PROXY_AUTH=localhost:9003 +export REQUESTS_HTTP_PROXY_AUTH_USER=test +export REQUESTS_HTTP_PROXY_AUTH_PASS=pass +``` -# Run the tests -composer test +Remember to stop any servers you start: -# Stop the proxy server -PORT=9002 tests/utils/proxy/stop.sh +```bash +vendor/bin/stop.sh # Stop test server +PORT=9002 tests/utils/proxy/stop.sh # Stop proxy servers PORT=9003 tests/utils/proxy/stop.sh - -# Stop the test server -vendor/bin/stop.sh ``` To run the test with code coverage, use `composer coverage` instead. From 1c076c9929f2068a077e553f687532a08393b078 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 11 Dec 2024 12:18:49 +0100 Subject: [PATCH 03/14] Improve POSIX compatibility --- scripts/run-phpunit.sh | 21 +++-- scripts/run-tests-with-server.sh | 23 ++++-- scripts/start-test-environment-wrapper.sh | 11 --- scripts/start-test-environment.sh | 93 +++++++++++++++++------ scripts/stop-test-environment.sh | 22 ++++-- 5 files changed, 117 insertions(+), 53 deletions(-) delete mode 100755 scripts/start-test-environment-wrapper.sh diff --git a/scripts/run-phpunit.sh b/scripts/run-phpunit.sh index d704f864b..adb303588 100755 --- a/scripts/run-phpunit.sh +++ b/scripts/run-phpunit.sh @@ -1,11 +1,18 @@ -#!/bin/bash +#!/bin/sh -# Get the directory of this script -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +# Try to determine script location +if [ -n "${BASH_SOURCE:-}" ]; then + # Bash-specific path resolution + SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +else + # POSIX fallback - assume we're in the repository root + PROJECT_ROOT="$(pwd)" + SCRIPT_DIR="$PROJECT_ROOT/scripts" +fi # Detect PHPUnit version -PHPUNIT_VERSION=$("${PROJECT_ROOT}/vendor/bin/phpunit" --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+') +PHPUNIT_VERSION=$("${PROJECT_ROOT}/vendor/bin/phpunit" --version | grep -oE '[0-9]+\.[0-9]+' | head -n1) # Determine if we should include coverage based on argument COVERAGE_ARG="" @@ -14,8 +21,8 @@ if [ "$1" != "--no-coverage" ]; then fi # Run the tests with the appropriate config -if printf '%s\n%s\n' "10.0" "$PHPUNIT_VERSION" | sort -V -C; then +if printf '%s\n%s\n' "10.0" "$PHPUNIT_VERSION" | sort -V -C 2>/dev/null; then "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist $COVERAGE_ARG else "${PROJECT_ROOT}/vendor/bin/phpunit" $COVERAGE_ARG -fi \ No newline at end of file +fi diff --git a/scripts/run-tests-with-server.sh b/scripts/run-tests-with-server.sh index f128439a5..232ecd2aa 100755 --- a/scripts/run-tests-with-server.sh +++ b/scripts/run-tests-with-server.sh @@ -1,17 +1,26 @@ -#!/bin/bash +#!/bin/sh -# Get the directory of this script -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +# Try to determine script location +if [ -n "${BASH_SOURCE:-}" ]; then + # Bash-specific path resolution + SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +else + # POSIX fallback - assume we're in the repository root + PROJECT_ROOT="$(pwd)" + SCRIPT_DIR="$PROJECT_ROOT/scripts" +fi # Source the environment setup -source "${SCRIPT_DIR}/start-test-environment.sh" || exit 1 +if ! . "${SCRIPT_DIR}/start-test-environment.sh"; then + exit 1 +fi # Run the tests "${SCRIPT_DIR}/run-phpunit.sh" --no-coverage TEST_EXIT_CODE=$? # Stop the test environment -source "${SCRIPT_DIR}/stop-test-environment.sh" +. "${SCRIPT_DIR}/stop-test-environment.sh" -exit $TEST_EXIT_CODE \ No newline at end of file +exit $TEST_EXIT_CODE diff --git a/scripts/start-test-environment-wrapper.sh b/scripts/start-test-environment-wrapper.sh deleted file mode 100755 index 18d3f94e6..000000000 --- a/scripts/start-test-environment-wrapper.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# Get the directory of this script -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -# Source the main script -if source "${SCRIPT_DIR}/start-test-environment.sh"; then - exit 0 -else - exit 1 -fi \ No newline at end of file diff --git a/scripts/start-test-environment.sh b/scripts/start-test-environment.sh index 82580c336..66be8ad79 100755 --- a/scripts/start-test-environment.sh +++ b/scripts/start-test-environment.sh @@ -1,31 +1,67 @@ -#!/bin/bash +#!/bin/sh -# Ensure the script is being sourced -if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then - echo "Error: This script must be sourced to set environment variables" - echo "Usage: source scripts/start-test-environment.sh" - exit 1 +# Try to determine script location +if [ -n "${BASH_SOURCE:-}" ]; then + # Bash-specific path resolution + SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +else + # POSIX fallback - assume we're in the repository root + PROJECT_ROOT="$(pwd)" + SCRIPT_DIR="$PROJECT_ROOT/scripts" +fi + +# Detect if we're being sourced (POSIX-compatible way) +sourced=0 +if [ -n "$ZSH_EVAL_CONTEXT" ]; then + case $ZSH_EVAL_CONTEXT in *:file:*) sourced=1;; esac +elif [ -n "$KSH_VERSION" ]; then + [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1 +elif [ -n "$BASH_VERSION" ]; then + (return 0 2>/dev/null) && sourced=1 +else + # POSIX fallback - check if we can modify our environment + # Try to modify a test variable + TEST_VAR="test" + if [ -z "${TEST_VAR:-}" ]; then + sourced=0 + else + sourced=1 + fi +fi + +if [ $sourced -eq 0 ]; then + echo "Warning: This script should be sourced to set environment variables" + echo "Please either:" + echo "1. Source this script: source scripts/start-test-environment.sh" + echo "2. Or manually set these environment variables:" + echo " export REQUESTS_TEST_HOST_HTTP=localhost:8080" + echo " export REQUESTS_HTTP_PROXY=localhost:9002" + echo " export REQUESTS_HTTP_PROXY_AUTH=localhost:9003" + echo " export REQUESTS_HTTP_PROXY_AUTH_USER=test" + echo " export REQUESTS_HTTP_PROXY_AUTH_PASS=pass" + if [ -n "${BASH_VERSION:-}" ]; then + exit 1 + fi fi -# Store script dir for relative paths -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" PID_DIR="${PROJECT_ROOT}/.test-pids" # Check if mitmproxy is installed -if ! command -v mitmdump &> /dev/null; then +if ! command -v mitmdump >/dev/null 2>&1; then echo "Error: mitmproxy is not installed. Please install it with: pip3 install mitmproxy" - return 1 + return 1 2>/dev/null || exit 1 fi -# Get mitmproxy version and compare with minimum required (using 9.0.0 as minimum) +# Get mitmproxy version and compare with minimum required MITM_VERSION=$(mitmdump --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1) MINIMUM_VERSION="11.0.2" -if ! printf '%s\n%s\n' "$MINIMUM_VERSION" "$MITM_VERSION" | sort -V -C; then +# POSIX-compatible version comparison +if ! printf '%s\n%s\n' "$MINIMUM_VERSION" "$MITM_VERSION" | sort -V -C 2>/dev/null; then echo "Error: mitmproxy version $MITM_VERSION is too old" echo "Please upgrade to version $MINIMUM_VERSION or newer with: pip3 install --upgrade mitmproxy" - return 1 + return 1 2>/dev/null || exit 1 fi echo "Found mitmproxy version $MITM_VERSION" @@ -37,7 +73,8 @@ mkdir -p "$PID_DIR" echo "Starting test server..." PORT=8080 "${PROJECT_ROOT}/vendor/bin/start.sh" echo $! > "${PID_DIR}/test-server.pid" -export REQUESTS_TEST_HOST_HTTP=localhost:8080 +REQUESTS_TEST_HOST_HTTP=localhost:8080 +export REQUESTS_TEST_HOST_HTTP # Start proxy servers echo "Starting proxy servers..." @@ -48,10 +85,15 @@ PORT=9003 AUTH="test:pass" "${PROJECT_ROOT}/tests/utils/proxy/start.sh" echo $! > "${PID_DIR}/proxy-auth-server.pid" # Set environment variables -export REQUESTS_HTTP_PROXY=localhost:9002 -export REQUESTS_HTTP_PROXY_AUTH=localhost:9003 -export REQUESTS_HTTP_PROXY_AUTH_USER=test -export REQUESTS_HTTP_PROXY_AUTH_PASS=pass +REQUESTS_HTTP_PROXY=localhost:9002 +REQUESTS_HTTP_PROXY_AUTH=localhost:9003 +REQUESTS_HTTP_PROXY_AUTH_USER=test +REQUESTS_HTTP_PROXY_AUTH_PASS=pass + +export REQUESTS_HTTP_PROXY +export REQUESTS_HTTP_PROXY_AUTH +export REQUESTS_HTTP_PROXY_AUTH_USER +export REQUESTS_HTTP_PROXY_AUTH_PASS # Wait for servers to be ready echo "Waiting for servers to be ready..." @@ -59,8 +101,15 @@ sleep 2 # Test server connections echo "Testing server connections..." -curl -s -I http://localhost:8080 > /dev/null || { echo "Test server not responding"; return 1; } -curl -s -I http://localhost:9002 > /dev/null || { echo "Proxy server not responding"; return 1; } +if ! curl -s -I http://localhost:8080 >/dev/null 2>&1; then + echo "Test server not responding" + return 1 2>/dev/null || exit 1 +fi + +if ! curl -s -I http://localhost:9002 >/dev/null 2>&1; then + echo "Proxy server not responding" + return 1 2>/dev/null || exit 1 +fi echo "Test environment is ready!" echo "Environment variables set:" @@ -68,4 +117,4 @@ echo "REQUESTS_TEST_HOST_HTTP=localhost:8080" echo "REQUESTS_HTTP_PROXY=localhost:9002" echo "REQUESTS_HTTP_PROXY_AUTH=localhost:9003" -return 0 \ No newline at end of file +return 0 2>/dev/null || exit 0 diff --git a/scripts/stop-test-environment.sh b/scripts/stop-test-environment.sh index 86518d17b..5e4654580 100755 --- a/scripts/stop-test-environment.sh +++ b/scripts/stop-test-environment.sh @@ -1,13 +1,21 @@ -#!/bin/bash +#!/bin/sh + +# Try to determine script location +if [ -n "${BASH_SOURCE:-}" ]; then + # Bash-specific path resolution + SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" +else + # POSIX fallback - assume we're in the repository root + PROJECT_ROOT="$(pwd)" + SCRIPT_DIR="$PROJECT_ROOT/scripts" +fi -# Store script dir for relative paths -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" PID_DIR="${PROJECT_ROOT}/.test-pids" # Function to safely kill a process kill_process() { - local pid_file="$1" + pid_file="$1" if [ -f "$pid_file" ]; then pid=$(cat "$pid_file") if kill -0 "$pid" 2>/dev/null; then @@ -38,4 +46,6 @@ kill_process "${PID_DIR}/proxy-auth-server.pid" # Clean up PID directory if empty rmdir "${PID_DIR}" 2>/dev/null || true -echo "Test environment stopped" \ No newline at end of file +echo "Test environment stopped" + +return 0 2>/dev/null || exit 0 From 057f31c2e777490821bd19369b9a31b5ce8557bf Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 11 Dec 2024 15:03:09 +0100 Subject: [PATCH 04/14] Use different folder for storing pids and gitignore as needed --- .gitignore | 1 + scripts/start-test-environment.sh | 2 +- scripts/stop-test-environment.sh | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8f4479dd1..bb57d438f 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ certificates/etag-*.txt # Ignore temporary files generated by the testing proxy. tests/utils/proxy/__pycache__ tests/utils/proxy/*.pid +tests/utils/pids diff --git a/scripts/start-test-environment.sh b/scripts/start-test-environment.sh index 66be8ad79..dfe3e80f8 100755 --- a/scripts/start-test-environment.sh +++ b/scripts/start-test-environment.sh @@ -45,7 +45,7 @@ if [ $sourced -eq 0 ]; then fi fi -PID_DIR="${PROJECT_ROOT}/.test-pids" +PID_DIR="${PROJECT_ROOT}/tests/utils/pids" # Check if mitmproxy is installed if ! command -v mitmdump >/dev/null 2>&1; then diff --git a/scripts/stop-test-environment.sh b/scripts/stop-test-environment.sh index 5e4654580..b00c631a0 100755 --- a/scripts/stop-test-environment.sh +++ b/scripts/stop-test-environment.sh @@ -11,7 +11,7 @@ else SCRIPT_DIR="$PROJECT_ROOT/scripts" fi -PID_DIR="${PROJECT_ROOT}/.test-pids" +PID_DIR="${PROJECT_ROOT}/tests/utils/pids" # Function to safely kill a process kill_process() { From 0efd79ccc9835672cc5e08c00e09244d7f32b18a Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 21 Jan 2025 12:01:51 +0100 Subject: [PATCH 05/14] Remove conditional PHPUnit paths from GHA workflows --- .github/workflows/quicktest.yml | 7 +------ .github/workflows/test.yml | 16 ++++------------ 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/.github/workflows/quicktest.yml b/.github/workflows/quicktest.yml index fae653090..ae9a5927c 100644 --- a/.github/workflows/quicktest.yml +++ b/.github/workflows/quicktest.yml @@ -94,14 +94,9 @@ jobs: id: phpunit_version run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT" - - name: Run the unit tests (PHPUnit < 10) - if: ${{ ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }} + - name: Run the unit tests run: composer test - - name: Run the unit tests (PHPUnit 10+) - if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }} - run: composer test10 - - name: Stop proxy server continue-on-error: true run: | diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4e04b5de2..ed29672b6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -118,22 +118,14 @@ jobs: id: phpunit_version run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT" - - name: Run the unit tests, no code coverage (PHPUnit < 10) - if: ${{ matrix.coverage == false && ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }} + - name: Run the unit tests, no code coverage + if: ${{ matrix.coverage == false }} run: composer test - - name: Run the unit tests, no code coverage (PHPUnit 10+) - if: ${{ matrix.coverage == false && startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }} - run: composer test10 - - - name: Run the unit tests with code coverage (PHPUnit < 10) - if: ${{ matrix.coverage == true && ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }} + - name: Run the unit tests with code coverage + if: ${{ matrix.coverage == true }} run: composer coverage -- --coverage-clover clover.xml - - name: Run the unit tests with code coverage (PHPUnit 10+) - if: ${{ matrix.coverage == true && startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }} - run: composer coverage10 -- --coverage-clover clover.xml - - name: Stop proxy server continue-on-error: true run: | From 360d9e27e50b669b276e70938fb4419baa91d88e Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 21 Jan 2025 12:18:41 +0100 Subject: [PATCH 06/14] Adapt argument support in PHPUnit script --- scripts/run-phpunit.sh | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/scripts/run-phpunit.sh b/scripts/run-phpunit.sh index adb303588..135c96ada 100755 --- a/scripts/run-phpunit.sh +++ b/scripts/run-phpunit.sh @@ -14,15 +14,9 @@ fi # Detect PHPUnit version PHPUNIT_VERSION=$("${PROJECT_ROOT}/vendor/bin/phpunit" --version | grep -oE '[0-9]+\.[0-9]+' | head -n1) -# Determine if we should include coverage based on argument -COVERAGE_ARG="" -if [ "$1" != "--no-coverage" ]; then - COVERAGE_ARG="--coverage-html build/coverage" -fi - # Run the tests with the appropriate config if printf '%s\n%s\n' "10.0" "$PHPUNIT_VERSION" | sort -V -C 2>/dev/null; then - "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist $COVERAGE_ARG + "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist "$1" else - "${PROJECT_ROOT}/vendor/bin/phpunit" $COVERAGE_ARG + "${PROJECT_ROOT}/vendor/bin/phpunit" "$1" fi From 16a71f82d2989935850e7db5ff938ae1f5f61a33 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 21 Jan 2025 12:22:35 +0100 Subject: [PATCH 07/14] Support multiple arguments in PHPUnit helper script --- scripts/run-phpunit.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/run-phpunit.sh b/scripts/run-phpunit.sh index 135c96ada..89a8ca68c 100755 --- a/scripts/run-phpunit.sh +++ b/scripts/run-phpunit.sh @@ -16,7 +16,7 @@ PHPUNIT_VERSION=$("${PROJECT_ROOT}/vendor/bin/phpunit" --version | grep -oE '[0- # Run the tests with the appropriate config if printf '%s\n%s\n' "10.0" "$PHPUNIT_VERSION" | sort -V -C 2>/dev/null; then - "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist "$1" + "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist "$@" else - "${PROJECT_ROOT}/vendor/bin/phpunit" "$1" + "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit.xml.dist "$@" fi From dd37db509b839487184b08bbbef0d398f2121218 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 11:21:56 +0100 Subject: [PATCH 08/14] Apply phrasing suggestions Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- .github/CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index e6b15b8eb..da272f0d2 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -149,11 +149,11 @@ Note: The environment scripts must be sourced (using `source` or `.`) to properl For debugging or development, you might want to manage individual components: ```bash -# Just the test server +# Start only the test server PORT=8080 vendor/bin/start.sh export REQUESTS_TEST_HOST_HTTP=localhost:8080 -# Just the proxy servers +# Start only the proxy servers PORT=9002 tests/utils/proxy/start.sh PORT=9003 AUTH="test:pass" tests/utils/proxy/start.sh export REQUESTS_HTTP_PROXY=localhost:9002 From 9e79ec02834d5df8e016cd2832b0127e779ed2ae Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 11:16:51 +0100 Subject: [PATCH 09/14] Use PHP version that Composer locked into, instead of system one --- scripts/run-phpunit.sh | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/scripts/run-phpunit.sh b/scripts/run-phpunit.sh index 89a8ca68c..e147fd4e8 100755 --- a/scripts/run-phpunit.sh +++ b/scripts/run-phpunit.sh @@ -1,22 +1,20 @@ #!/bin/sh -# Try to determine script location -if [ -n "${BASH_SOURCE:-}" ]; then - # Bash-specific path resolution - SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" -else - # POSIX fallback - assume we're in the repository root - PROJECT_ROOT="$(pwd)" - SCRIPT_DIR="$PROJECT_ROOT/scripts" -fi + +# Set up PHPUnit command +# We're using composer exec to ensure we use the version of PHP that Composer +# is locked into, instead of the version of PHP that the system provides. +PHPUNIT="composer exec phpunit --" # Detect PHPUnit version -PHPUNIT_VERSION=$("${PROJECT_ROOT}/vendor/bin/phpunit" --version | grep -oE '[0-9]+\.[0-9]+' | head -n1) +PHPUNIT_VERSION=$($PHPUNIT --version | grep -oE '[0-9]+\.[0-9]+' | head -n 1) -# Run the tests with the appropriate config +# Determine config file based on version if printf '%s\n%s\n' "10.0" "$PHPUNIT_VERSION" | sort -V -C 2>/dev/null; then - "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit10.xml.dist "$@" + CONFIG_FILE="phpunit10.xml.dist" else - "${PROJECT_ROOT}/vendor/bin/phpunit" -c phpunit.xml.dist "$@" + CONFIG_FILE="phpunit.xml.dist" fi + +# Run the tests +$PHPUNIT -c "$CONFIG_FILE" "$@" From eaf993c47a885f34825f81305cde3f6a461497ed Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 11:17:33 +0100 Subject: [PATCH 10/14] Add script/ to .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 3dc7af676..2a808429e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,6 +9,7 @@ build/ export-ignore docs/ export-ignore examples/ export-ignore +scripts/ export-ignore tests/ export-ignore .codecov.yml export-ignore .editorconfig export-ignore From f365f33dab1d11b10dfd00110442a9eeb06dcfe6 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 11:20:21 +0100 Subject: [PATCH 11/14] Remove unnecessary PHPUnit version check in GHA workflows --- .github/workflows/quicktest.yml | 4 ---- .github/workflows/test.yml | 4 ---- 2 files changed, 8 deletions(-) diff --git a/.github/workflows/quicktest.yml b/.github/workflows/quicktest.yml index ae9a5927c..dbfdb4f7c 100644 --- a/.github/workflows/quicktest.yml +++ b/.github/workflows/quicktest.yml @@ -90,10 +90,6 @@ jobs: - name: Access localhost on port 9002 run: curl -i http://localhost:9002 - - name: Grab PHPUnit version - id: phpunit_version - run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT" - - name: Run the unit tests run: composer test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ed29672b6..7669ef610 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -114,10 +114,6 @@ jobs: - name: Access localhost on port 9002 run: curl -i http://localhost:9002 - - name: Grab PHPUnit version - id: phpunit_version - run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT" - - name: Run the unit tests, no code coverage if: ${{ matrix.coverage == false }} run: composer test From fb62d2b08c46dedcb843240a1f52da7ef7a8092f Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 11:34:54 +0100 Subject: [PATCH 12/14] Avoid dedicated script for all-in-one commands and support coverage as well --- composer.json | 20 ++++++++++++++++++-- scripts/run-tests-with-server.sh | 26 -------------------------- 2 files changed, 18 insertions(+), 28 deletions(-) delete mode 100755 scripts/run-tests-with-server.sh diff --git a/composer.json b/composer.json index 3e0d6af06..f5aca4cbb 100644 --- a/composer.json +++ b/composer.json @@ -99,7 +99,22 @@ "./scripts/stop-test-environment.sh" ], "test:withserver": [ - "./scripts/run-tests-with-server.sh" + "@putenv REQUESTS_HTTP_PROXY=localhost:9002", + "@putenv REQUESTS_HTTP_PROXY_AUTH=localhost:9003", + "@putenv REQUESTS_HTTP_PROXY_AUTH_USER=test", + "@putenv REQUESTS_HTTP_PROXY_AUTH_PASS=pass", + "@testserver:start", + "@test", + "@testserver:stop" + ], + "coverage:withserver": [ + "@putenv REQUESTS_HTTP_PROXY=localhost:9002", + "@putenv REQUESTS_HTTP_PROXY_AUTH=localhost:9003", + "@putenv REQUESTS_HTTP_PROXY_AUTH_USER=test", + "@putenv REQUESTS_HTTP_PROXY_AUTH_PASS=pass", + "@testserver:start", + "@coverage", + "@testserver:stop" ] }, "scripts-descriptions": { @@ -110,6 +125,7 @@ "coverage": "Run the unit tests with code coverage (auto-detects PHPUnit version)", "testserver:start": "Start the test environment including test server and proxy servers (Note: environment variables will only be available in a sourced shell)", "testserver:stop": "Stop all running test environment servers", - "test:withserver": "Start test servers, run PHPUnit tests with appropriate config, then stop servers" + "test:withserver": "Start test servers, run PHPUnit tests with appropriate config, then stop servers", + "coverage:withserver": "Start test servers, run PHPUnit tests with code coverage, then stop servers" } } diff --git a/scripts/run-tests-with-server.sh b/scripts/run-tests-with-server.sh deleted file mode 100755 index 232ecd2aa..000000000 --- a/scripts/run-tests-with-server.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -# Try to determine script location -if [ -n "${BASH_SOURCE:-}" ]; then - # Bash-specific path resolution - SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" -else - # POSIX fallback - assume we're in the repository root - PROJECT_ROOT="$(pwd)" - SCRIPT_DIR="$PROJECT_ROOT/scripts" -fi - -# Source the environment setup -if ! . "${SCRIPT_DIR}/start-test-environment.sh"; then - exit 1 -fi - -# Run the tests -"${SCRIPT_DIR}/run-phpunit.sh" --no-coverage -TEST_EXIT_CODE=$? - -# Stop the test environment -. "${SCRIPT_DIR}/stop-test-environment.sh" - -exit $TEST_EXIT_CODE From 132517ae70317e8a3bd57b4b2cd8375abc9c3c7b Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 11:41:15 +0100 Subject: [PATCH 13/14] Improve quoting --- scripts/start-test-environment.sh | 17 +++++++++++------ scripts/stop-test-environment.sh | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/start-test-environment.sh b/scripts/start-test-environment.sh index dfe3e80f8..df0adad1d 100755 --- a/scripts/start-test-environment.sh +++ b/scripts/start-test-environment.sh @@ -3,6 +3,9 @@ # Try to determine script location if [ -n "${BASH_SOURCE:-}" ]; then # Bash-specific path resolution + # Shellcheck complains about POSIX arrays being unreferenced, but this bit + # conditionally run on Bash only. + # shellcheck disable=SC3054 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" else @@ -16,7 +19,7 @@ sourced=0 if [ -n "$ZSH_EVAL_CONTEXT" ]; then case $ZSH_EVAL_CONTEXT in *:file:*) sourced=1;; esac elif [ -n "$KSH_VERSION" ]; then - [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1 + [ "$(cd $(dirname -- "$0") && pwd -P)/$(basename -- "$0")" != "$(cd $(dirname -- "${.sh.file}") && pwd -P)/$(basename -- "${.sh.file}")" ] && sourced=1 elif [ -n "$BASH_VERSION" ]; then (return 0 2>/dev/null) && sourced=1 else @@ -73,7 +76,7 @@ mkdir -p "$PID_DIR" echo "Starting test server..." PORT=8080 "${PROJECT_ROOT}/vendor/bin/start.sh" echo $! > "${PID_DIR}/test-server.pid" -REQUESTS_TEST_HOST_HTTP=localhost:8080 +REQUESTS_TEST_HOST_HTTP="localhost:8080" export REQUESTS_TEST_HOST_HTTP # Start proxy servers @@ -85,10 +88,10 @@ PORT=9003 AUTH="test:pass" "${PROJECT_ROOT}/tests/utils/proxy/start.sh" echo $! > "${PID_DIR}/proxy-auth-server.pid" # Set environment variables -REQUESTS_HTTP_PROXY=localhost:9002 -REQUESTS_HTTP_PROXY_AUTH=localhost:9003 -REQUESTS_HTTP_PROXY_AUTH_USER=test -REQUESTS_HTTP_PROXY_AUTH_PASS=pass +REQUESTS_HTTP_PROXY="localhost:9002" +REQUESTS_HTTP_PROXY_AUTH="localhost:9003" +REQUESTS_HTTP_PROXY_AUTH_USER="test" +REQUESTS_HTTP_PROXY_AUTH_PASS="pass" export REQUESTS_HTTP_PROXY export REQUESTS_HTTP_PROXY_AUTH @@ -116,5 +119,7 @@ echo "Environment variables set:" echo "REQUESTS_TEST_HOST_HTTP=localhost:8080" echo "REQUESTS_HTTP_PROXY=localhost:9002" echo "REQUESTS_HTTP_PROXY_AUTH=localhost:9003" +echo "REQUESTS_HTTP_PROXY_AUTH_USER=test" +echo "REQUESTS_HTTP_PROXY_AUTH_PASS=pass" return 0 2>/dev/null || exit 0 diff --git a/scripts/stop-test-environment.sh b/scripts/stop-test-environment.sh index b00c631a0..279ec1edb 100755 --- a/scripts/stop-test-environment.sh +++ b/scripts/stop-test-environment.sh @@ -3,6 +3,9 @@ # Try to determine script location if [ -n "${BASH_SOURCE:-}" ]; then # Bash-specific path resolution + # Shellcheck complains about POSIX arrays being unreferenced, but this bit + # conditionally run on Bash only. + # shellcheck disable=SC3054 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" else From e6c455dca6bd45975dee94b2368b474a4a63e7e3 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 Feb 2025 12:05:27 +0100 Subject: [PATCH 14/14] Add version requirements to CONTRIBUTING.md --- .github/CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index da272f0d2..5b6aafacf 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -99,8 +99,8 @@ Code coverage is monitored for every PR and for the code base as a whole using [ - [PHP][] >= 5.6 - [Composer][] -- [Python 3][] -- [mitmproxy][] (`pip3 install mitmproxy`) +- [Python 3][] >= 3.12 +- [mitmproxy][] >= 11.0.2 (`pip3 install mitmproxy`) [PHP]: https://www.php.net/ [Composer]: http://getcomposer.org/