Skip to content

Commit 9cd9ba9

Browse files
committed
Refactor CI workflow to support multiple OS and improve BATS installation
- Updated GitHub Actions workflow to run tests on both Ubuntu and macOS. - Added conditional steps for installing BATS based on the operating system. - Removed the old test script and integrated self-tests directly into the phpvm script. - Enhanced README with clearer instructions and additional features. - Introduced a new installation script for BATS to streamline the setup process. - Improved error handling and logging in the phpvm script. - Added helper functions for better maintainability and reduced code duplication.
1 parent 6a62d19 commit 9cd9ba9

File tree

6 files changed

+705
-299
lines changed

6 files changed

+705
-299
lines changed

.github/workflows/test.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
name: Test
1+
name: PHPVM Tests
22

33
on:
44
push:
5-
branches:
6-
- main
5+
branches: [ main, development ]
76
pull_request:
7+
branches: [ main, development ]
88

99
jobs:
1010
test:
11-
runs-on: macos-latest
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest, macos-latest]
1215

1316
steps:
14-
- name: Checkout Repository
15-
uses: actions/checkout@v3
17+
- uses: actions/checkout@v3
1618

17-
- name: Install Dependencies
18-
run: brew install bats
19+
- name: Install BATS (Ubuntu)
20+
if: matrix.os == 'ubuntu-latest'
21+
run: |
22+
git clone https://github.com/bats-core/bats-core.git
23+
cd bats-core
24+
sudo ./install.sh /usr/local
1925
20-
- name: Run PHPVM Tests
21-
run: bats test_phpvm.bats
26+
- name: Install BATS (macOS)
27+
if: matrix.os == 'macos-latest'
28+
run: |
29+
brew install bats-core
30+
31+
- name: Run BATS tests
32+
run: |
33+
bats phpvm.bats

README.MD

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
```sh
1717
$ phpvm use 8.2
18-
Now using PHP v8.2.10
18+
Switching to PHP 8.2...
19+
Switched to PHP 8.2.
1920
$ php -v
2021
PHP 8.2.10
2122
$ phpvm use 8.1
22-
Now using PHP v8.1.13
23+
Switching to PHP 8.1...
24+
Switched to PHP 8.1.
2325
$ php -v
2426
PHP 8.1.13
2527
```
@@ -31,8 +33,10 @@ PHP 8.1.13
3133
- Auto-switch PHP versions based on project `.phpvmrc`.
3234
- Supports macOS (via Homebrew) and Linux distributions.
3335
- Works with common shells (`bash`, `zsh`).
34-
- Improved error handling and informative, color-coded feedback.
36+
- Improved error handling with detailed messages and suggestions.
37+
- Informative, color-coded feedback with timestamps for logs.
3538
- Includes unit tests with BATS for automated testing.
39+
- Helper functions for better maintainability and reduced code duplication.
3640

3741
## Installation
3842

@@ -52,7 +56,8 @@ This script will download and set up `phpvm` in `~/.phpvm` and automatically upd
5256

5357
```sh
5458
export PHPVM_DIR="$HOME/.phpvm"
55-
source "$PHPVM_DIR/phpvm.sh"
59+
export PATH="$PHPVM_DIR/bin:$PATH"
60+
[ -s "$PHPVM_DIR/phpvm.sh" ] && . "$PHPVM_DIR/phpvm.sh"
5661
```
5762

