Skip to content

Commit 9aa8aa5

Browse files
authored
Merge pull request #3 from Thavarshan/development
PHPVM v1.3.0 - Enhanced Functionality and Error Handling
2 parents 6a62d19 + 42dc6d5 commit 9aa8aa5

File tree

8 files changed

+760
-297
lines changed

8 files changed

+760
-297
lines changed

.github/workflows/test.yml

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

33
on:
44
push:
55
branches:
66
- main
7+
- development
78
pull_request:
9+
branches:
10+
- main
11+
- development
812

913
jobs:
1014
test:
11-
runs-on: macos-latest
15+
runs-on: ${{ matrix.os }}
16+
strategy:
17+
matrix:
18+
os: [ubuntu-latest, macos-latest]
1219

1320
steps:
14-
- name: Checkout Repository
15-
uses: actions/checkout@v3
21+
- uses: actions/checkout@v3
1622

17-
- name: Install Dependencies
18-
run: brew install bats
23+
# Set up permissions for the script
24+
- name: Make script executable
25+
run: chmod +x ./phpvm.sh
1926

20-
- name: Run PHPVM Tests
21-
run: bats test_phpvm.bats
27+
# Run the integrated self-tests
28+
- name: Run integrated tests
29+
run: ./phpvm.sh test

.github/workflows/use.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
- development
78
pull_request:
89

910
jobs:
@@ -38,3 +39,26 @@ jobs:
3839
- name: Verify PHP Version
3940
run: |
4041
php -v
42+
43+
- name: Switch to System PHP
44+
run: |
45+
./phpvm.sh system
46+
47+
- name: Verify System PHP Switch
48+
run: |
49+
php -v
50+
51+
- name: Create Project with .phpvmrc
52+
run: |
53+
mkdir -p test_project
54+
echo "8.3" > test_project/.phpvmrc
55+
cd test_project
56+
57+
- name: Test Auto-Switch
58+
run: |
59+
cd test_project
60+
../phpvm.sh auto
61+
62+
- name: Verify Auto-Switch
63+
run: |
64+
php -v

CHANGELOG.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
# Release Notes
22

3+
## [v1.3.0](https://github.com/Thavarshan/phpvm/compare/v1.2.0...v1.3.0) - 2025-05-11
4+
5+
## Added
6+
7+
- Added `system` command to easily switch back to system PHP version
8+
- Added timestamps to all log messages for better traceability and debugging
9+
- Added log levels (INFO, ERROR, WARNING, DEBUG) for more structured logging
10+
- Added `run_with_sudo` helper function to centralize sudo usage logic
11+
- Added comprehensive self-tests with `phpvm test` command
12+
- Added test for corrupted `.phpvmrc` file handling
13+
- Added better support for detecting and using latest PHP version from Homebrew
14+
- Added improved error messages with more detailed information
15+
- Added ability to run self-tests with `phpvm test` command
16+
- Added debugging capability via `DEBUG=true` environment variable
17+
18+
## Changed
19+
20+
- Changed logging format to include timestamps and log levels
21+
- Changed sudo handling to use a centralized helper function
22+
- Changed path expansion to use `$HOME` instead of tilde notation for better compatibility
23+
- Changed error handling to provide more descriptive and actionable messages
24+
- Changed test framework to be integrated directly into the script
25+
- Changed help message to include information about the `test` command
26+
- Improved bash/zsh shell compatibility with better sourcing logic
27+
- Improved code organization and reduced duplication with helper functions
28+
29+
## Fixed
30+
31+
- Fixed shell crash issue when sourcing in zsh with p10k theme
32+
- Fixed path expansion issues in Ubuntu bashrc configurations
33+
- Fixed missing system PHP switching functionality on macOS
34+
- Fixed detection of latest PHP version on macOS when installed via Homebrew's generic 'php' formula
35+
- Fixed potential sudo permission issues on Linux by using `run_with_sudo` consistently
36+
- Fixed auto-switching edge case with invalid or corrupted `.phpvmrc` files
37+
- Fixed script execution issues when sourced from shell initialization files
38+
- Fixed various edge cases in version detection and switching
39+
340
## [v1.2.0](https://github.com/Thavarshan/phpvm/compare/v1.1.0...v1.2.0) - 2025-02-15
441

542
### Added

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

0 commit comments

Comments
 (0)