5
5
6
6
# Define a debug mode for testing
7
7
if [ " ${BATS_TEST_FILENAME:- } " != " " ]; then
8
- # Script is being tested
9
- # Don't execute main automatically
10
- PHPVM_TEST_MODE=true
11
- echo " TEST MODE ACTIVE" >&2
8
+ # Script is being tested
9
+ # Don't execute main automatically
10
+ PHPVM_TEST_MODE=true
11
+ echo " TEST MODE ACTIVE" >&2
12
12
else
13
- PHPVM_TEST_MODE=false
13
+ PHPVM_TEST_MODE=false
14
14
fi
15
15
16
16
# Fix to prevent shell crash when sourced
@@ -184,13 +184,13 @@ use_php_version() {
184
184
# Handle test mode specifically
185
185
if [ " ${PHPVM_TEST_MODE} " = " true" ]; then
186
186
if [ " $version " = " system" ]; then
187
- echo " system" > " $PHPVM_ACTIVE_VERSION_FILE "
187
+ echo " system" > " $PHPVM_ACTIVE_VERSION_FILE "
188
188
phpvm_echo " Switched to system PHP."
189
189
return 0
190
190
fi
191
191
192
192
if [ -d " ${TEST_PREFIX:-/ tmp} /opt/homebrew/Cellar/php@$version " ]; then
193
- echo " $version " > " $PHPVM_ACTIVE_VERSION_FILE "
193
+ echo " $version " > " $PHPVM_ACTIVE_VERSION_FILE "
194
194
phpvm_echo " Switched to PHP $version ."
195
195
return 0
196
196
else
543
543
output_file=$( mktemp)
544
544
545
545
# Run the command, redirecting both stdout and stderr to the temporary file
546
- " $@ " > " $output_file " 2>&1
546
+ " $@ " > " $output_file " 2>&1
547
547
548
548
# Check if the output contains the expected text
549
549
if grep -q " $expected " " $output_file " ; then
562
562
local dir=" $1 "
563
563
shift
564
564
565
- " $@ " > /dev/null 2>&1
565
+ " $@ " > /dev/null 2>&1
566
566
if [ -d " $dir " ]; then
567
567
return 0
568
568
else
580
580
# Test output functions with timestamps
581
581
test_output_functions () {
582
582
# Check that output contains timestamp format [YYYY-MM-DD HH:MM:SS]
583
- assert_output_contains " [INFO]" phpvm_echo " Test message" && \
584
- assert_output_contains " [ERROR]" phpvm_err " Test error" && \
585
- assert_output_contains " [WARNING]" phpvm_warn " Test warning"
583
+ assert_output_contains " [INFO]" phpvm_echo " Test message" &&
584
+ assert_output_contains " [ERROR]" phpvm_err " Test error" &&
585
+ assert_output_contains " [WARNING]" phpvm_warn " Test warning"
586
586
}
587
587
588
588
# Test timestamp format in logs
638
638
639
639
# Test install_php
640
640
test_install_php () {
641
- install_php " 7.4" > /dev/null
641
+ install_php " 7.4" > /dev/null
642
642
local status=$?
643
643
644
644
# Check for success and file existence
651
651
mkdir -p
" $TEST_DIR /opt/homebrew/Cellar/[email protected] /bin"
652
652
653
653
# Test switching
654
- use_php_version " 7.4" > /dev/null
654
+ use_php_version " 7.4" > /dev/null
655
655
local status=$?
656
656
657
657
# Check for success and correct active version
660
660
661
661
# Test system_php_version
662
662
test_system_php_version () {
663
- system_php_version > /dev/null
663
+ system_php_version > /dev/null
664
664
local status=$?
665
665
666
666
# Check for success and correct active version
@@ -674,13 +674,13 @@ EOF
674
674
675
675
# Create a project with .phpvmrc
676
676
mkdir -p " $HOME /project"
677
- echo " 7.4" > " $HOME /project/.phpvmrc"
677
+ echo " 7.4" > " $HOME /project/.phpvmrc"
678
678
679
679
# Change to the project directory
680
680
cd " $HOME /project"
681
681
682
682
# Test auto-switching
683
- auto_switch_php_version > /dev/null
683
+ auto_switch_php_version > /dev/null
684
684
local status=$?
685
685
686
686
# Check for success and correct active version
760
760
fi
761
761
}
762
762
763
+ # Uninstall a specific PHP version
764
+ uninstall_php () {
765
+ version=" $1 "
766
+ [ -z " $version " ] && {
767
+ phpvm_err " No PHP version specified for uninstallation."
768
+ return 1
769
+ }
770
+
771
+ phpvm_echo " Uninstalling PHP $version ..."
772
+
773
+ # If in test mode, just remove the mock directory
774
+ if [ " ${PHPVM_TEST_MODE} " = " true" ]; then
775
+ rm -rf " ${TEST_PREFIX:-/ tmp} /opt/homebrew/Cellar/php@$version "
776
+ phpvm_echo " PHP $version uninstalled."
777
+ return 0
778
+ fi
779
+
780
+ case " $PKG_MANAGER " in
781
+ brew)
782
+ if brew list --versions php@" $version " > /dev/null 2>&1 ; then
783
+ brew uninstall php@" $version " || {
784
+ phpvm_err " Failed to uninstall PHP $version with Homebrew."
785
+ return 1
786
+ }
787
+ phpvm_echo " PHP $version uninstalled."
788
+ else
789
+ phpvm_warn " PHP $version is not installed via Homebrew."
790
+ return 1
791
+ fi
792
+ ;;
793
+ apt)
794
+ if dpkg -l | grep -q " php$version " ; then
795
+ run_with_sudo apt-get remove -y php" $version " || {
796
+ phpvm_err " Failed to uninstall PHP $version with apt."
797
+ return 1
798
+ }
799
+ phpvm_echo " PHP $version uninstalled."
800
+ else
801
+ phpvm_warn " PHP $version is not installed via apt."
802
+ return 1
803
+ fi
804
+ ;;
805
+ dnf | yum)
806
+ if $PKG_MANAGER list installed | grep -q " php$version " ; then
807
+ run_with_sudo $PKG_MANAGER remove -y php" $version " || {
808
+ phpvm_err " Failed to uninstall PHP $version with $PKG_MANAGER ."
809
+ return 1
810
+ }
811
+ phpvm_echo " PHP $version uninstalled."
812
+ else
813
+ phpvm_warn " PHP $version is not installed via $PKG_MANAGER ."
814
+ return 1
815
+ fi
816
+ ;;
817
+ pacman)
818
+ if pacman -Q | grep -q " ^php$version " ; then
819
+ run_with_sudo pacman -R --noconfirm php" $version " || {
820
+ phpvm_err " Failed to uninstall PHP $version with pacman."
821
+ return 1
822
+ }
823
+ phpvm_echo " PHP $version uninstalled."
824
+ else
825
+ phpvm_warn " PHP $version is not installed via pacman."
826
+ return 1
827
+ fi
828
+ ;;
829
+ * )
830
+ phpvm_err " Uninstall not supported for this package manager."
831
+ return 1
832
+ ;;
833
+ esac
834
+
835
+ # Clean up symlink and active version if needed
836
+ if [ -f " $PHPVM_ACTIVE_VERSION_FILE " ] && [ " $( cat " $PHPVM_ACTIVE_VERSION_FILE " ) " = " $version " ]; then
837
+ rm -f " $PHPVM_CURRENT_SYMLINK "
838
+ rm -f " $PHPVM_ACTIVE_VERSION_FILE "
839
+ phpvm_warn " Active PHP version was uninstalled. Please select another version."
840
+ fi
841
+
842
+ return 0
843
+ }
844
+
763
845
# Main function to handle commands
764
846
main () {
765
847
# Only run if not being sourced
@@ -790,6 +872,13 @@ main() {
790
872
fi
791
873
install_php " $@ "
792
874
;;
875
+ uninstall)
876
+ if [ " $# " -eq 0 ]; then
877
+ phpvm_err " Missing PHP version argument for 'uninstall' command."
878
+ exit 1
879
+ fi
880
+ uninstall_php " $@ "
881
+ ;;
793
882
system)
794
883
system_php_version
795
884
;;
@@ -813,8 +902,46 @@ main() {
813
902
esac
814
903
}
815
904
816
- # This allows the script to be sourced without running main
817
- if [ " $PHPVM_TEST_MODE " != " true" ] && [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
818
- # Script is being executed directly and not in test mode
819
- main " $@ "
905
+ # Robust execution detection with multiple fallbacks
906
+ phpvm_should_execute_main () {
907
+ # Layer 1: Explicit override (highest priority)
908
+ case " ${PHPVM_SOURCED:- auto} " in
909
+ true | 1 | yes) return 1 ;; # Don't execute
910
+ false | 0 | no) return 0 ;; # Execute
911
+ esac
912
+
913
+ # Layer 2: Test mode
914
+ [ " $PHPVM_TEST_MODE " = " true" ] && return 1
915
+
916
+ # Layer 3: Return test (most reliable POSIX method)
917
+ if (return 0 2> /dev/null); then
918
+ return 1 # Sourced
919
+ else
920
+ return 0 # Executed
921
+ fi
922
+ }
923
+
924
+ # Safe main execution with error handling
925
+ if phpvm_should_execute_main " $@ " ; then
926
+ phpvm_debug " Executing main with $# arguments"
927
+
928
+ # Verify main function exists
929
+ if command -v main > /dev/null 2>&1 ; then
930
+ main " $@ "
931
+ else
932
+ phpvm_err " main function not found - script may be corrupted"
933
+ exit 1
934
+ fi
935
+ else
936
+ phpvm_debug " Script sourced for function loading"
937
+
938
+ # Set environment for shell integration
939
+ PHPVM_FUNCTIONS_LOADED=true
940
+ export PHPVM_FUNCTIONS_LOADED
941
+
942
+ # Auto-use .phpvmrc if enabled and present
943
+ if [ " ${PHPVM_AUTO_USE:- true} " = " true" ] && [ -f " .phpvmrc" ]; then
944
+ command -v auto_switch_php_version > /dev/null 2>&1 &&
945
+ auto_switch_php_version 2> /dev/null || true
946
+ fi
820
947
fi
0 commit comments