Skip to content

Commit 1c8e267

Browse files
committed
Refactor GitHub Actions workflows for PHPVM: streamline job configurations, update Node.js actions, and enhance test scripts.
1 parent 9b98de5 commit 1c8e267

File tree

4 files changed

+51
-149
lines changed

4 files changed

+51
-149
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,27 @@ on:
55
branches:
66
- main
77
pull_request:
8-
branches:
9-
- main
108

119
jobs:
1210
test:
13-
runs-on: ${{ matrix.os }}
14-
1511
strategy:
1612
matrix:
17-
os: [ubuntu-latest, macos-latest]
18-
platform: [intel, arm]
13+
os: [macos-latest, ubuntu-latest] # Run tests on both MacOS & Ubuntu
14+
runs-on: ${{ matrix.os }}
1915

2016
steps:
21-
# Checkout the code
22-
- name: Checkout repository
23-
uses: actions/checkout@v2
17+
- name: Checkout Repository
18+
uses: actions/checkout@v3
2419

25-
# Cache Node.js dependencies
26-
- name: Cache Node.js modules
27-
uses: actions/cache@v2
28-
with:
29-
path: node_modules
30-
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
31-
restore-keys: |
32-
${{ runner.os }}-node-
33-
34-
# Set up Node.js
35-
- name: Set up Node.js
36-
uses: actions/setup-node@v2
37-
with:
38-
node-version: '16'
39-
40-
# Install dependencies
41-
- name: Install dependencies
42-
run: npm install
43-
44-
# Matrix conditional setup for different OS/Platforms
45-
- name: Setup Platform Details
20+
- name: Install Dependencies
4621
run: |
47-
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
48-
if [[ "${{ matrix.platform }}" == "arm" ]]; then
49-
sudo apt-get update
50-
sudo apt-get install -y build-essential libxml2-dev
51-
fi
52-
fi
53-
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
54-
if [[ "${{ matrix.platform }}" == "arm" ]]; then
55-
echo "Simulating Apple Silicon scenario"
56-
else
57-
echo "Simulating Intel Mac scenario"
58-
fi
22+
if [[ "$RUNNER_OS" == "Linux" ]]; then
23+
sudo apt update
24+
sudo apt install -y bats
25+
else
26+
brew install bats
5927
fi
6028
61-
# Run tests
62-
- name: Run tests
63-
run: npm run test
29+
- name: Run PHPVM Tests
30+
run: |
31+
bats test_phpvm.bats

.github/workflows/use.yml

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,36 @@ on:
55
branches:
66
- main
77
pull_request:
8-
branches:
9-
- main
108

119
jobs:
1210
use:
13-
runs-on: ${{ matrix.os }}
14-
15-
strategy:
16-
matrix:
17-
os: [ubuntu-latest, macos-latest]
18-
platform: [intel, arm]
11+
runs-on: ubuntu-latest
1912

2013
steps:
21-
# Checkout the code
22-
- name: Checkout repository
23-
uses: actions/checkout@v2
24-
25-
# Cache Node.js dependencies
26-
- name: Cache Node.js modules
27-
uses: actions/cache@v2
28-
with:
29-
path: node_modules
30-
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
31-
restore-keys: |
32-
${{ runner.os }}-node-
33-
34-
# Set up Node.js
35-
- name: Set up Node.js
36-
uses: actions/setup-node@v2
37-
with:
38-
node-version: '16'
39-
40-
# Install dependencies
41-
- name: Install dependencies
42-
run: npm install
14+
- name: Checkout Repository
15+
uses: actions/checkout@v3
4316

