From f43618fc156a925c4dcf8af3b88c77b6bc0ed849 Mon Sep 17 00:00:00 2001 From: nmarghetti Date: Mon, 28 Dec 2020 16:55:03 +0100 Subject: [PATCH] Make it work on Windows with WSL, MSYS, Cygwin --- .github/workflows/windows-npm.yml | 67 ++++++++++ .travis.yml | 5 + README.md | 8 +- install.sh | 18 ++- nvm.sh | 181 +++++++++++++++++++++----- test/fast/Unit tests/nvm_get_checksum | 4 +- test/install_script/nvm_source | 12 +- test/nvm/nvm_ensure_posix_path | 43 ++++++ 8 files changed, 292 insertions(+), 46 deletions(-) create mode 100755 .github/workflows/windows-npm.yml create mode 100755 test/nvm/nvm_ensure_posix_path diff --git a/.github/workflows/windows-npm.yml b/.github/workflows/windows-npm.yml new file mode 100755 index 00000000000..3385090c256 --- /dev/null +++ b/.github/workflows/windows-npm.yml @@ -0,0 +1,67 @@ +name: 'Tests on Windows: `nvm install`' + +on: [pull_request, push] + +env: + NVM_INSTALL_GITHUB_USER: ${{ github.repository }} + NVM_INSTALL_VERSION: ${{ github.sha }} + +jobs: + node: + name: 'MSYS fail prefix nvm install' + runs-on: windows-latest + steps: + - name: Retrieve nvm + shell: bash + run: | + ./install.sh + . "$HOME/.nvm/nvm.sh" + ! nvm install --lts + + nodes: + name: 'MSYS nvm install' + runs-on: windows-latest + strategy: + matrix: + npm-node-version: + - '--lts' + - '--default 12' + - '--no-progress 10' + + steps: + - name: Retrieve nvm + shell: bash + run: | + unset npm_config_prefix + ./install.sh + . "$HOME/.nvm/nvm.sh" + nvm install ${{ matrix.npm-node-version }} + + wsl_nodes: + name: 'WSL nvm install' + runs-on: windows-latest + strategy: + matrix: + wsl-distrib: + - Debian + - Alpine + - Ubuntu-18.04 + npm-node-version: + - '--lts' + - '11' + steps: + - uses: Vampire/setup-wsl@v1 + with: + distribution: ${{ matrix.wsl-distrib }} + additional-packages: bash curl ca-certificates + wsl-shell-command: WSLENV=NVM_INSTALL_GITHUB_USER:NVM_INSTALL_VERSION/p bash -c "sudo -u test bash --noprofile --norc -euo pipefail " + - name: Retrieve nvm on WSL + shell: wsl-bash {0} + run: | + env | sort + export NVM_INSTALL_GITHUB_USER=${{ github.repository }} + export NVM_INSTALL_VERSION=${{ github.sha }} + curl -sSLo "install.sh" "https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}/install.sh" + ./install.sh + . "$HOME/.nvm/nvm.sh" + nvm install ${{ matrix.npm-node-version }} diff --git a/.travis.yml b/.travis.yml index 7e991ed54c8..e9454438410 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,11 @@ env: - NVM_DIR="${TRAVIS_BUILD_DIR}" matrix: - SHELL=bash TEST_SUITE=install_script + - SHELL=sh TEST_SUITE=nvm + - SHELL=dash TEST_SUITE=nvm + - SHELL=bash TEST_SUITE=nvm + - SHELL=zsh TEST_SUITE=nvm + # - SHELL=ksh TEST_SUITE=nvm - SHELL=sh TEST_SUITE=fast - SHELL=dash TEST_SUITE=fast - SHELL=bash TEST_SUITE=fast diff --git a/README.md b/README.md index c05612a6dc6..d9794d8f966 100644 --- a/README.md +++ b/README.md @@ -136,11 +136,17 @@ which should output `nvm` if the installation was successful. Please note that ` If you're running a system without prepackaged binary available, which means you're going to install nodejs or io.js from its source code, you need to make sure your system has a C++ compiler. For OS X, Xcode will work, for Debian/Ubuntu based GNU/Linux, the `build-essential` and `libssl-dev` packages work. -**Note:** `nvm` does not support Windows (see [#284](https://github.com/nvm-sh/nvm/issues/284)), but may work in WSL (Windows Subsystem for Linux) depending on the version of WSL. For Windows, two alternatives exist, which are neither supported nor developed by us: +**Note:** `nvm` also support Windows in some cases. It should work through WSL (Windows Subsystem for Linux) depending on the version of WSL. It should also work with GitBash (MSYS) or Cygwin. Otherwise, for Windows, two alternatives exist, which are neither supported nor developed by us: - [nvm-windows](https://github.com/coreybutler/nvm-windows) - [nodist](https://github.com/marcelklehr/nodist) +**Note:** `nvm` should work with [GitBash](https://gitforwindows.org/) if installed with script method (the git repository contains filenames with characters not supported on Windows): + +```sh +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | METHOD=script bash +``` + **Note:** `nvm` does not support [Fish] either (see [#303](https://github.com/nvm-sh/nvm/issues/303)). Alternatives exist, which are neither supported nor developed by us: - [bass](https://github.com/edc/bass) allows you to use utilities written for Bash in fish shell diff --git a/install.sh b/install.sh index 3c332897285..e13139a0d09 100755 --- a/install.sh +++ b/install.sh @@ -42,19 +42,29 @@ nvm_profile_is_bash_or_zsh() { # NVM_SOURCE always takes precedence unless the method is "script-nvm-exec" # nvm_source() { + local NVM_GITHUB_USER + NVM_GITHUB_USER="$NVM_INSTALL_GITHUB_USER" + if [ -z "${NVM_GITHUB_USER-}" ]; then + NVM_GITHUB_USER="nvm-sh" + fi + local NVM_VERSION + NVM_VERSION="$NVM_INSTALL_VERSION" + if [ -z "${NVM_INSTALL_VERSION-}" ]; then + NVM_VERSION="$(nvm_latest_version)" + fi local NVM_METHOD NVM_METHOD="$1" local NVM_SOURCE_URL NVM_SOURCE_URL="$NVM_SOURCE" if [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then - NVM_SOURCE_URL="https://raw.githubusercontent.com/nvm-sh/nvm/$(nvm_latest_version)/nvm-exec" + NVM_SOURCE_URL="https://raw.githubusercontent.com/${NVM_GITHUB_USER}/nvm/${NVM_VERSION}/nvm-exec" elif [ "_$NVM_METHOD" = "_script-nvm-bash-completion" ]; then - NVM_SOURCE_URL="https://raw.githubusercontent.com/nvm-sh/nvm/$(nvm_latest_version)/bash_completion" + NVM_SOURCE_URL="https://raw.githubusercontent.com/${NVM_GITHUB_USER}/nvm/${NVM_VERSION}/bash_completion" elif [ -z "$NVM_SOURCE_URL" ]; then if [ "_$NVM_METHOD" = "_script" ]; then - NVM_SOURCE_URL="https://raw.githubusercontent.com/nvm-sh/nvm/$(nvm_latest_version)/nvm.sh" + NVM_SOURCE_URL="https://raw.githubusercontent.com/${NVM_GITHUB_USER}/nvm/${NVM_VERSION}/nvm.sh" elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then - NVM_SOURCE_URL="https://github.com/nvm-sh/nvm.git" + NVM_SOURCE_URL="https://github.com/${NVM_GITHUB_USER}/nvm.git" else echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD" return 1 diff --git a/nvm.sh b/nvm.sh index 2f59708464a..4c84160f0d2 100644 --- a/nvm.sh +++ b/nvm.sh @@ -146,7 +146,12 @@ nvm_has_system_iojs() { } nvm_is_version_installed() { - [ -n "${1-}" ] && [ -x "$(nvm_version_path "$1" 2>/dev/null)"/bin/node ] + local NVM_OS + NVM_OS="$(nvm_get_os)" + local NODE + NODE='node' + [ "_${NVM_OS}" = "_win" ] && NODE='node.exe' + [ -n "${1-}" ] && [ -x "$(nvm_version_path "$1" 2>/dev/null)"/bin/"${NODE}" ] } nvm_print_npm_version() { @@ -326,10 +331,14 @@ nvm_tree_contains_path() { return 2 fi + local previous_pathdir + previous_pathdir="${node_path}" local pathdir - pathdir=$(dirname "${node_path}") - while [ "${pathdir}" != "" ] && [ "${pathdir}" != "." ] && [ "${pathdir}" != "/" ] && [ "${pathdir}" != "${tree}" ]; do - pathdir=$(dirname "${pathdir}") + pathdir=$(dirname "${previous_pathdir}") + while [ "${pathdir}" != "" ] && [ "${pathdir}" != "." ] && [ "${pathdir}" != "/" ] && + [ "${pathdir}" != "${tree}" ] && [ "${pathdir}" != "${previous_pathdir}" ]; do + previous_pathdir="${pathdir}" + pathdir=$(dirname "${previous_pathdir}") done [ "${pathdir}" = "${tree}" ] } @@ -1559,7 +1568,7 @@ nvm_get_checksum() { SHASUMS_URL="${MIRROR}/${3}/SHASUMS.txt" fi - nvm_download -L -s "${SHASUMS_URL}" -o - | command awk "{ if (\"${4}.tar.${5}\" == \$2) print \$1}" + nvm_download -L -s "${SHASUMS_URL}" -o - | command awk "{ if (\"${4}.${5}\" == \$2) print \$1}" } nvm_print_versions() { @@ -1761,6 +1770,7 @@ nvm_get_os() { SunOS\ *) NVM_OS=sunos ;; FreeBSD\ *) NVM_OS=freebsd ;; AIX\ *) NVM_OS=aix ;; + CYGWIN* | MSYS* | MINGW*) NVM_OS=win ;; esac nvm_echo "${NVM_OS-}" } @@ -1858,6 +1868,61 @@ nvm_get_mirror() { esac } +# args: os, prefixed version, version, tarball, extract directory +nvm_install_binary_extract() { + [ "$#" -ne 5 ] && nvm_err "nvm_install_binary_extract needs 5 parameters" && return 1 + + local NVM_OS + local PREFIXED_VERSION + local VERSION + local TARBALL + local TMPDIR + NVM_OS="${1}" + PREFIXED_VERSION="${2}" + VERSION="${3}" + TARBALL="${4}" + TMPDIR="${5}" + + local VERSION_PATH + + [ -n "${TMPDIR-}" ] && \ + command mkdir -p "${TMPDIR}" && \ + VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" || return 1 + + # For Windows system (GitBash with MSYS, Cygwin) + if [ "${NVM_OS}" = 'win' ]; then + VERSION_PATH="${VERSION_PATH}/bin" + command unzip -q "${TARBALL}" -d "${TMPDIR}" || return 1 + # For non Windows system (including WSL running on Windows) + else + local tar_compression_flag + tar_compression_flag='z' + if nvm_supports_xz "${VERSION}"; then + tar_compression_flag='J' + fi + + local tar + if [ "${NVM_OS}" = 'aix' ]; then + tar='gtar' + else + tar='tar' + fi + command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1 + fi + + command mkdir -p "${VERSION_PATH}" || return 1 + + if [ "${NVM_OS}" = 'win' ]; then + command mv "${TMPDIR}/"*/* "${VERSION_PATH}" || return 1 + else + command mv "${TMPDIR}/"* "${VERSION_PATH}" || return 1 + fi + + command rm -rf "${TMPDIR}" || return 1 + + return 0 +} + # args: flavor, type, version, reinstall nvm_install_binary() { local FLAVOR @@ -1882,19 +1947,15 @@ nvm_install_binary() { local VERSION VERSION="$(nvm_strip_iojs_prefix "${PREFIXED_VERSION}")" - if [ -z "$(nvm_get_os)" ]; then - return 2 - fi + local NVM_OS + NVM_OS="$(nvm_get_os)" - local tar_compression_flag - tar_compression_flag='z' - if nvm_supports_xz "${VERSION}"; then - tar_compression_flag='J' + if [ -z "${NVM_OS}" ]; then + return 2 fi local TARBALL local TMPDIR - local VERSION_PATH local PROGRESS_BAR local NODE_OR_IOJS @@ -1914,21 +1975,8 @@ nvm_install_binary() { if [ -f "${TARBALL}" ]; then TMPDIR="$(dirname "${TARBALL}")/files" fi - local tar - tar='tar' - if [ "${NVM_OS}" = 'aix' ]; then - tar='gtar' - fi - if ( - [ -n "${TMPDIR-}" ] && \ - command mkdir -p "${TMPDIR}" && \ - command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \ - VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" && \ - command mkdir -p "${VERSION_PATH}" && \ - command mv "${TMPDIR}/"* "${VERSION_PATH}" && \ - command rm -rf "${TMPDIR}" - ); then + if nvm_install_binary_extract "${NVM_OS}" "${PREFIXED_VERSION}" "${VERSION}" "${TARBALL}" "${TMPDIR}"; then if [ -n "${ALIAS-}" ]; then nvm alias "${ALIAS}" "${provided_version}" fi @@ -2028,10 +2076,15 @@ nvm_download_artifact() { local SLUG SLUG="$(nvm_get_download_slug "${FLAVOR}" "${KIND}" "${VERSION}")" + local NVM_OS + NVM_OS="$(nvm_get_os)" + local COMPRESSION - COMPRESSION='gz' - if nvm_supports_xz "${VERSION}"; then - COMPRESSION='xz' + COMPRESSION='tar.gz' + if [ "${NVM_OS}" = "win" ]; then + COMPRESSION='zip' + elif nvm_supports_xz "${VERSION}"; then + COMPRESSION='tar.xz' fi local CHECKSUM @@ -2049,13 +2102,13 @@ nvm_download_artifact() { ) local TARBALL - TARBALL="${tmpdir}/${SLUG}.tar.${COMPRESSION}" + TARBALL="${tmpdir}/${SLUG}.${COMPRESSION}" local TARBALL_URL if nvm_version_greater_than_or_equal_to "${VERSION}" 0.1.14; then - TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.tar.${COMPRESSION}" + TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.${COMPRESSION}" else # node <= 0.1.13 does not have a directory - TARBALL_URL="${MIRROR}/${SLUG}.tar.${COMPRESSION}" + TARBALL_URL="${MIRROR}/${SLUG}.${COMPRESSION}" fi if [ -r "${TARBALL}" ]; then @@ -2317,6 +2370,51 @@ nvm_npmrc_bad_news_bears() { return 1 } +# args: path to check and transform if needed +nvm_ensure_posix_path() { + [ -z "${1-}" ] && nvm_err "Need the location as first parameter" && return 1 + local location + location="$1" + + # If already posix path, just return it + [ "$(command printf '%s' "$location" | command cut -b 1)" = "/" ] && nvm_echo "$location" && return 0 + + # If absolute path (eg. starting with C:) + if command printf '%s' "$location" | command grep '^[a-zA-Z]:' >/dev/null 2>&1; then + # Get lower case drive letter, eg. c for C:\... + local letter + letter=$(command printf '%s' "$location" | command cut -b 1 | command tr '[:upper:]' '[:lower:]') + # Get upper case drive letter, eg. C for C:\... + local letter_upper + letter_upper=$(command printf '%s' "$letter" | command tr '[:lower:]' '[:upper:]') + local letter_prefix + if [ -d '/mnt/' ]; then + letter_prefix='/mnt' + elif [ -d '/drives/' ]; then + letter_prefix='/drives' + fi + + # prefix like /mnt/c, /drives/c or /c + if [ -d "$letter_prefix/$letter" ]; then + letter_prefix="$letter_prefix/$letter" + # prefix like /mnt/C, /drives/C or /C + elif [ -d "$letter_prefix/$letter_upper" ]; then + letter_prefix="$letter_prefix/$letter_upper" + else + nvm_err "Unable to convert the path to posix" + return 1 + fi + # Remove drive letter prefix (eg. C:) + location=$(command printf '%s' "$location" | command cut -b 3-) + # Set prefix + location="${letter_prefix}${location}" + fi + + # Replace \ by / + location=$(command printf '%s' "$location" | command sed 's#\\#/#g') + nvm_echo "${location}" +} + nvm_die_on_prefix() { local NVM_DELETE_PREFIX NVM_DELETE_PREFIX="${1-}" @@ -2346,6 +2444,9 @@ nvm_die_on_prefix() { return 3 fi + local NVM_OS + NVM_OS="$(nvm_get_os)" + # npm normalizes NPM_CONFIG_-prefixed env vars # https://github.com/npm/npmconf/blob/22827e4038d6eebaafeb5c13ed2b92cf97b8fb82/npmconf.js#L331-L348 # https://github.com/npm/npm/blob/5e426a78ca02d0044f8dd26e0c5f881217081cbd/lib/config/core.js#L343-L359 @@ -2357,6 +2458,9 @@ nvm_die_on_prefix() { if [ -n "${NVM_NPM_CONFIG_PREFIX_ENV-}" ]; then local NVM_CONFIG_VALUE eval "NVM_CONFIG_VALUE=\"\$${NVM_NPM_CONFIG_PREFIX_ENV}\"" + if [ -n "$NVM_CONFIG_VALUE" ] && [ "_${NVM_OS}" = "_win" ]; then + NVM_CONFIG_VALUE="$(nvm_ensure_posix_path "$NVM_CONFIG_VALUE")" + fi if [ -n "${NVM_CONFIG_VALUE-}" ] && ! nvm_tree_contains_path "${NVM_DIR}" "${NVM_CONFIG_VALUE}"; then nvm deactivate >/dev/null 2>&1 nvm_err "nvm is not compatible with the \"${NVM_NPM_CONFIG_PREFIX_ENV}\" environment variable: currently set to \"${NVM_CONFIG_VALUE}\"" @@ -3130,8 +3234,13 @@ nvm() { nvm_get_make_jobs fi - NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}" - EXIT_CODE=$? + if [ "_${NVM_OS}" = "_win" ]; then + nvm_err "Unable to install from source for Windows" + EXIT_CODE=1 + else + NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}" + EXIT_CODE=$? + fi fi fi @@ -3944,6 +4053,8 @@ nvm() { nvm_npmrc_bad_news_bears \ nvm_get_colors nvm_set_colors nvm_print_color_code nvm_format_help_message_colors \ nvm_echo_with_colors nvm_err_with_colors \ + nvm_ensure_posix_path \ + nvm_install_binary_extract \ >/dev/null 2>&1 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \ NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \ diff --git a/test/fast/Unit tests/nvm_get_checksum b/test/fast/Unit tests/nvm_get_checksum index 53e6839b11c..975a3e6972d 100755 --- a/test/fast/Unit tests/nvm_get_checksum +++ b/test/fast/Unit tests/nvm_get_checksum @@ -31,14 +31,14 @@ nvm_download() { nvm_get_checksum_alg() { echo 'sha-256' } -OUTPUT="$(nvm_get_checksum node std foo bar baz)" +OUTPUT="$(nvm_get_checksum node std foo bar tar.baz)" EXPECTED_OUTPUT="mirror-node-std/foo/SHASUMS256.txt" [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" nvm_get_checksum_alg() { echo 'sha-1' } -OUTPUT="$(nvm_get_checksum iojs std foo bar baz)" +OUTPUT="$(nvm_get_checksum iojs std foo bar tar.baz)" EXPECTED_OUTPUT="mirror-iojs-std/foo/SHASUMS.txt" [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" diff --git a/test/install_script/nvm_source b/test/install_script/nvm_source index 9c1ec080efb..ea277efadc9 100755 --- a/test/install_script/nvm_source +++ b/test/install_script/nvm_source @@ -9,16 +9,20 @@ die () { echo "$@" ; cleanup ; exit 1; } NVM_ENV=testing \. ../../install.sh # nvm_source with no parameter returns the git endpoint -echo $(nvm_source) | grep "nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo" +echo $(nvm_source) | grep "https://github.com/nvm-sh/nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo" +NVM_GITHUB_USER="other" echo $(nvm_source) | grep "https://github.com/other/nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo" # nvm_source with git parameter returns the location of the nvm repo -echo $(nvm_source "git") | grep "nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo" +echo $(nvm_source "git") | grep "https://github.com/nvm-sh/nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo" +NVM_GITHUB_USER="other" echo $(nvm_source "git") | grep "https://github.com/other/nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo" # nvm_source with script parameter returns the location of nvm.sh -echo $(nvm_source "script") | grep "nvm.sh$" > /dev/null || die "nvm_source \"script\" should return the location of nvm.sh" +echo $(nvm_source "script") | grep "https://raw.githubusercontent.com/nvm-sh/nvm/$(nvm_latest_version)/nvm.sh$" > /dev/null || die "nvm_source \"script\" should return the location of nvm.sh" +NVM_GITHUB_USER="other" NVM_INSTALL_VERSION="master" echo $(nvm_source "script") | grep "https://raw.githubusercontent.com/other/nvm/master/nvm.sh$" > /dev/null || die "nvm_source \"script\" should return the location of nvm.sh" # nvm_source with script-nvm-exec parameter returns the location of nvm-exec -echo $(nvm_source "script-nvm-exec") | grep "nvm-exec$" > /dev/null || die "nvm_source \"script-nvm-exec\" should return the location of nvm.sh" +echo $(nvm_source "script-nvm-exec") | grep "https://raw.githubusercontent.com/nvm-sh/nvm/$(nvm_latest_version)/nvm-exec$" > /dev/null || die "nvm_source \"script-nvm-exec\" should return the location of nvm.sh" +NVM_GITHUB_USER="other" NVM_INSTALL_VERSION="master" echo $(nvm_source "script-nvm-exec") | grep "https://raw.githubusercontent.com/other/nvm/master/nvm-exec$" > /dev/null || die "nvm_source \"script-nvm-exec\" should return the location of nvm.sh" # nvm_source with any other parameter errors out and exits nvm_source "anything" 2> /dev/null && die "nvm_source with invalid parameter should exit" diff --git a/test/nvm/nvm_ensure_posix_path b/test/nvm/nvm_ensure_posix_path new file mode 100755 index 00000000000..0a4c52ba39f --- /dev/null +++ b/test/nvm/nvm_ensure_posix_path @@ -0,0 +1,43 @@ +#!/bin/sh + +cleanup () { + unset -f die cleanup + unset NVM_SOURCE out + rmdir '/mnt/z' 2>/dev/null + rmdir '/mnt' 2>/dev/null + rmdir '/drives/z' 2>/dev/null + rmdir '/drives/w' 2>/dev/null + rmdir '/drives' 2>/dev/null +} +die () { echo "$@" ; cleanup ; exit 1; } + +NVM_ENV=testing \. ../../nvm.sh +mkdir -p '/mnt/z' +mkdir -p '/drives/z' +mkdir -p '/drives/w' + +# nvm_ensure_posix_path is available +type nvm_ensure_posix_path > /dev/null 2>&1 || die 'nvm_ensure_posix_path is not available' + +# nvm_ensure_posix_path with no parameter fails +[ "$(nvm_ensure_posix_path 2>&1)" = "Need the location as first parameter" ] || die 'nvm_ensure_posix_path without parameter does not fail' + +# nvm_ensure_posix_path does not change a path wich is already posix +[ "$(nvm_ensure_posix_path '/')" = "/" ] || die 'nvm_ensure_posix_path changed "/"' +[ "$(nvm_ensure_posix_path '/some/path')" = "/some/path" ] || die 'nvm_ensure_posix_path changed "/some/path"' + +# nvm_ensure_posix_path works with relative path +[ "$(nvm_ensure_posix_path 'some/path/to/something')" = "some/path/to/something" ] || die 'nvm_ensure_posix_path did not properply convert "some/path/to/something"' +[ "$(nvm_ensure_posix_path 'some\path\to\something')" = "some/path/to/something" ] || die 'nvm_ensure_posix_path did not properply convert "some\path\to\something"' + +# nvm_ensure_posix_path works with absolute path +[ "$(nvm_ensure_posix_path 'C:/some/path')" = "/c/some/path" ] || die 'nvm_ensure_posix_path did not properply convert "C:/some/path"' +[ "$(nvm_ensure_posix_path 'C:\some\path')" = "/c/some/path" ] || die 'nvm_ensure_posix_path did not properply convert "C:\some\path"' + +# nvm_ensure_posix_path works using /mnt with absolute path +[ "$(nvm_ensure_posix_path 'Z:/some/path')" = "/mnt/z/some/path" ] || die 'nvm_ensure_posix_path did not properply convert "z:/some/path"' + +# nvm_ensure_posix_path works using /drives with absolute path +[ "$(nvm_ensure_posix_path 'W:/some/path')" = "/drives/w/some/path" ] || die 'nvm_ensure_posix_path did not properply convert "W:/some/path"' + +cleanup