|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Save the PATH as it was when the test started to restore it when it finishes |
| 4 | +ORIG_PATH="${PATH}" |
| 5 | + |
| 6 | +cleanup() { |
| 7 | + # Restore the PATH as it was when the test started |
| 8 | + export PATH="${ORIG_PATH}" |
| 9 | + rm -rf "${TMP_DIR}" |
| 10 | +} |
| 11 | + |
| 12 | +die() { |
| 13 | + cleanup |
| 14 | + echo "$@" |
| 15 | + exit 1 |
| 16 | +} |
| 17 | + |
| 18 | +. ../../../nvm.sh |
| 19 | + |
| 20 | +# Sets the PATH for these tests to include the symlinks to the mocked binaries |
| 21 | +export PATH=".:${PATH}" |
| 22 | + |
| 23 | +TMP_DIR=$(mktemp -d) |
| 24 | +CHROOT_WITH_ALPINE="$TMP_DIR/with_alpine" |
| 25 | +CHROOT_WITHOUT_ALPINE="$TMP_DIR/without_alpine" |
| 26 | + |
| 27 | +setup_chroot() { |
| 28 | + chroot_dir=$1 |
| 29 | + |
| 30 | + # Directories |
| 31 | + mkdir -p "${chroot_dir}/etc" |
| 32 | + mkdir -p "${chroot_dir}/bin" |
| 33 | + mkdir -p "${chroot_dir}/usr/bin" |
| 34 | + mkdir -p "${chroot_dir}/lib64" |
| 35 | + mkdir -p "${chroot_dir}/dev" |
| 36 | + |
| 37 | + # Files and binaries |
| 38 | + cp ../../../nvm.sh "${chroot_dir}/" |
| 39 | + cp /bin/sh /usr/bin/dirname "${chroot_dir}/bin/" |
| 40 | + [ "${chroot_dir}" = "${CHROOT_WITH_ALPINE}" ] && touch "${chroot_dir}/etc/alpine-release" |
| 41 | + |
| 42 | + # Libraries |
| 43 | + for binary in /bin/sh /usr/bin/dirname; do |
| 44 | + for lib in $(ldd $binary | awk '{print $3}' | grep "^/"); do |
| 45 | + dir=$(dirname "${lib}") |
| 46 | + mkdir -p "${chroot_dir}${dir}" |
| 47 | + cp "${lib}" "${chroot_dir}${dir}/" |
| 48 | + done |
| 49 | + done |
| 50 | + |
| 51 | + # Dynamic linker |
| 52 | + cp /lib64/ld-linux-x86-64.so.2 "${chroot_dir}/lib64/" |
| 53 | + |
| 54 | + # /dev/null |
| 55 | + sudo mknod "${chroot_dir}/dev/null" c 1 3 |
| 56 | +} |
| 57 | + |
| 58 | +setup_chroot "${CHROOT_WITH_ALPINE}" |
| 59 | +setup_chroot "${CHROOT_WITHOUT_ALPINE}" |
| 60 | + |
| 61 | +# Run tests in chroot environments |
| 62 | +ARCH_WITH_ALPINE=$(sudo chroot "${CHROOT_WITH_ALPINE}" /bin/sh -c ". ./nvm.sh && nvm_get_arch") |
| 63 | +[ "${ARCH_WITH_ALPINE}" = "x64-musl" ] || die "Expected x64-musl for alpine environment but got ${ARCH_WITH_ALPINE}" |
| 64 | + |
| 65 | +ARCH_WITHOUT_ALPINE=$(sudo chroot "${CHROOT_WITHOUT_ALPINE}" /bin/sh -c ". ./nvm.sh && nvm_get_arch") |
| 66 | +[ "${ARCH_WITHOUT_ALPINE}" != "x64-musl" ] || die "Did not expect x64-musl for non-alpine environment" |
| 67 | + |
| 68 | +# Run tests for nvm ls-remote |
| 69 | +test_default_ls_remote() { |
| 70 | + mock_response='N/A' |
| 71 | + result=$(NVM_NODEJS_ORG_MIRROR='http://nonexistent-url' nvm ls-remote 18) |
| 72 | + if [ "${result}" = "${mock_response}" ]; then |
| 73 | + die "Test failed: Expected '${mock_response}' for but got '${result}'" |
| 74 | + else |
| 75 | + echo "Test passed" |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +test_unofficial_mirror_ls_remote() { |
| 80 | + mock_response='v18.18.0 (LTS: Hydrogen)' |
| 81 | + result=$(NVM_NODEJS_ORG_MIRROR='https://unofficial-builds.nodejs.org/download/release' nvm ls-remote 18.18.0 | sed -e 's/^[[:space:]]*//') |
| 82 | + result=$(echo "${result}" | sed 's/\x1b\[[0-9;]*m//g') |
| 83 | + |
| 84 | + if [ "${result}" = "${mock_response}" ]; then |
| 85 | + echo "Test passed" |
| 86 | + else |
| 87 | + die "Test failed: Expected '${mock_response}' but got '${result}'" |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +test_default_ls_remote |
| 92 | +test_unofficial_mirror_ls_remote |
| 93 | + |
| 94 | +cleanup |
0 commit comments