5863
### Verify Installation
@@ -83,6 +88,12 @@ To switch between installed versions:
8388
phpvm use 8.0
8489
```
8590

91+
To switch back to the system PHP version:
92+
93+
```sh
94+
phpvm system
95+
```
96+
8697
Verify the active version with:
8798

8899
```sh
@@ -97,24 +108,34 @@ Create a `.phpvmrc` file in your project directory to specify the desired PHP ve
97108
echo "8.1" > .phpvmrc
98109
```
99110

100-
When you navigate to that project directory, `phpvm` will automatically switch to PHP 8.1.
101-
102-
### Uninstalling PHP Versions
103-
104-
To uninstall a specific PHP version:
111+
When you navigate to that project directory and run:
105112

106113
```sh
107-
phpvm uninstall 7.4
114+
phpvm auto
108115
```
109116

117+
phpvm will automatically detect and switch to the version specified in the `.phpvmrc` file.
118+
110119
### Listing Installed Versions
111120

112-
To list installed PHP versions:
121+
To list all installed PHP versions:
113122

114123
```sh
115124
phpvm list
116125
```
117126

127+
This will show all installed PHP versions and indicate the currently active one.
128+
129+
### Running Self-Tests
130+
131+
phpvm includes built-in self-tests to verify everything is working correctly:
132+
133+
```sh
134+
phpvm test
135+
```
136+
137+
This will run a series of tests on your system to ensure all phpvm functions are working properly.
138+
118139
## Uninstallation
119140

120141
To completely remove `phpvm`, run:
@@ -127,23 +148,57 @@ Then remove the following lines from your shell profile (`~/.bashrc`, `~/.zshrc`
127148

128149
```sh
129150
export PHPVM_DIR="$HOME/.phpvm"
130-
source "$PHPVM_DIR/phpvm.sh"
151+
export PATH="$PHPVM_DIR/bin:$PATH"
152+
[ -s "$PHPVM_DIR/phpvm.sh" ] && . "$PHPVM_DIR/phpvm.sh"
131153
```
132154

133155
## Troubleshooting
134156

135157
If you experience issues with `phpvm`, try the following:
136158

137-
- Ensure your shell profile is sourcing `phpvm.sh`.
138-
- Restart your terminal after installing or updating.
139-
- Verify that Homebrew is installed (for macOS users).
140-
- Check for permission issues during the installation or PHP version switching process.
141-
- Refer to the [Changelog](./CHANGELOG.md) for recent updates and fixes.
159+
- Run `phpvm test` to verify all functions are working correctly
160+
- Ensure your shell profile is sourcing `phpvm.sh`
161+
- Restart your terminal after installing or updating
162+
- Verify that the required package manager is installed:
163+
- Homebrew for macOS
164+
- apt, dnf, yum, or pacman for Linux
165+
- Check for permission issues during the installation or PHP version switching process
166+
- For Linux systems, you may need to use `sudo` for installation and switching
167+
- Refer to the [Changelog](./CHANGELOG.md) for recent updates and fixes
142168

143169
## Development & Testing
144170

145171
- This project includes a set of BATS unit tests located in the repository (e.g., `test_phpvm.bats`).
146172
- Contributions, bug reports, and feature requests are welcome.
173+
- The codebase now includes helper functions for better maintainability and reduced duplication.
174+
175+
### Testing
176+
177+
phpvm includes built-in self-tests that validate all core functionality:
178+
179+
```sh
180+
# Run the built-in tests
181+
phpvm test
182+
```
183+
184+
The test framework verifies:
185+
186+
- Output and formatting functions
187+
- System detection
188+
- PHP version installation (mocked)
189+
- Version switching
190+
- Auto-switching based on .phpvmrc
191+
- System PHP integration
192+
193+
These tests can be run in any environment with a shell and don't require external testing frameworks.
194+
195+
### Debugging
196+
197+
To enable debug output, set the `DEBUG` environment variable to `true`:
198+
199+
```sh
200+
DEBUG=true phpvm install 8.1
201+
```
147202

148203
## Maintainers
149204

install-bats.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
# Script to install BATS (Bash Automated Testing System)
3+
4+
set -e
5+
6+
# Detect OS
7+
if [ "$(uname)" = "Darwin" ]; then
8+
# macOS
9+
echo "Detected macOS, using Homebrew to install BATS..."
10+
if ! command -v brew >/dev/null 2>&1; then
11+
echo "Homebrew not found. Please install Homebrew first:"
12+
echo " /bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
13+
exit 1
14+
fi
15+
16+
if ! brew list bats-core >/dev/null 2>&1; then
17+
brew install bats-core
18+
else
19+
echo "BATS already installed"
20+
fi
21+
22+
elif [ -f /etc/debian_version ]; then
23+
# Debian/Ubuntu
24+
echo "Detected Debian/Ubuntu, using apt to install BATS..."
25+
if ! command -v bats >/dev/null 2>&1; then
26+
sudo apt-get update
27+
sudo apt-get install -y bats
28+
else
29+
echo "BATS already installed"
30+
fi
31+
32+
elif [ -f /etc/redhat-release ]; then
33+
# RHEL/CentOS
34+
echo "Detected RHEL/CentOS, installing BATS from GitHub..."
35+
if ! command -v bats >/dev/null 2>&1; then
36+
# Create temporary directory for installation
37+
TEMP_DIR=$(mktemp -d)
38+
cd "$TEMP_DIR"
39+
40+
# Clone and install BATS
41+
git clone https://github.com/bats-core/bats-core.git
42+
cd bats-core
43+
sudo ./install.sh /usr/local
44+
45+
# Clean up
46+
cd /
47+
rm -rf "$TEMP_DIR"
48+
else
49+
echo "BATS already installed"
50+
fi
51+
52+
else
53+
# Other Linux or Unix
54+
echo "Installing BATS from GitHub..."
55+
if ! command -v bats >/dev/null 2>&1; then
56+
# Create temporary directory for installation
57+
TEMP_DIR=$(mktemp -d)
58+
cd "$TEMP_DIR"
59+
60+
# Clone and install BATS
61+
git clone https://github.com/bats-core/bats-core.git
62+
cd bats-core
63+
sudo ./install.sh /usr/local
64+
65+
# Clean up
66+
cd /
67+
rm -rf "$TEMP_DIR"
68+
else
69+
echo "BATS already installed"
70+
fi
71+
fi
72+
73+
# Verify installation
74+
if command -v bats >/dev/null 2>&1; then
75+
echo "BATS installed successfully!"
76+
echo "Version information:"
77+
bats --version
78+
echo ""
79+
echo "To run phpvm tests, use: bats test_phpvm.bats"
80+
else
81+
echo "BATS installation failed."
82+
exit 1
83+
fi

install.sh

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@
6363
elif [ "$SHELL_NAME" = "bash" ]; then
6464
if [ -f "$HOME/.bashrc" ]; then
6565
DETECTED_PROFILE="$HOME/.bashrc"
66+
elif [ -f "$HOME/.bash_profile" ]; then
67+
DETECTED_PROFILE="$HOME/.bash_profile"
6668
fi
6769
fi
6870

6971
if [ -z "$DETECTED_PROFILE" ]; then
70-
for EACH_PROFILE in ".profile" ".bashrc" ".zshrc"; do
72+
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
7173
if [ -f "$HOME/$EACH_PROFILE" ]; then
7274
DETECTED_PROFILE="$HOME/$EACH_PROFILE"
7375
break
@@ -100,9 +102,20 @@
100102
local PROFILE
101103
PROFILE="$(phpvm_detect_profile)"
102104

103-
if [ -n "$PROFILE" ] && ! grep -q 'phpvm.sh' "$PROFILE"; then
104-
phpvm_echo "Appending phpvm source to $PROFILE"
105-
printf "\nexport PHPVM_DIR=\"%s\"\nexport PATH=\"\$PHPVM_DIR/bin:\$PATH\"\n[ -s \"\$PHPVM_DIR/phpvm.sh\" ] && . \"\$PHPVM_DIR/phpvm.sh\"\n" "$(phpvm_install_dir)" >>"$PROFILE"
105+
if [ -n "$PROFILE" ]; then
106+
phpvm_echo "Adding phpvm to $PROFILE"
107+
108+
# Check shell type and use appropriate syntax
109+
if echo "$PROFILE" | grep -q "zsh"; then
110+
# For zsh - use proper if statement to prevent shell crash
111+
printf "\nexport PHPVM_DIR=\"%s\"\nexport PATH=\"\$PHPVM_DIR/bin:\$PATH\"\nif [[ -s \"\$PHPVM_DIR/phpvm.sh\" ]]; then\n source \"\$PHPVM_DIR/phpvm.sh\"\nfi\n" "$(phpvm_install_dir)" >>"$PROFILE"
112+
else
113+
# For bash and others - use POSIX compatible syntax
114+
printf "\nexport PHPVM_DIR=\"%s\"\nexport PATH=\"\$PHPVM_DIR/bin:\$PATH\"\n[ -s \"\$PHPVM_DIR/phpvm.sh\" ] && . \"\$PHPVM_DIR/phpvm.sh\"\n" "$(phpvm_install_dir)" >>"$PROFILE"
115+
fi
116+
else
117+
phpvm_warn "Could not detect profile file. Please manually add the following to your shell profile:"
118+
printf "\nexport PHPVM_DIR=\"%s\"\nexport PATH=\"\$PHPVM_DIR/bin:\$PATH\"\n[ -s \"\$PHPVM_DIR/phpvm.sh\" ] && . \"\$PHPVM_DIR/phpvm.sh\"\n" "$(phpvm_install_dir)"
106119
fi
107120

108121
phpvm_echo "Applying changes..."
@@ -111,11 +124,12 @@
111124
# Only source the profile if it exists
112125
if [ -f "$PROFILE" ]; then
113126
# Use . instead of source for POSIX compatibility
114-
. "$PROFILE" || true
127+
. "$PROFILE" 2>/dev/null || true
115128
fi
116129

117130
phpvm_echo "phpvm installation complete!"
118-
phpvm_echo "Run: phpvm use 8.4"
131+
phpvm_echo "You may need to restart your terminal or run: source $PROFILE"
132+
phpvm_echo "Then try: phpvm install 8.2 && phpvm use 8.2"
119133
}
120134

121135
phpvm_do_install

0 commit comments

Comments
 (0)