mirror of
				https://github.com/nvm-sh/nvm.git
				synced 2025-10-31 10:15:53 +08:00 
			
		
		
		
	Create nvm_ensure_version_installed
				
					
				
			This commit is contained in:
		
							
								
								
									
										18
									
								
								nvm.sh
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								nvm.sh
									
									
									
									
									
								
							| @@ -189,6 +189,24 @@ nvm_version_path() { | |||||||
|   fi |   fi | ||||||
| } | } | ||||||
|  |  | ||||||
|  | nvm_ensure_version_installed() { | ||||||
|  |   local PROVIDED_VERSION | ||||||
|  |   PROVIDED_VERSION="$1" | ||||||
|  |   local LOCAL_VERSION | ||||||
|  |   LOCAL_VERSION="$(nvm_version "$PROVIDED_VERSION")" | ||||||
|  |   local NVM_VERSION_DIR | ||||||
|  |   NVM_VERSION_DIR="$(nvm_version_path "$LOCAL_VERSION")" | ||||||
|  |   if [ ! -d "$NVM_VERSION_DIR" ]; then | ||||||
|  |     VERSION="$(nvm_resolve_alias "$PROVIDED_VERSION")" | ||||||
|  |     if [ $? -eq 0 ]; then | ||||||
|  |       echo "N/A: version \"$PROVIDED_VERSION -> $VERSION\" is not yet installed" >&2 | ||||||
|  |     else | ||||||
|  |       echo "N/A: version \"$(nvm_ensure_version_prefix "$PROVIDED_VERSION")\" is not yet installed" >&2 | ||||||
|  |     fi | ||||||
|  |     return 1 | ||||||
|  |   fi | ||||||
|  | } | ||||||
|  |  | ||||||
| # Expand a version using the version cache | # Expand a version using the version cache | ||||||
| nvm_version() { | nvm_version() { | ||||||
|   local PATTERN |   local PATTERN | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								test/fast/Unit tests/nvm_ensure_version_installed
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										23
									
								
								test/fast/Unit tests/nvm_ensure_version_installed
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | #!/bin/sh | ||||||
|  |  | ||||||
|  | die () { echo $@ ; cleanup ; exit 1; } | ||||||
|  | cleanup () { | ||||||
|  |   rm -rf "$(nvm_version_path v0.1.2)" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | . ../../../nvm.sh | ||||||
|  |  | ||||||
|  | mkdir -p "$(nvm_version_path v0.1.2)" | ||||||
|  |  | ||||||
|  | OUTPUT="$(nvm_ensure_version_installed foo 2>&1)" | ||||||
|  | EXIT_CODE=$? | ||||||
|  | EXPECTED_OUTPUT='N/A: version "foo" is not yet installed' | ||||||
|  | [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed foo' to give $EXPECTED_OUTPUT, got $OUTPUT" | ||||||
|  | [ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm_ensure_version_installed foo' to exit with 1, got $EXIT_CODE" | ||||||
|  |  | ||||||
|  | OUTPUT="$(nvm_ensure_version_installed 0.1)" | ||||||
|  | EXIT_CODE=$? | ||||||
|  | [ "_$OUTPUT" = "_" ] || die "expected 'nvm_ensure_version_installed 0.1' to have no output, got $OUTPUT" | ||||||
|  | [ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm_ensure_version_installed 0.1' to exit with 0, got $EXIT_CODE" | ||||||
|  |  | ||||||
|  | cleanup | ||||||
		Reference in New Issue
	
	Block a user