44-
# Matrix conditional setup for different OS/Platforms
45-
- name: Setup Platform Details
17+
- name: Install Homebrew
4618
run: |
47-
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
48-
if [[ "${{ matrix.platform }}" == "arm" ]]; then
49-
sudo apt-get update
50-
sudo apt-get install -y build-essential libxml2-dev
51-
fi
52-
fi
53-
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
54-
if [[ "${{ matrix.platform }}" == "arm" ]]; then
55-
echo "Simulating Apple Silicon scenario"
56-
else
57-
echo "Simulating Intel Mac scenario"
58-
fi
59-
fi
19+
sudo apt update
20+
sudo apt install -y build-essential curl file git
21+
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
22+
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.bashrc
23+
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
6024
61-
# Install PHP on Ubuntu
62-
- name: Install PHP on Ubuntu
63-
if: matrix.os == 'ubuntu-latest'
25+
- name: Install PHP using PHPVM
6426
run: |
65-
sudo apt-get update
66-
sudo apt-get install -y php
27+
chmod +x phpvm.sh
28+
./phpvm.sh install 8.3
6729
68-
# Install PHP on macOS
69-
- name: Install PHP on macOS
70-
if: matrix.os == 'macos-latest'
30+
- name: Switch PHP Version
7131
run: |
72-
brew update && brew install php
73-
74-
# Run the PHP version manager (Install Test)
75-
- name: Run PHP Version Manager Install (Download Test)
76-
run: |
77-
node ./lib/commands/install.js 7.4.10
78-
79-
# Check if PHP is installed and verify its version
80-
- name: Verify PHP Installation
81-
run: |
82-
php -v
32+
./phpvm.sh use 8.3
8333
84-
# Simulate failed download and fallback to package manager
85-
- name: Simulate Failed Download and Fallback
34+
- name: List Installed PHP Versions
8635
run: |
87-
node ./lib/commands/install.js non-existent-version
88-
continue-on-error: true
36+
./phpvm.sh list
8937
90-
# Verify PHP Version Manager fallback handling
91-
- name: Check if fallback worked
38+
- name: Verify PHP Version
9239
run: |
9340
php -v

phpvm.sh

100644100755
File mode changed.

test_phpvm.bats

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
#!/usr/bin/env bats
22

33
# Source the script under test.
4-
# Adjust the relative path if necessary.
5-
source "./phpvm.sh"
6-
74
setup() {
8-
# Save the original PATH
95
ORIGINAL_PATH="$PATH"
10-
# Create a temporary directory for our mock commands
116
MOCK_DIR="$(mktemp -d)"
127
PATH="$MOCK_DIR:$PATH"
138

14-
# Create a dummy 'brew' command
15-
cat <<'EOF' >"$MOCK_DIR/brew"
16-
#!/bin/bash
17-
# Simply print the arguments to verify the call.
18-
echo "brew $*"
19-
exit 0
20-
EOF
21-
chmod +x "$MOCK_DIR/brew"
9+
# Ensure phpvm.sh exists before sourcing
10+
if [[ ! -f "./phpvm.sh" ]]; then
11+
echo "Error: phpvm.sh not found!"
12+
exit 1
13+
fi
14+
15+
chmod +x "./phpvm.sh"
16+
source "./phpvm.sh"
2217
}
2318

2419
teardown() {
2520
PATH="$ORIGINAL_PATH"
2621
rm -rf "$MOCK_DIR"
27-
# Remove any temporary test directories if created.
28-
[ -n "$TEST_DIR" ] && rm -rf "$TEST_DIR"
22+
# Ensure .phpvmrc exists before attempting to remove it
23+
if [[ -f "$PWD/.phpvmrc" ]]; then
24+
rm -f "$PWD/.phpvmrc"
25+
fi
2926
}
3027

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

3734
@test "install_php calls brew with version" {
38-
run install_php "7.4"
35+
run install_php "8.3"
3936
[ "$status" -eq 0 ]
40-
# Check that the function echoed the installation messages.
41-
[[ "$output" == *"Installing PHP 7.4..."* ]]
42-
[[ "$output" == *"PHP 7.4 installed."* ]]
37+
[[ "$output" == *"Installing PHP 8.3..."* ]]
38+
[[ "$output" == *"PHP 8.3 installed."* ]]
4339
}
4440

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

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

6759
@test "auto_switch_php_version switches PHP version from .phpvmrc" {
68-
# Create a temporary directory with a .phpvmrc file
69-
TEST_DIR="$(mktemp -d)"
70-
echo "7.4" >"$TEST_DIR/.phpvmrc"
71-
72-
# Create a fake Homebrew cellar directory to simulate an installed PHP version.
73-
mkdir -p "/opt/homebrew/Cellar/[email protected]"
74-
75-
pushd "$TEST_DIR" >/dev/null
60+
echo "8.3" >.phpvmrc
61+
export HOMEBREW_PHP_CELLAR="/opt/homebrew/Cellar"
62+
export HOMEBREW_PHP_BIN="/opt/homebrew/bin" # Ensure binary path is available
7663
run auto_switch_php_version
77-
popd >/dev/null
78-
64+
echo "Test output: $output" # Debugging line to inspect output
7965
[ "$status" -eq 0 ]
80-
[[ "$output" == *"Auto-switching to PHP 7.4"* ]]
66+
[[ "$output" == *"Auto-switching to PHP 8.3"* ]]
67+
rm -f .phpvmrc # Cleanup after test
8168
}

0 commit comments

Comments
 (0)