Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline local test environment setup #916

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

schlessera
Copy link
Member

@schlessera schlessera commented Dec 11, 2024

Refactored the test environment setup to make it more robust and user-friendly. This consolidates multiple test commands and improves process management for local development.

What Changed

  • Added shell scripts to manage test environment lifecycle
  • Consolidated PHPUnit commands to auto-detect version and config
  • Improved process management with proper PID tracking
  • Added mitmproxy version verification (≥11.0.2 required)
  • Adapted CONTRIBUTING.md to explain the new setup

The main improvement is the simplified test workflow. Instead of juggling different commands for PHPUnit versions and remembering to start/stop test servers, everything is now handled through clear, single-purpose commands.

New Scripts

  • scripts/start-test-environment.sh - Boots test and proxy servers - should be sourced to set environment vairables
  • scripts/stop-test-environment.sh - Clean shutdown of all processes
  • scripts/run-phpunit.sh - Version-agnostic PHPUnit runner
  • scripts/run-tests-with-server.sh - Full test environment run

Usage

Basic testing is now simpler:

composer test # Run tests (auto-detects PHPUnit version)
composer coverage # Same, but with coverage

For development with local test servers:

# Direct usage (sets env vars in your shell)
source scripts/start-test-environment.sh
scripts/run-phpunit.sh
scripts/stop-test-environment.sh

# Or through composer (automated start/stop)
composer test:withserver

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good.

Will still need a note in CONTRIBUTING that this will not work on Windows for now.

As discussed, not merging yet until a shellcheck check has been added in CI to safeguard the scripts.

@jrfnl
Copy link
Member

jrfnl commented Dec 11, 2024

Future scope: add a Docker config file to get up & running with even less effort (and which would work on Windows too).

@jrfnl
Copy link
Member

jrfnl commented Dec 11, 2024

Just noticed, just running the script on my machine created some .test-pids/*.pid files. You may want to add those to the .gitignore file ?

@schlessera
Copy link
Member Author

I changed the folder where PIDs are stored and added that location to .gitignore: tests/utils/pids

@schlessera schlessera force-pushed the add/simplified-local-testserver branch from 60f07ce to f09ca3e Compare January 21, 2025 11:01
@jrfnl jrfnl mentioned this pull request Jan 22, 2025
@jrfnl
Copy link
Member

jrfnl commented Jan 22, 2025

Just checking - this PR relates to #819 (and closes it ?)

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schlessera I've had a good look at this PR, with the caveat of limited knowledge of bash scripting...

Left a number of questions in-line.

Additionally:

  • The scripts re-introduce a problem we previously solved: the system default PHP version being used instead of the Composer PHP version being used. This is a regression, which I'm not happy with.
  • /scripts/ directory needs to be added to .gitattributes
  • composer.json: script descriptions should mention that this doesn't work on Windows, only on *nix
  • CONTRIBUTING: should mention that this doesn't work on Windows, only on *nix.
  • CONTRIBUTING: "The environment scripts must be sourced"... what does that mean ?
    Note: the source command is not available on Windows.

Other remarks:

  • The minimum versions for Python and mitmproxy will now need to be maintained in quite a few places (CONTRIBUTING, quicktest.yml, test.yml, the new scripts). This feels pretty error prone...
  • Just as a test, I've ran the scripts on Windows:
    • run-tests-with-server.sh doesn't appears to do anything (other than flash open a new shell window & close it) and exits without giving the user information about why it's not working.
    • start-test-environment.sh doesn't appears to do anything (other than flash open a new shell window & close it) and exits without giving the user information about why it's not working.
    • stop-test-environment.sh doesn't appears to do anything (other than flash open a new shell window & close it) and exits without giving the user information about why it's not working.
    • run-phpunit.sh runs, but opens a new shell window and closes it immediately once the test run has finished, meaning you can't read the results.
      The new window is also really annoying for other reasons, like usability issues, no scrolling, window size being tiny etc

```bash
# Start the test server
# Just the test server
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Just the test server
# Start only the test server


# Start the proxy server
# Just the proxy servers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Just the proxy servers
# Start only the proxy servers

Comment on lines 94 to 95
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this step can now be removed ?

Comment on lines -125 to -126
# Run the tests
composer test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being removed here ?

Comment on lines 118 to 119
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dito. This step can probably be removed now.

Comment on lines +3 to +12
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiousity: why is this needed ? AFAIK Composer scripts only work from the directory containing the composer.json file, so in that case, we'd always be in the project root.

I imagine it's because people can call these scripts directly ?

TEST_EXIT_CODE=$?

# Stop the test environment
. "${SCRIPT_DIR}/stop-test-environment.sh"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be an error as well if the test environment couldn't be stopped cleanly ?


# Check if mitmproxy is installed
if ! command -v mitmdump >/dev/null 2>&1; then
echo "Error: mitmproxy is not installed. Please install it with: pip3 install mitmproxy"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a check first to see if Python/Pip3 is installed ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering: if a user already has a server running which serves localhost, will this script cause problems with that?

@@ -109,28 +109,65 @@ Code coverage is monitored for every PR and for the code base as a whole using [

### Running the Tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to update the "Prerequisites for running the tests" section to mention the minimum versions, i.e. Python 3.12 and mitmproxy 11.0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants