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,6 +760,106 @@ EOF
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
+ case " $PKG_MANAGER " in
776
+ brew)
777
+ mock_dir=" ${TEST_PREFIX:-/ tmp} /opt/homebrew/Cellar/php@$version "
778
+ ;;
779
+ apt)
780
+ mock_dir=" ${TEST_PREFIX:-/ tmp} /var/lib/dpkg/info/php$version "
781
+ ;;
782
+ dnf | yum)
783
+ mock_dir=" ${TEST_PREFIX:-/ tmp} /var/lib/rpm/php$version "
784
+ ;;
785
+ pacman)
786
+ mock_dir=" ${TEST_PREFIX:-/ tmp} /var/lib/pacman/local/php$version "
787
+ ;;
788
+ * )
789
+ phpvm_err " Test mode not supported for this package manager."
790
+ return 1
791
+ ;;
792
+ esac
793
+ rm -rf " $mock_dir "
794
+ phpvm_echo " PHP $version uninstalled."
795
+ return 0
796
+ fi
797
+
798
+ case " $PKG_MANAGER " in
799
+ brew)
800
+ if brew list --versions php@" $version " > /dev/null 2>&1 ; then
801
+ brew uninstall php@" $version " || {
802
+ phpvm_err " Failed to uninstall PHP $version with Homebrew."
803
+ return 1
804
+ }
805
+ phpvm_echo " PHP $version uninstalled."
806
+ else
807
+ phpvm_warn " PHP $version is not installed via Homebrew."
808
+ return 1
809
+ fi
810
+ ;;
811
+ apt)
812
+ if dpkg -l | grep -q " ^ii\s*php$version \s" ; then
813
+ run_with_sudo apt-get remove -y php" $version " || {
814
+ phpvm_err " Failed to uninstall PHP $version with apt."
815
+ return 1
816
+ }
817
+ phpvm_echo " PHP $version uninstalled."
818
+ else
819
+ phpvm_warn " PHP $version is not installed via apt."
820
+ return 1
821
+ fi
822
+ ;;
823
+ dnf | yum)
824
+ if $PKG_MANAGER list installed | grep -q " ^php$version $" ; then
825
+ run_with_sudo $PKG_MANAGER remove -y php" $version " || {
826
+ phpvm_err " Failed to uninstall PHP $version with $PKG_MANAGER ."
827
+ return 1
828
+ }
829
+ phpvm_echo " PHP $version uninstalled."
830
+ else
831
+ phpvm_warn " PHP $version is not installed via $PKG_MANAGER ."
832
+ return 1
833
+ fi
834
+ ;;
835
+ pacman)
836
+ if pacman -Qi php" $version " > /dev/null 2>&1 ; then
837
+ run_with_sudo pacman -R --noconfirm php" $version " || {
838
+ phpvm_err " Failed to uninstall PHP $version with pacman."
839
+ return 1
840
+ }
841
+ phpvm_echo " PHP $version uninstalled."
842
+ else
843
+ phpvm_warn " PHP $version is not installed via pacman."
844
+ return 1
845
+ fi
846
+ ;;
847
+ * )
848
+ phpvm_err " Uninstall not supported for this package manager."
849
+ return 1
850
+ ;;
851
+ esac
852
+
853
+ # Clean up symlink and active version if needed
854
+ if [ -f " $PHPVM_ACTIVE_VERSION_FILE " ] && [ " $( cat " $PHPVM_ACTIVE_VERSION_FILE " ) " = " $version " ]; then
855
+ rm -f " $PHPVM_CURRENT_SYMLINK "
856
+ rm -f " $PHPVM_ACTIVE_VERSION_FILE "
857
+ phpvm_warn " Active PHP version was uninstalled. Please select another version."
858
+ fi
859
+
860
+ return 0
861
+ }
862
+
763
863
# Main function to handle commands
764
864
main () {
765
865
# Only run if not being sourced
@@ -790,6 +890,13 @@ main() {
790
890
fi
791
891
install_php " $@ "
792
892
;;
893
+ uninstall)
894
+ if [ " $# " -eq 0 ]; then
895
+ phpvm_err " Missing PHP version argument for 'uninstall' command."
896
+ exit 1
897
+ fi
898
+ uninstall_php " $@ "
899
+ ;;
793
900
system)
794
901
system_php_version
795
902
;;
@@ -813,8 +920,46 @@ main() {
813
920
esac
814
921
}
815
922
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 " $@ "
923
+ # Robust execution detection with multiple fallbacks
924
+ phpvm_should_execute_main () {
925
+ # Layer 1: Explicit override (highest priority)
926
+ case " ${PHPVM_SOURCED:- auto} " in
927
+ true | 1 | yes) return 1 ;; # Don't execute
928
+ false | 0 | no) return 0 ;; # Execute
929
+ esac
930
+
931
+ # Layer 2: Test mode
932
+ [ " $PHPVM_TEST_MODE " = " true" ] && return 1
933
+
934
+ # Layer 3: Return test (most reliable POSIX method)
935
+ if (return 0 2> /dev/null); then
936
+ return 1 # Sourced
937
+ else
938
+ return 0 # Executed
939
+ fi
940
+ }
941
+
942
+ # Safe main execution with error handling
943
+ if phpvm_should_execute_main " $@ " ; then
944
+ phpvm_debug " Executing main with $# arguments"
945
+
946
+ # Verify main function exists
947
+ if command -v main > /dev/null 2>&1 ; then
948
+ main " $@ "
949
+ else
950
+ phpvm_err " main function not found - script may be corrupted"
951
+ exit 1
952
+ fi
953
+ else
954
+ phpvm_debug " Script sourced for function loading"
955
+
956
+ # Set environment for shell integration
957
+ PHPVM_FUNCTIONS_LOADED=true
958
+ export PHPVM_FUNCTIONS_LOADED
959
+
960
+ # Auto-use .phpvmrc if enabled and present
961
+ if [ " ${PHPVM_AUTO_USE:- true} " = " true" ] && [ -f " .phpvmrc" ]; then
962
+ command -v auto_switch_php_version > /dev/null 2>&1 &&
963
+ auto_switch_php_version 2> /dev/null || true
964
+ fi
820
965
fi
0 commit comments