Skip to content

Commit

Permalink
Refactor GitHub Actions workflows for PHPVM: streamline job configura…
Browse files Browse the repository at this point in the history
…tions, update Node.js actions, and enhance test scripts.
  • Loading branch information
Thavarshan committed Feb 15, 2025
1 parent 9b98de5 commit 1c8e267
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 149 deletions.
58 changes: 13 additions & 45 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,27 @@ on:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest]
platform: [intel, arm]
os: [macos-latest, ubuntu-latest] # Run tests on both MacOS & Ubuntu
runs-on: ${{ matrix.os }}

steps:
# Checkout the code
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout Repository
uses: actions/checkout@v3

# Cache Node.js dependencies
- name: Cache Node.js modules
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
# Set up Node.js
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

# Install dependencies
- name: Install dependencies
run: npm install

# Matrix conditional setup for different OS/Platforms
- name: Setup Platform Details
- name: Install Dependencies
run: |
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
if [[ "${{ matrix.platform }}" == "arm" ]]; then
sudo apt-get update
sudo apt-get install -y build-essential libxml2-dev
fi
fi
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
if [[ "${{ matrix.platform }}" == "arm" ]]; then
echo "Simulating Apple Silicon scenario"
else
echo "Simulating Intel Mac scenario"
fi
if [[ "$RUNNER_OS" == "Linux" ]]; then
sudo apt update
sudo apt install -y bats
else
brew install bats
fi
# Run tests
- name: Run tests
run: npm run test
- name: Run PHPVM Tests
run: |
bats test_phpvm.bats
87 changes: 17 additions & 70 deletions .github/workflows/use.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,36 @@ on:
branches:
- main
pull_request:
branches:
- main

jobs:
use:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest]
platform: [intel, arm]
runs-on: ubuntu-latest

steps:
# Checkout the code
- name: Checkout repository
uses: actions/checkout@v2

# Cache Node.js dependencies
- name: Cache Node.js modules
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
# Set up Node.js
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

# Install dependencies
- name: Install dependencies
run: npm install
- name: Checkout Repository
uses: actions/checkout@v3

# Matrix conditional setup for different OS/Platforms
- name: Setup Platform Details
- name: Install Homebrew
run: |
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
if [[ "${{ matrix.platform }}" == "arm" ]]; then
sudo apt-get update
sudo apt-get install -y build-essential libxml2-dev
fi
fi
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
if [[ "${{ matrix.platform }}" == "arm" ]]; then
echo "Simulating Apple Silicon scenario"
else
echo "Simulating Intel Mac scenario"
fi
fi
sudo apt update
sudo apt install -y build-essential curl file git
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.bashrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
# Install PHP on Ubuntu
- name: Install PHP on Ubuntu
if: matrix.os == 'ubuntu-latest'
- name: Install PHP using PHPVM
run: |
sudo apt-get update
sudo apt-get install -y php
chmod +x phpvm.sh
./phpvm.sh install 8.3
# Install PHP on macOS
- name: Install PHP on macOS
if: matrix.os == 'macos-latest'
- name: Switch PHP Version
run: |
brew update && brew install php
# Run the PHP version manager (Install Test)
- name: Run PHP Version Manager Install (Download Test)
run: |
node ./lib/commands/install.js 7.4.10
# Check if PHP is installed and verify its version
- name: Verify PHP Installation
run: |
php -v
./phpvm.sh use 8.3
# Simulate failed download and fallback to package manager
- name: Simulate Failed Download and Fallback
- name: List Installed PHP Versions
run: |
node ./lib/commands/install.js non-existent-version
continue-on-error: true
./phpvm.sh list
# Verify PHP Version Manager fallback handling
- name: Check if fallback worked
- name: Verify PHP Version
run: |
php -v
Empty file modified phpvm.sh
100644 → 100755
Empty file.
55 changes: 21 additions & 34 deletions test_phpvm.bats
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
#!/usr/bin/env bats

# Source the script under test.
# Adjust the relative path if necessary.
source "./phpvm.sh"

setup() {
# Save the original PATH
ORIGINAL_PATH="$PATH"
# Create a temporary directory for our mock commands
MOCK_DIR="$(mktemp -d)"
PATH="$MOCK_DIR:$PATH"

# Create a dummy 'brew' command
cat <<'EOF' >"$MOCK_DIR/brew"
#!/bin/bash
# Simply print the arguments to verify the call.
echo "brew $*"
exit 0
EOF
chmod +x "$MOCK_DIR/brew"
# Ensure phpvm.sh exists before sourcing
if [[ ! -f "./phpvm.sh" ]]; then
echo "Error: phpvm.sh not found!"
exit 1
fi

chmod +x "./phpvm.sh"
source "./phpvm.sh"
}

teardown() {
PATH="$ORIGINAL_PATH"
rm -rf "$MOCK_DIR"
# Remove any temporary test directories if created.
[ -n "$TEST_DIR" ] && rm -rf "$TEST_DIR"
# Ensure .phpvmrc exists before attempting to remove it
if [[ -f "$PWD/.phpvmrc" ]]; then
rm -f "$PWD/.phpvmrc"
fi
}

@test "install_php returns error if version not provided" {
Expand All @@ -35,11 +32,10 @@ teardown() {
}

@test "install_php calls brew with version" {
run install_php "7.4"
run install_php "8.3"
[ "$status" -eq 0 ]
# Check that the function echoed the installation messages.
[[ "$output" == *"Installing PHP 7.4..."* ]]
[[ "$output" == *"PHP 7.4 installed."* ]]
[[ "$output" == *"Installing PHP 8.3..."* ]]
[[ "$output" == *"PHP 8.3 installed."* ]]
}

@test "use_php_version returns error if version not provided" {
Expand All @@ -55,27 +51,18 @@ teardown() {
}

@test "auto_switch_php_version warns when .phpvmrc is not found" {
# Run from a temporary directory that does not contain .phpvmrc.
TEST_DIR="$(mktemp -d)"
pushd "$TEST_DIR" >/dev/null
run auto_switch_php_version
popd >/dev/null
[ "$status" -ne 0 ]
[[ "$output" == *"No .phpvmrc file found"* ]]
}

@test "auto_switch_php_version switches PHP version from .phpvmrc" {
# Create a temporary directory with a .phpvmrc file
TEST_DIR="$(mktemp -d)"
echo "7.4" >"$TEST_DIR/.phpvmrc"

# Create a fake Homebrew cellar directory to simulate an installed PHP version.
mkdir -p "/opt/homebrew/Cellar/[email protected]"

pushd "$TEST_DIR" >/dev/null
echo "8.3" >.phpvmrc
export HOMEBREW_PHP_CELLAR="/opt/homebrew/Cellar"
export HOMEBREW_PHP_BIN="/opt/homebrew/bin" # Ensure binary path is available
run auto_switch_php_version
popd >/dev/null

echo "Test output: $output" # Debugging line to inspect output
[ "$status" -eq 0 ]
[[ "$output" == *"Auto-switching to PHP 7.4"* ]]
[[ "$output" == *"Auto-switching to PHP 8.3"* ]]
rm -f .phpvmrc # Cleanup after test
}

0 comments on commit 1c8e267

Please sign in to comment.