Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea0fcdbe16 | ||
|
|
f6e169b130 | ||
|
|
11f7827dfc | ||
|
|
62adf80079 | ||
|
|
dbda4b9387 | ||
|
|
a4fadf1eae | ||
|
|
ecf142d1f0 | ||
|
|
b0a377a524 | ||
|
|
887c2969b7 | ||
|
|
e8144d4194 | ||
|
|
0054ed6022 | ||
|
|
60e2ec3b8e | ||
|
|
944232943b | ||
|
|
e8cfc3975d | ||
|
|
ad42d90860 | ||
|
|
f3ad1d23b7 | ||
|
|
d66749b21c | ||
|
|
e837e16067 | ||
|
|
f07f4ed8f3 | ||
|
|
2abfc380ad | ||
|
|
cb7b66c729 | ||
|
|
f39a6cbc2d | ||
|
|
391a1ccde9 | ||
|
|
f7280d116e | ||
|
|
5671e688d2 | ||
|
|
e4fcf988c5 | ||
|
|
ceef142f8a | ||
|
|
68986f7778 | ||
|
|
a2c0ebaec1 | ||
|
|
e759a03ee4 | ||
|
|
6294413afa | ||
|
|
682b43037d | ||
|
|
6f5767a530 | ||
|
|
09e620c401 | ||
|
|
362f5e2c71 | ||
|
|
015c4f1e3b |
No files matched your search
+2
-2
@@ -28,8 +28,8 @@ We meet 78% of the “gold” criteria. The gaps are as follows:
|
||||
|
||||
## Threat Model
|
||||
|
||||
See [THREAT_MODEL.md](.github/THREAT_MODEL.md).
|
||||
See [THREAT_MODEL.md](https://github.com/nvm-sh/nvm/blob/HEAD/.github/THREAT_MODEL.md).
|
||||
|
||||
## Incident Response Plan
|
||||
|
||||
Please see our [Incident Response Plan](.github/INCIDENT_RESPONSE_PLAN.md).
|
||||
Please see our [Incident Response Plan](https://github.com/nvm-sh/nvm/blob/HEAD/.github/INCIDENT_RESPONSE_PLAN.md).
|
||||
@@ -37,6 +37,8 @@ jobs:
|
||||
matrix:
|
||||
node-version: ${{ fromJson(needs.matrix.outputs.latest) }}
|
||||
include:
|
||||
- node-version: "24.14"
|
||||
- node-version: "22.22.1"
|
||||
- node-version: "21"
|
||||
- node-version: "20.5"
|
||||
- node-version: "20.4"
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@ Before sending the pull request, make sure your code is running on the latest av
|
||||
|
||||
```
|
||||
git fetch upstream
|
||||
git rebase upstream/main
|
||||
git rebase upstream/master
|
||||
```
|
||||
|
||||
Verify your changes
|
||||
|
||||
@@ -20,14 +20,6 @@ ENV DEBIAN_FRONTEND=noninteractive
|
||||
# ShellCheck version
|
||||
ENV SHELLCHECK_VERSION=0.7.0
|
||||
|
||||
# Pick a Ubuntu apt mirror site for better speed
|
||||
# ref: https://launchpad.net/ubuntu/+archivemirrors
|
||||
ENV UBUNTU_APT_SITE=ubuntu.cs.utah.edu
|
||||
|
||||
# Replace origin apt package site with the mirror site
|
||||
RUN sed -E -i "s/([a-z]+.)?archive.ubuntu.com/$UBUNTU_APT_SITE/g" /etc/apt/sources.list
|
||||
RUN sed -i "s/security.ubuntu.com/$UBUNTU_APT_SITE/g" /etc/apt/sources.list
|
||||
|
||||
# Install apt packages
|
||||
RUN apt update && \
|
||||
apt upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" && \
|
||||
|
||||
@@ -263,7 +263,7 @@ You can use a task:
|
||||
ansible.builtin.shell: >
|
||||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash
|
||||
args:
|
||||
creates: "{{ ansible_env.HOME }}/.nvm/nvm.sh"
|
||||
creates: "{{ ansible_facts.env.HOME }}/.nvm/nvm.sh"
|
||||
```
|
||||
|
||||
### Verify Installation
|
||||
@@ -288,6 +288,8 @@ If you're running a system without prepackaged binary available, which means you
|
||||
- [nodist](https://github.com/marcelklehr/nodist)
|
||||
- [nvs](https://github.com/jasongin/nvs)
|
||||
|
||||
**Note:** When using Cygwin, install the `curl` package through [Cygwin's setup program](https://cygwin.com/install.html). Run `command -v curl` in your Cygwin terminal and ensure it points to Cygwin's `curl` (for example, `/usr/bin/curl`), rather than a Windows executable. A Windows version of `curl` may fail to write downloads to Cygwin paths (see [#2515](https://github.com/nvm-sh/nvm/issues/2515)).
|
||||
|
||||
**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
|
||||
@@ -384,13 +386,13 @@ nvm install node # "node" is an alias for the latest version
|
||||
To install a specific version of node:
|
||||
|
||||
```sh
|
||||
nvm install 14.7.0 # or 16.3.0, 12.22.1, etc
|
||||
nvm install 22.11.0 # or 24.0.0, etc
|
||||
```
|
||||
|
||||
To set an alias:
|
||||
|
||||
```sh
|
||||
nvm alias my_alias v14.4.0
|
||||
nvm alias my_alias v22.11.0
|
||||
```
|
||||
Make sure that your alias does not contain any spaces or slashes.
|
||||
|
||||
@@ -402,6 +404,13 @@ You can list available versions using `ls-remote`:
|
||||
nvm ls-remote
|
||||
```
|
||||
|
||||
To hide versions below a given version, use `--min=`, or set `NVM_MIN` to the same kind of version, for example in your shell profile; `--min=` overrides it. `nvm install` and `nvm version-remote` accept it too, and fail rather than select a version below it:
|
||||
|
||||
```sh
|
||||
nvm ls-remote --min=20
|
||||
nvm ls-remote --only=lts --min=v18.2
|
||||
```
|
||||
|
||||
When no version or `--lts` filter is given, the list also marks the version the `node` alias would install, and every version that one of your own aliases would resolve to. Each kind of annotation always starts in the same column, so the output can be sliced with `cut -c` or similar tools:
|
||||
|
||||
```
|
||||
@@ -426,16 +435,16 @@ nvm run node --version
|
||||
Or, you can run any arbitrary command in a subshell with the desired version of node:
|
||||
|
||||
```sh
|
||||
nvm exec 4.2 node --version
|
||||
nvm exec 22.11 node --version
|
||||
```
|
||||
|
||||
You can also get the path to the executable to where it was installed:
|
||||
|
||||
```sh
|
||||
nvm which 12.22
|
||||
nvm which 22.11
|
||||
```
|
||||
|
||||
In place of a version pointer like "14.7" or "16.3" or "12.22.1", you can use the following special default aliases with `nvm install`, `nvm use`, `nvm run`, `nvm exec`, `nvm which`, etc:
|
||||
In place of a version pointer like "22" or "24.0" or "22.11.0", you can use the following special default aliases with `nvm install`, `nvm use`, `nvm run`, `nvm exec`, `nvm which`, etc:
|
||||
|
||||
- `node`: this installs the latest version of [`node`](https://nodejs.org/en/)
|
||||
- `iojs`: this installs the latest version of [`io.js`](https://iojs.org/en/)
|
||||
@@ -535,12 +544,12 @@ This reinstalls all global packages from `22.20.0` into the currently-active ver
|
||||
If you've previously downloaded a node version (or it's still in the cache), you can install it without any network access using the `--offline` flag:
|
||||
|
||||
```sh
|
||||
nvm install --offline 14.7.0
|
||||
nvm install --offline 22.11.0
|
||||
```
|
||||
|
||||
This resolves versions using only locally installed versions and cached downloads. It will not attempt to download anything. This is useful in air-gapped environments, on planes, or when you want to avoid network latency.
|
||||
|
||||
A download is only added to the cache (`$NVM_DIR/.cache`) once its checksum has been verified (unless no checksum tool is available, in which case `nvm` warns that it is continuing without verification). `--offline` does not verify cached archives again, so anything placed in the cache by other means is trusted as-is.
|
||||
A download is only added to the cache (`$NVM_DIR/.cache`) once its checksum has been verified. `--offline` does not verify cached archives again, so anything placed in the cache by other means is trusted as-is.
|
||||
|
||||
You can combine `--offline` with `--lts` to install the latest cached LTS version (as long as LTS aliases have been populated by a prior `nvm ls-remote --lts`):
|
||||
|
||||
@@ -659,10 +668,10 @@ To set a default Node version to be used in any new shell, use the alias 'defaul
|
||||
|
||||
```sh
|
||||
nvm alias default node # this refers to the latest installed version of node
|
||||
nvm alias default 18 # this refers to the latest installed v18.x version of node
|
||||
nvm alias default 18.12 # this refers to the latest installed v18.12.x version of node
|
||||
nvm alias default 22 # this refers to the latest installed v22.x version of node
|
||||
nvm alias default 22.11 # this refers to the latest installed v22.11.x version of node
|
||||
nvm alias default --lts node # this refers to the latest installed LTS version of node
|
||||
nvm alias default --lts=iron 20 # this refers to the latest installed v20.x version of node from the "iron" LTS line
|
||||
nvm alias default --lts=jod 22 # this refers to the latest installed v22.x version of node from the "jod" LTS line
|
||||
```
|
||||
|
||||
nvm records the LTS status of every version each time it downloads the list of available versions, so `--lts` aliases know which installed versions are LTS.
|
||||
@@ -674,7 +683,7 @@ To use a mirror of the node binaries, set `$NVM_NODEJS_ORG_MIRROR`:
|
||||
export NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
|
||||
nvm install node
|
||||
|
||||
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist nvm install 4.2
|
||||
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist nvm install 22.11
|
||||
```
|
||||
|
||||
To use a mirror of the io.js binaries, set `$NVM_IOJS_ORG_MIRROR`:
|
||||
@@ -703,7 +712,7 @@ The header is only sent with requests for URLs under a mirror, never to any othe
|
||||
|
||||
nvm gives the header to `curl` and to GNU Wget in a config file (for GNU Wget, a private temporary file that also holds a copy of your `~/.wgetrc`), not as a command-line argument, which other users of the same machine could read. Other `wget`s, like BusyBox's, read no config file, so on a machine shared with other users, install `curl` before using `$NVM_AUTH_HEADER`.
|
||||
|
||||
`$NVM_AUTH_HEADER` is the recommended way to authenticate to a mirror. Credentials embedded in the mirror URL itself (`https://user:password@host/...`) also work, and nvm prints such URLs with the credentials masked as `***@`.
|
||||
`$NVM_AUTH_HEADER` is the recommended way to authenticate to a mirror. Credentials embedded in the mirror URL itself (`https://user:password@host/...`) also work, and nvm prints such URLs with the credentials masked as `***@`. Like the header, such a URL is given to `curl` in a config and to GNU Wget on its standard input, not as a command-line argument; other `wget`s, like BusyBox's, only take a URL as an argument, so on a machine shared with other users, install `curl` before putting credentials in a mirror URL.
|
||||
|
||||
### Platforms without official binaries
|
||||
|
||||
@@ -735,10 +744,10 @@ Neither host is gapless, and both change over time; the `index.tab` at the root
|
||||
You can create a `.nvmrc` file containing a node version number (or any other string that `nvm` understands; see `nvm --help` for details) in the project root directory (or any parent directory).
|
||||
Afterwards, `nvm use`, `nvm install`, and `nvm which` will use the version specified in the `.nvmrc` file if no version is supplied on the command line; if no `.nvmrc` is found either, they exit with status `127`. (`nvm exec` and `nvm run` follow the same `.nvmrc` lookup, but currently fall back to the active node if neither resolves — treat that fallback as undefined behavior; pass an explicit version if you need predictable scripting.) If you want the currently active version, pass `current` explicitly (e.g. `nvm which current`) — `current` is not affected by `.nvmrc`.
|
||||
|
||||
For example, to make nvm default to the latest 5.9 release, the latest LTS version, or the latest node version for the current directory:
|
||||
For example, to make nvm default to the latest 22.11 release, the latest LTS version, or the latest node version for the current directory:
|
||||
|
||||
```sh
|
||||
$ echo "5.9" > .nvmrc
|
||||
$ echo "22.11" > .nvmrc
|
||||
|
||||
$ echo "lts/*" > .nvmrc # to default to the latest LTS version
|
||||
|
||||
@@ -751,21 +760,21 @@ Then when you run nvm use:
|
||||
|
||||
```sh
|
||||
$ nvm use
|
||||
Found '/path/to/project/.nvmrc' with version <5.9>
|
||||
Now using node v5.9.1 (npm v3.7.3)
|
||||
Found '/path/to/project/.nvmrc' with version <22.11>
|
||||
Now using node v22.11.0 (npm v10.9.0)
|
||||
```
|
||||
|
||||
Running nvm install will also switch over to the correct version, but if the correct node version isn't already installed, it will install it for you.
|
||||
|
||||
```sh
|
||||
$ nvm install
|
||||
Found '/path/to/project/.nvmrc' with version <5.9>
|
||||
Downloading and installing node v5.9.1...
|
||||
Downloading https://nodejs.org/dist/v5.9.1/node-v5.9.1-linux-x64.tar.xz...
|
||||
Found '/path/to/project/.nvmrc' with version <22.11>
|
||||
Downloading and installing node v22.11.0...
|
||||
Downloading https://nodejs.org/dist/v22.11.0/node-v22.11.0-linux-x64.tar.xz...
|
||||
#################################################################################### 100.0%
|
||||
Computing checksum with sha256sum
|
||||
Checksums matched!
|
||||
Now using node v5.9.1 (npm v3.7.3)
|
||||
Now using node v22.11.0 (npm v10.9.0)
|
||||
```
|
||||
|
||||
`nvm use` et. al. will traverse directory structure upwards from the current directory looking for the `.nvmrc` file. In other words, running `nvm use` et. al. in any subdirectory of a directory with an `.nvmrc` will result in that `.nvmrc` being utilized.
|
||||
@@ -853,12 +862,19 @@ Put this into your `$HOME/.zshrc` to call `nvm use` automatically whenever you e
|
||||
autoload -U add-zsh-hook
|
||||
|
||||
load-nvmrc() {
|
||||
# skip during tab completion, which can run `cd` in a subshell
|
||||
(( $+compstate )) && return
|
||||
local nvmrc_path
|
||||
nvmrc_path="$(nvm_find_nvmrc)"
|
||||
|
||||
if [ -n "$nvmrc_path" ]; then
|
||||
local nvmrc_version
|
||||
# Let nvm parse `.nvmrc`, so that comments are ignored and the content of an invalid
|
||||
# or symlinked `.nvmrc` is not passed along to other commands
|
||||
nvmrc_version="$(nvm_process_nvmrc "${nvmrc_path}")" || return
|
||||
|
||||
local nvmrc_node_version
|
||||
nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
|
||||
nvmrc_node_version=$(nvm version "${nvmrc_version}")
|
||||
|
||||
if [ "$nvmrc_node_version" = "N/A" ]; then
|
||||
nvm install
|
||||
@@ -897,9 +913,15 @@ function load_nvm --on-variable="PWD"
|
||||
set -l node_version (nvm version)
|
||||
set -l nvmrc_path (nvm_find_nvmrc)
|
||||
if test -n "$nvmrc_path"
|
||||
set -l nvmrc_node_version (nvm version (cat $nvmrc_path))
|
||||
# Let nvm parse `.nvmrc`, so that comments are ignored and the content of an invalid
|
||||
# or symlinked `.nvmrc` is not passed along to other commands.
|
||||
# bass runs its arguments as bash code, so pass the path and version in the environment.
|
||||
set -lx NVMRC_PATH $nvmrc_path
|
||||
set -lx NVMRC_VERSION (bass source ~/.nvm/nvm.sh --no-use ';' nvm_process_nvmrc '"$NVMRC_PATH"')
|
||||
or return
|
||||
set -l nvmrc_node_version (nvm version '"$NVMRC_VERSION"')
|
||||
if test "$nvmrc_node_version" = "N/A"
|
||||
nvm install (cat $nvmrc_path)
|
||||
nvm install '"$NVMRC_VERSION"'
|
||||
else if test "$nvmrc_node_version" != "$node_version"
|
||||
nvm use $nvmrc_node_version
|
||||
end
|
||||
@@ -1094,29 +1116,17 @@ export NVM_DIR="$HOME/.nvm"
|
||||
To make development and testing work easier we supply a Dockerfile for development usage. It's based on an Ubuntu base image prepared with essential and useful tools for `nvm` development. To build the docker image of the environment, do a Docker build at the root of `nvm` repository:
|
||||
|
||||
```sh
|
||||
$ docker build -t nvm-dev .
|
||||
docker build -t nvm-dev . # Create the nvm-dev image
|
||||
docker images # Confirm nvm-dev appears in the list.
|
||||
|
||||
# Create a container named `nvm-dev` (with the same hostname)
|
||||
# and enter an interactive session in it.
|
||||
docker run --name nvm-dev -h nvm-dev -it nvm-dev
|
||||
```
|
||||
|
||||
This will package your current nvm working copy with our pre-defined development environment into a Docker image named `nvm-dev`. After the build you should see it appear in the list of images:
|
||||
This will place you in the container in the `~/.nvm/` directory as the `nvm` user. When you exit the container will not be deleted (so as to avoid losing any work in it); you can re-enter the container with `docker start -i nvm-dev` or delete it permanently with `docker container rm -f nvm-dev`. (For more information on using Docker, see the [website][docker-www] and [documentation][docker-docs].)
|
||||
|
||||
```sh
|
||||
$ docker images
|
||||
|
||||
REPOSITORY TAG IMAGE ID CREATED SIZE
|
||||
nvm-dev latest 9ca4c57a97d8 7 days ago 650 MB
|
||||
```
|
||||
|
||||
To start and enter a container based on this image:
|
||||
|
||||
```sh
|
||||
$ docker run -h nvm-dev -it nvm-dev
|
||||
|
||||
nvm@nvm-dev:~/.nvm$
|
||||
```
|
||||
|
||||
It takes several minutes to build the image and the image size is about 650MB, so it's not suitable for production usage.
|
||||
|
||||
For more information and documentation about Docker, please refer to its [official website][docker-www] and [documentation][docker-docs]:
|
||||
Note that the build takes several minutes and well over half a gigabyte of disk space, so it's not suitable for production usage.
|
||||
|
||||
## Problems
|
||||
|
||||
|
||||
+27
-4
@@ -172,11 +172,33 @@ install_nvm_from_git() {
|
||||
fi
|
||||
|
||||
local fetch_error
|
||||
local NVM_REMOTE_NAME
|
||||
NVM_REMOTE_NAME='origin'
|
||||
if [ -d "$INSTALL_DIR/.git" ]; then
|
||||
# Updating repo
|
||||
nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"
|
||||
command printf '\r=> '
|
||||
fetch_error="Failed to update nvm with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."
|
||||
local NVM_REMOTE
|
||||
local NVM_GIT_SOURCE
|
||||
NVM_GIT_SOURCE="$(nvm_source git)"
|
||||
NVM_REMOTE_NAME=''
|
||||
for NVM_REMOTE in $(command git --git-dir="${INSTALL_DIR}/.git" remote); do
|
||||
if [ "$(command git --git-dir="${INSTALL_DIR}/.git" config --get "remote.${NVM_REMOTE}.url")" = "${NVM_GIT_SOURCE}" ]; then
|
||||
NVM_REMOTE_NAME="${NVM_REMOTE}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ -z "${NVM_REMOTE_NAME}" ]; then
|
||||
NVM_REMOTE_NAME='nvm-origin'
|
||||
local NVM_REMOTE_SUFFIX
|
||||
NVM_REMOTE_SUFFIX=0
|
||||
while command git --git-dir="${INSTALL_DIR}/.git" config --get "remote.${NVM_REMOTE_NAME}.url" >/dev/null; do
|
||||
NVM_REMOTE_SUFFIX=$((NVM_REMOTE_SUFFIX + 1))
|
||||
NVM_REMOTE_NAME="nvm-origin-${NVM_REMOTE_SUFFIX}"
|
||||
done
|
||||
command git --git-dir="${INSTALL_DIR}/.git" remote add "${NVM_REMOTE_NAME}" "${NVM_GIT_SOURCE}" || return 2
|
||||
fi
|
||||
else
|
||||
fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"
|
||||
nvm_echo "=> Downloading nvm from git to '$INSTALL_DIR'"
|
||||
@@ -205,10 +227,10 @@ install_nvm_from_git() {
|
||||
fi
|
||||
fi
|
||||
# Try to fetch tag
|
||||
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$NVM_VERSION" --depth=1 2>/dev/null; then
|
||||
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch "${NVM_REMOTE_NAME}" tag "$NVM_VERSION" --depth=1 2>/dev/null; then
|
||||
:
|
||||
# Fetch given version
|
||||
elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin "$NVM_VERSION" --depth=1; then
|
||||
elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch "${NVM_REMOTE_NAME}" "$NVM_VERSION" --depth=1; then
|
||||
nvm_echo >&2 "$fetch_error"
|
||||
exit 1
|
||||
fi
|
||||
@@ -403,14 +425,15 @@ nvm_check_global_modules() {
|
||||
*/*) nvm_is_wsl && [ -f "${NPM_COMMAND%/*}/npm.cmd" ] && return 0 ;;
|
||||
esac
|
||||
|
||||
# run npm from `/`, so it does not apply the npmrc of the (untrusted) project that `$PWD` is in
|
||||
local NPM_VERSION
|
||||
NPM_VERSION="$(npm --version)"
|
||||
NPM_VERSION="$(command cd / && npm --version)"
|
||||
NPM_VERSION="${NPM_VERSION:--1}"
|
||||
[ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0
|
||||
|
||||
local NPM_GLOBAL_MODULES
|
||||
NPM_GLOBAL_MODULES="$(
|
||||
npm list -g --depth=0 |
|
||||
(command cd / && npm list -g --depth=0) |
|
||||
command sed -e '/ npm@/d' -e '/ (empty)$/d'
|
||||
)"
|
||||
|
||||
|
||||
@@ -47,6 +47,14 @@ nvm alias work --lts=jod 22 >/dev/null 2>&1 || die 'nvm alias work --lts=jod 22
|
||||
nvm alias other --lts=iron 22 >/dev/null 2>&1
|
||||
[ "_$(nvm version other)" = '_N/A' ] || die "no installed v22 is iron; got >$(nvm version other)<"
|
||||
|
||||
# an LTS name matches every codename containing it, but not as a regex
|
||||
nvm alias some --lts=o node >/dev/null 2>&1 || die 'nvm alias some --lts=o node failed'
|
||||
[ "_$(nvm version some)" = '_v24.11.0' ] || die "some should be the latest installed version whose codename contains o; got >$(nvm version some)<"
|
||||
nvm alias some --lts=ir node >/dev/null 2>&1 || die 'nvm alias some --lts=ir node failed'
|
||||
[ "_$(nvm version some)" = '_v20.19.0' ] || die "some should be the latest installed version whose codename contains ir; got >$(nvm version some)<"
|
||||
nvm alias some --lts=. node >/dev/null 2>&1 || die 'nvm alias some --lts=. node failed'
|
||||
[ "_$(nvm version some)" = '_N/A' ] || die "no codename contains a literal .; got >$(nvm version some)<"
|
||||
|
||||
OUTPUT="$(nvm unalias work)"
|
||||
[ "_${OUTPUT}" = '_Deleted alias work - restore it with `nvm alias "work" --lts=jod "22"`' ] || die "the restore hint should keep --lts=jod as its own argument; got >${OUTPUT}<"
|
||||
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#!/bin/sh
|
||||
|
||||
ORIG_NVM_DIR="${NVM_DIR}"
|
||||
TMP_DIR=''
|
||||
|
||||
cleanup () {
|
||||
case "${TMP_DIR}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
NVM_DIR="${ORIG_NVM_DIR}"
|
||||
export NVM_DIR
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../nvm.sh
|
||||
|
||||
\. ../common.sh
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
NVM_DIR="${TMP_DIR}/nvm"
|
||||
export NVM_DIR
|
||||
mkdir -p "${NVM_DIR}/alias/lts" || die 'could not create alias dir'
|
||||
# `nvm exec` runs `$NVM_DIR/nvm-exec`, which sources the `nvm.sh` beside it
|
||||
ln -s "${PWD}/../../nvm.sh" "${PWD}/../../nvm-exec" "${NVM_DIR}/" || die 'could not link nvm.sh and nvm-exec'
|
||||
|
||||
for VERSION in v20.19.0 v22.11.0 v24.11.0; do
|
||||
make_fake_node "${VERSION}" || die "fake node ${VERSION} could not be made"
|
||||
done
|
||||
row () {
|
||||
printf '%s\t2024-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2"
|
||||
}
|
||||
{ row v26.12.1 -; row v24.11.0 Krypton; row v24.0.0 -; row v22.11.0 Jod; row v20.19.0 Iron; } | nvm_record_lts_status || die 'could not record LTS status'
|
||||
nvm_make_alias lts/iron v20.19.0 >/dev/null 2>&1 || die 'could not alias lts/iron'
|
||||
nvm_make_alias lts/jod v22.11.0 >/dev/null 2>&1 || die 'could not alias lts/jod'
|
||||
nvm_make_alias lts/krypton v24.11.0 >/dev/null 2>&1 || die 'could not alias lts/krypton'
|
||||
nvm_make_alias 'lts/*' lts/krypton >/dev/null 2>&1 || die 'could not alias lts/*'
|
||||
|
||||
# "o" is in Iron, Jod and Krypton; Krypton is the newest
|
||||
nvm use --delete-prefix --lts=o >/dev/null 2>&1 || die '`nvm use --lts=o` failed'
|
||||
[ "_$(nvm current)" = '_v24.11.0' ] || die "\`nvm use --lts=o\` should use the newest codename containing it, krypton; got >$(nvm current)<"
|
||||
nvm use --delete-prefix --lts=ir >/dev/null 2>&1 || die '`nvm use --lts=ir` failed'
|
||||
[ "_$(nvm current)" = '_v20.19.0' ] || die "\`nvm use --lts=ir\` should use iron; got >$(nvm current)<"
|
||||
nvm use --delete-prefix --lts=jod >/dev/null 2>&1 || die '`nvm use --lts=jod` failed'
|
||||
[ "_$(nvm current)" = '_v22.11.0' ] || die "\`nvm use --lts=jod\` should use jod; got >$(nvm current)<"
|
||||
|
||||
# not a regex
|
||||
try_err nvm use --delete-prefix --lts=.
|
||||
[ "_${CAPTURED_EXIT_CODE}" != '_0' ] || die '`nvm use --lts=.` should fail'
|
||||
|
||||
OUTPUT="$(nvm exec --lts=ir node 2>/dev/null)" || die '`nvm exec --lts=ir node` failed'
|
||||
[ "_$(nvm_echo "${OUTPUT}" | command tail -1)" = '_v20.19.0' ] || die "\`nvm exec --lts=ir node\` should run iron; got >${OUTPUT}<"
|
||||
case "${OUTPUT}" in
|
||||
'Running node LTS "iron" -> v20.19.0'*) ;;
|
||||
*) die "\`nvm exec --lts=ir\` should name the codename it picked; got >${OUTPUT}<" ;;
|
||||
esac
|
||||
|
||||
OUTPUT="$(nvm run --silent --lts=kr 2>/dev/null)" || die '`nvm run --lts=kr` failed'
|
||||
[ "_$(nvm_echo "${OUTPUT}" | command tail -1)" = '_v24.11.0' ] || die "\`nvm run --lts=kr\` should run krypton; got >${OUTPUT}<"
|
||||
|
||||
nvm uninstall --lts=ir >/dev/null 2>&1 || die '`nvm uninstall --lts=ir` failed'
|
||||
! nvm_is_version_installed v20.19.0 || die '`nvm uninstall --lts=ir` should uninstall iron'
|
||||
nvm_is_version_installed v22.11.0 || die '`nvm uninstall --lts=ir` should keep jod'
|
||||
|
||||
cleanup
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/bin/sh
|
||||
|
||||
TMP_DIR=''
|
||||
|
||||
cleanup () {
|
||||
case "${TMP_DIR}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
mkdir -p "${TMP_DIR}/project" || die 'could not make the project dir'
|
||||
echo '{}' > "${TMP_DIR}/project/package.json"
|
||||
echo "logs-dir=${TMP_DIR}/logs" > "${TMP_DIR}/project/.npmrc"
|
||||
|
||||
NPM_CALLS="${TMP_DIR}/npm-calls"
|
||||
npm() {
|
||||
echo "$(pwd): $*" >> "${NPM_CALLS}"
|
||||
echo "npm $*"
|
||||
}
|
||||
|
||||
OUTPUT="$(cd "${TMP_DIR}/project" && nvm debug 2>&1)"
|
||||
|
||||
EXPECTED_CALLS="/: config get prefix
|
||||
/: root -g"
|
||||
CALLS="$(cat "${NPM_CALLS}")"
|
||||
[ "_${CALLS}" = "_${EXPECTED_CALLS}" ] || die "nvm debug should run npm outside the project: expected >${EXPECTED_CALLS}<; got >${CALLS}<"
|
||||
|
||||
case "${OUTPUT}" in
|
||||
*"npm config get prefix: npm config get prefix"*"npm root -g: npm root -g"*) ;;
|
||||
*) die "nvm debug should print npm's prefix and global root; got >${OUTPUT}<" ;;
|
||||
esac
|
||||
|
||||
cleanup
|
||||
@@ -72,6 +72,32 @@ nvm install -s --lts --default "${version}" > /dev/null 2>&1 || die "\`nvm insta
|
||||
OUTPUT="$(nvm_alias default)"
|
||||
[ "${OUTPUT}" = "${version}" ] || die "expected the default alias to be ${version} for \`--lts ${version}\`, got >${OUTPUT}<"
|
||||
|
||||
# `--lts=<name>` aliases the newest codename containing the name
|
||||
reset
|
||||
printf 'v22.0.0 jod\n%s iron\nv18.0.0 hydrogen\n' "${version}" > "$(nvm_lts_status_path)"
|
||||
nvm install -s --lts=ir --default > /dev/null 2>&1 || die '`nvm install -s --lts=ir --default` failed'
|
||||
OUTPUT="$(nvm_alias default)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the default alias to be lts/iron after \`--lts=ir\`, got >${OUTPUT}<"
|
||||
|
||||
# again, now that the version is installed
|
||||
nvm alias default v18.0.0 > /dev/null 2>&1
|
||||
nvm install -s --lts=ir --default > /dev/null 2>&1 || die '`nvm install -s --lts=ir --default` failed for an installed version'
|
||||
OUTPUT="$(nvm_alias default)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the default alias to be lts/iron for an installed version, got >${OUTPUT}<"
|
||||
nvm install -s --lts=ir --alias=foo > /dev/null 2>&1 || die '`nvm install -s --lts=ir --alias=foo` failed for an installed version'
|
||||
OUTPUT="$(nvm_alias foo)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the foo alias to be lts/iron for an installed version, got >${OUTPUT}<"
|
||||
|
||||
reset
|
||||
nvm install -s --lts=ir --alias=foo > /dev/null 2>&1 || die '`nvm install -s --lts=ir --alias=foo` failed'
|
||||
OUTPUT="$(nvm_alias foo)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the foo alias to be lts/iron after \`--lts=ir\`, got >${OUTPUT}<"
|
||||
|
||||
# offline, `--lts=<name>` finds the installed version of that codename
|
||||
nvm_echo "${version}" > "$(nvm_alias_path)/lts/iron"
|
||||
OUTPUT="$(nvm_remote_version() { return 3; }; nvm install --offline --lts=ir 2>&1)" || die "\`nvm install --offline --lts=ir\` failed: >${OUTPUT}<"
|
||||
rm -f "$(nvm_lts_status_path)"
|
||||
|
||||
reset
|
||||
nvm install --save "${version}" > /dev/null 2>&1 || die "\`nvm install --save ${version}\` failed"
|
||||
OUTPUT="$(cat .nvmrc 2> /dev/null)"
|
||||
|
||||
Executable
+183
@@ -0,0 +1,183 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
rm -rf "${TEST_NVM_DIR-}"
|
||||
unset -f nvm_download nvm_record_lts_status nvm_get_remote_aliases nvm_has_colors versions install_below_min
|
||||
unset TEST_NVM_DIR OUTPUT EXIT_CODE NVM_MIN NVM_MIN_ARG NVM_MIN_COMMAND
|
||||
}
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
unset NVM_MIN NVM_ONLY
|
||||
|
||||
TEST_NVM_DIR="$(mktemp -d)" || die 'could not create a temporary NVM_DIR'
|
||||
NVM_DIR="${TEST_NVM_DIR}"
|
||||
|
||||
nvm_has_colors() { return 1; }
|
||||
nvm_record_lts_status() { :; }
|
||||
nvm_get_remote_aliases() { :; }
|
||||
|
||||
nvm_download() {
|
||||
case "$*" in
|
||||
*"$(nvm_get_mirror node std)/index.tab"*)
|
||||
printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\tlts\tsecurity\n'
|
||||
printf '%s\t2026-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' \
|
||||
v27.0.0-alpha.1.0.0 - \
|
||||
v26.1.0 - \
|
||||
v24.2.0 Krypton \
|
||||
v22.12.0 Jod \
|
||||
v20.18.1 Iron \
|
||||
v20.0.0 - \
|
||||
v18.20.5 Hydrogen \
|
||||
v0.12.18 - \
|
||||
v0.10.48 -
|
||||
;;
|
||||
*"$(nvm_get_mirror iojs std)/index.tab"*)
|
||||
printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\n'
|
||||
printf '%s\t2015-01-01\tsrc\t-\t-\t-\t-\t-\t-\n' v3.3.1 v2.5.0 v1.8.4
|
||||
;;
|
||||
*) return 42 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# prints only the version column, on one line
|
||||
versions() {
|
||||
nvm ls-remote --no-colors "$@" 2>/dev/null | command awk '{ printf "%s ", $1 }'
|
||||
}
|
||||
|
||||
[ "$(versions)" = 'v0.10.48 v0.12.18 iojs-v1.8.4 iojs-v2.5.0 iojs-v3.3.1 v18.20.5 v20.0.0 v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|
||||
|| die "without a minimum, expected every non-alpha version; got >$(versions)<"
|
||||
|
||||
[ "$(versions --min=20)" = 'v20.0.0 v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|
||||
|| die "--min=20: got >$(versions --min=20)<"
|
||||
[ "$(versions --min=v20.1)" = 'v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|
||||
|| die "--min=v20.1: got >$(versions --min=v20.1)<"
|
||||
[ "$(versions --min=20.18.1)" = 'v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|
||||
|| die "--min=20.18.1: got >$(versions --min=20.18.1)<"
|
||||
|
||||
# io.js majors are compared like node majors
|
||||
[ "$(versions --min=2)" = 'iojs-v2.5.0 iojs-v3.3.1 v18.20.5 v20.0.0 v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|
||||
|| die "--min=2: got >$(versions --min=2)<"
|
||||
|
||||
# combined with --only and --lts
|
||||
[ "$(versions --only=lts --min=20)" = 'v20.18.1 v22.12.0 v24.2.0 ' ] \
|
||||
|| die "--only=lts --min=20: got >$(versions --only=lts --min=20)<"
|
||||
[ "$(versions --min=20 --only=non-lts)" = 'v20.0.0 v26.1.0 ' ] \
|
||||
|| die "--min=20 --only=non-lts: got >$(versions --min=20 --only=non-lts)<"
|
||||
[ "$(versions --only=iojs --min=3)" = 'iojs-v3.3.1 ' ] \
|
||||
|| die "--only=iojs --min=3: got >$(versions --only=iojs --min=3)<"
|
||||
[ "$(versions --only=alpha,lts --min=24)" = 'v24.2.0 v27.0.0-alpha.1.0.0 ' ] \
|
||||
|| die "--only=alpha,lts --min=24: got >$(versions --only=alpha,lts --min=24)<"
|
||||
[ "$(versions --lts --min=22)" = 'v22.12.0 v24.2.0 ' ] \
|
||||
|| die "--lts --min=22: got >$(versions --lts --min=22)<"
|
||||
|
||||
# a partial minimum includes the prereleases of its first release; a full one is above them
|
||||
[ "$(versions --only=alpha --min=27)" = 'v27.0.0-alpha.1.0.0 ' ] \
|
||||
|| die "--only=alpha --min=27: got >$(versions --only=alpha --min=27)<"
|
||||
OUTPUT="$(nvm ls-remote --no-colors --only=alpha --min=27.0.0)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|
||||
|| die "--only=alpha --min=27.0.0 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
|
||||
|
||||
# nothing at or above the minimum, or nothing matching the pattern, is N/A
|
||||
OUTPUT="$(nvm ls-remote --no-colors --min=99)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|
||||
|| die "--min=99 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
|
||||
OUTPUT="$(nvm ls-remote --no-colors --only=lts --min=25)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|
||||
|| die "--only=lts --min=25 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
|
||||
OUTPUT="$(nvm ls-remote --no-colors --min=18 99)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|
||||
|| die "a pattern with no match should print N/A and exit 3 with --min; got >${OUTPUT}<, exit ${EXIT_CODE}"
|
||||
|
||||
# NVM_MIN is the default, and --min= overrides it
|
||||
NVM_MIN=22
|
||||
[ "$(versions)" = 'v22.12.0 v24.2.0 v26.1.0 ' ] \
|
||||
|| die "NVM_MIN=22: got >$(versions)<"
|
||||
[ "$(versions --min=26)" = 'v26.1.0 ' ] \
|
||||
|| die "NVM_MIN=22 --min=26: got >$(versions --min=26)<"
|
||||
nvm ls-remote --no-colors >/dev/null 2>"${TEST_NVM_DIR}/stderr"
|
||||
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
|
||||
[ "_${OUTPUT}" = '_8 versions below 22 are hidden by `$NVM_MIN`.' ] \
|
||||
|| die "NVM_MIN=22 notice: got >${OUTPUT}<"
|
||||
OUTPUT="$(nvm version-remote 20)"
|
||||
[ "_${OUTPUT}" = '_N/A' ] || die "NVM_MIN=22 version-remote 20: got >${OUTPUT}<"
|
||||
[ "$(nvm version-remote --min=20 20)" = 'v20.18.1' ] \
|
||||
|| die "NVM_MIN=22 version-remote --min=20 20: got >$(nvm version-remote --min=20 20)<"
|
||||
|
||||
# a notice on stderr says how many versions are hidden, and by what
|
||||
nvm ls-remote --no-colors --min=20 >/dev/null 2>"${TEST_NVM_DIR}/stderr"
|
||||
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
|
||||
[ "_${OUTPUT}" = '_6 versions below 20 are hidden by `--min=20`.' ] \
|
||||
|| die "--min=20 notice: got >${OUTPUT}<"
|
||||
nvm ls-remote --no-colors --only=lts --min=20 >/dev/null 2>"${TEST_NVM_DIR}/stderr"
|
||||
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
|
||||
[ "_${OUTPUT}" = '_1 version below 20 is hidden by `--min=20`.' ] \
|
||||
|| die "--only=lts --min=20 notice: got >${OUTPUT}<"
|
||||
nvm ls-remote --no-colors --min=0 >/dev/null 2>"${TEST_NVM_DIR}/stderr"
|
||||
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
|
||||
[ -z "${OUTPUT}" ] || die "--min=0 hides nothing, so should print no notice; got >${OUTPUT}<"
|
||||
|
||||
# version-remote and install select only from versions at or above the minimum
|
||||
[ "$(nvm version-remote --min=20 20)" = 'v20.18.1' ] \
|
||||
|| die "version-remote --min=20 20: got >$(nvm version-remote --min=20 20)<"
|
||||
OUTPUT="$(nvm version-remote --min=22 20)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] && [ "_${OUTPUT}" = '_N/A' ] \
|
||||
|| die "version-remote --min=22 20 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
|
||||
OUTPUT="$(nvm version-remote --lts=iron --min=22)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] && [ "_${OUTPUT}" = '_N/A' ] \
|
||||
|| die "version-remote --lts=iron --min=22 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
|
||||
install_below_min() {
|
||||
OUTPUT="$(nvm install "$@" 2>&1)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 3 ] || die "install $* should exit 3; got ${EXIT_CODE}: >${OUTPUT}<"
|
||||
case "${OUTPUT}" in
|
||||
*'It matches v20.18.1, which is below the minimum version 22 from `--min=22`.'*) ;;
|
||||
*) die "install $* should explain the minimum; got >${OUTPUT}<" ;;
|
||||
esac
|
||||
}
|
||||
install_below_min --min=22 20
|
||||
install_below_min 20 --min=22
|
||||
|
||||
# NVM_MIN only applies to commands that select remote versions
|
||||
mkdir -p "${NVM_DIR}/versions/node/v18.20.5/bin" || die 'could not create an installed version'
|
||||
OUTPUT="$(nvm ls --no-colors --no-alias | command awk '{ printf "%s ", $1 }')"
|
||||
case "${OUTPUT}" in
|
||||
*v18.20.5*) ;;
|
||||
*) die "NVM_MIN should not hide installed versions from nvm ls; got >${OUTPUT}<" ;;
|
||||
esac
|
||||
[ "${NVM_MIN}" = 22 ] || die "nvm ls changed NVM_MIN to >${NVM_MIN}<"
|
||||
|
||||
# the minimum must be a version, given with `=`
|
||||
for NVM_MIN_ARG in --min --min= --min=lts/iron --min=node --min=18.x --min=v18.1.0.0; do
|
||||
OUTPUT="$(nvm ls-remote --no-colors "${NVM_MIN_ARG}" 2>&1)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 55 ] || die "ls-remote ${NVM_MIN_ARG} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
|
||||
OUTPUT="$(nvm version-remote "${NVM_MIN_ARG}" 20 2>&1)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 55 ] || die "version-remote ${NVM_MIN_ARG} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
|
||||
OUTPUT="$(nvm install "${NVM_MIN_ARG}" 20 2>&1)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 55 ] || die "install ${NVM_MIN_ARG} 20 should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
|
||||
OUTPUT="$(nvm install 20 "${NVM_MIN_ARG}" 2>&1)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 55 ] || die "install 20 ${NVM_MIN_ARG} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
|
||||
done
|
||||
unset NVM_MIN_ARG
|
||||
NVM_MIN=node
|
||||
for NVM_MIN_COMMAND in ls-remote version-remote install; do
|
||||
OUTPUT="$(nvm "${NVM_MIN_COMMAND}" 2>&1)"
|
||||
EXIT_CODE=$?
|
||||
[ "${EXIT_CODE}" = 55 ] || die "NVM_MIN=node ${NVM_MIN_COMMAND} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
|
||||
done
|
||||
unset NVM_MIN_COMMAND
|
||||
|
||||
cleanup
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
#!/bin/sh
|
||||
|
||||
TMP_DIR=''
|
||||
|
||||
cleanup () {
|
||||
case "${TMP_DIR}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
mkdir -p "${TMP_DIR}/project" "${TMP_DIR}/bin" || die 'could not make the test dirs'
|
||||
echo '{}' > "${TMP_DIR}/project/package.json"
|
||||
echo 'registry=http://127.0.0.1:9/' > "${TMP_DIR}/project/.npmrc"
|
||||
|
||||
# a real executable, since `xargs` cannot run a shell function
|
||||
NPM_CALLS="${TMP_DIR}/npm-calls"
|
||||
cat > "${TMP_DIR}/bin/npm" <<FAKE
|
||||
#!/bin/sh
|
||||
echo "\$(pwd): \$*" >> '${NPM_CALLS}'
|
||||
echo '6.0.0'
|
||||
FAKE
|
||||
chmod +x "${TMP_DIR}/bin/npm" || die 'could not make the fake npm'
|
||||
PATH="${TMP_DIR}/bin:${PATH}"
|
||||
|
||||
check_calls () {
|
||||
CALLS="$(cat "${NPM_CALLS}")"
|
||||
[ "_${CALLS}" = "_$2" ] || die "$1 should run npm from \`/\`: expected >$2<; got >${CALLS}<"
|
||||
rm -f "${NPM_CALLS}"
|
||||
}
|
||||
|
||||
cd "${TMP_DIR}/project" || die 'could not cd to the project'
|
||||
PROJECT_PWD="$(pwd)"
|
||||
|
||||
if nvm_is_zsh; then
|
||||
# eg, a hook that runs `nvm use` on every directory change
|
||||
eval 'chpwd() { echo "chpwd: $(pwd)" >> "${NPM_CALLS}"; }'
|
||||
fi
|
||||
|
||||
nvm_npm config get registry >/dev/null
|
||||
check_calls nvm_npm '/: config get registry'
|
||||
|
||||
[ "_$(pwd)" = "_${PROJECT_PWD}" ] || die "nvm_npm should not change the caller's directory; got >$(pwd)<"
|
||||
|
||||
OUTPUT="$(nvm_print_npm_version)"
|
||||
[ "_${OUTPUT}" = '_ (npm v6.0.0)' ] || die "nvm_print_npm_version should print the npm version; got >${OUTPUT}<"
|
||||
check_calls nvm_print_npm_version '/: --version'
|
||||
|
||||
nvm_get_default_packages() { echo 'a b'; }
|
||||
nvm_install_default_packages >/dev/null
|
||||
check_calls nvm_install_default_packages '/: install -g --quiet a b'
|
||||
|
||||
nvm_ls_current() { echo 'v8.0.0'; }
|
||||
nvm_install_latest_npm >/dev/null
|
||||
check_calls nvm_install_latest_npm '/: --version
|
||||
/: install -g npm@6
|
||||
/: --version'
|
||||
|
||||
cleanup
|
||||
@@ -33,9 +33,9 @@ EXPECTED_OUTPUT='Provided checksum to compare to is empty.'
|
||||
|
||||
try_err nvm_compare_checksum ../../../nvm.sh checksum
|
||||
EXPECTED_OUTPUT="Computed checksum of '../../../nvm.sh' is empty.
|
||||
WARNING: Continuing *without checksum verification*"
|
||||
Refusing to continue *without checksum verification*"
|
||||
[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<"
|
||||
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected to exit with code 1, got ${CAPTURED_EXIT_CODE}"
|
||||
|
||||
nvm_compute_checksum() {
|
||||
echo "not checksum: ${1}"
|
||||
|
||||
Executable
+149
@@ -0,0 +1,149 @@
|
||||
#!/bin/sh
|
||||
|
||||
WORK="$PWD/nvm_download-url-credentials-work.$$"
|
||||
TEST_BIN="$WORK/bin"
|
||||
ARGV_LOG="$WORK/argv.log"
|
||||
STDIN_LOG="$WORK/stdin.log"
|
||||
OLDHOME="$HOME"
|
||||
|
||||
cleanup() {
|
||||
unset -f die cleanup nvm_has_executable
|
||||
unset NVM_AUTH_HEADER NVM_NODEJS_ORG_MIRROR WGET_IS_GNU FAKE_EXIT
|
||||
rm -rf "$WORK"
|
||||
export PATH="$OLDPATH"
|
||||
HOME="$OLDHOME"
|
||||
export HOME
|
||||
}
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
\. ../../../nvm.sh
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
OLDPATH="$PATH"
|
||||
|
||||
# fake curl: record every argument, and the config it reads from stdin
|
||||
make_fake_curl "$TEST_BIN" 'for a in "$@"; do printf "%s\n" "$a"; done > "$ARGV_LOG"
|
||||
: > "$STDIN_LOG"
|
||||
for a in "$@"; do if [ "$a" = --config ]; then cat > "$STDIN_LOG"; fi; done'
|
||||
VERSION_MESSAGE="curl 7.99.0 (fake)"
|
||||
|
||||
# fake wget: GNU Wget when $WGET_IS_GNU is set, and like BusyBox's otherwise;
|
||||
# record every argument, and the URLs it reads from stdin
|
||||
{
|
||||
echo '#!/bin/sh'
|
||||
echo 'if [ "$1" = --version ]; then'
|
||||
echo ' [ -n "${WGET_IS_GNU-}" ] || exit 1'
|
||||
echo ' echo "GNU Wget 1.21.2 built on linux-gnu."; exit 0'
|
||||
echo 'fi'
|
||||
echo 'for a in "$@"; do printf "%s\n" "$a"; done > "$ARGV_LOG"'
|
||||
echo ': > "$STDIN_LOG"'
|
||||
echo 'for a in "$@"; do if [ "$a" = --input-file=- ]; then cat > "$STDIN_LOG"; fi; done'
|
||||
echo 'exit "${FAKE_EXIT:-0}"'
|
||||
} > "$TEST_BIN/wget"
|
||||
chmod +x "$TEST_BIN/wget"
|
||||
|
||||
mkdir -p "$WORK/home"
|
||||
HOME="$WORK/home"
|
||||
export HOME ARGV_LOG STDIN_LOG
|
||||
export PATH="$TEST_BIN:$OLDPATH"
|
||||
|
||||
NVM_NODEJS_ORG_MIRROR='https://ci-bot:s3cr3t@mirror.example/dist'
|
||||
export NVM_NODEJS_ORG_MIRROR
|
||||
URL="${NVM_NODEJS_ORG_MIRROR}/index.tab"
|
||||
|
||||
assert_not_an_argument() {
|
||||
! grep -q 's3cr3t' "$ARGV_LOG" || die "${1}: the mirror password is an argument; got: $(cat "$ARGV_LOG")"
|
||||
}
|
||||
|
||||
# given curl
|
||||
nvm_download -L -s "$URL" -o - || die 'curl: nvm_download returned nonzero'
|
||||
assert_not_an_argument 'curl'
|
||||
EXPECTED='-q
|
||||
--fail
|
||||
-L
|
||||
-s
|
||||
-o
|
||||
-
|
||||
--config
|
||||
-'
|
||||
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "curl: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
|
||||
[ "$(cat "$STDIN_LOG")" = "url = \"${URL}\"" ] || die "curl: expected the URL in its config, got >$(cat "$STDIN_LOG")<"
|
||||
|
||||
# given a header as well, both go in the one config
|
||||
NVM_AUTH_HEADER='Bearer test-token'
|
||||
export NVM_AUTH_HEADER
|
||||
nvm_download -L -s "$URL" -o - || die 'curl with header: nvm_download returned nonzero'
|
||||
assert_not_an_argument 'curl with header'
|
||||
[ "$(grep -c -Fxe '--config' "$ARGV_LOG")" = 1 ] || die "curl with header: expected one --config, got >$(cat "$ARGV_LOG")<"
|
||||
EXPECTED="header = \"Authorization: Bearer test-token\"
|
||||
url = \"${URL}\""
|
||||
[ "$(cat "$STDIN_LOG")" = "$EXPECTED" ] || die "curl with header: expected config >$EXPECTED<, got >$(cat "$STDIN_LOG")<"
|
||||
unset NVM_AUTH_HEADER
|
||||
|
||||
# given a quote or a backslash, they are escaped within curl's quoted string
|
||||
nvm_download -L -s "${NVM_NODEJS_ORG_MIRROR}/a\"b\\c" -o - || die 'curl, quote: nvm_download returned nonzero'
|
||||
assert_not_an_argument 'curl, quote'
|
||||
[ "$(cat "$STDIN_LOG")" = "url = \"${NVM_NODEJS_ORG_MIRROR}/a\\\"b\\\\c\"" ] || die "curl, quote: got config >$(cat "$STDIN_LOG")<"
|
||||
|
||||
# given a line break, the URL cannot add a line to the config
|
||||
nvm_download -L -s "$(printf '%s\noutput = "/tmp/nvm-pwned' "$URL")" -o - || die 'curl, line break: nvm_download returned nonzero'
|
||||
! grep -Fxqe '--config' "$ARGV_LOG" || die "curl, line break: got config >$(cat "$STDIN_LOG")<"
|
||||
|
||||
# given a URL without credentials, it stays an argument
|
||||
nvm_download -L -s 'https://nodejs.org/dist/index.tab' -o - || die 'curl, no credentials: nvm_download returned nonzero'
|
||||
EXPECTED='-q
|
||||
--fail
|
||||
-L
|
||||
-s
|
||||
https://nodejs.org/dist/index.tab
|
||||
-o
|
||||
-'
|
||||
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "curl, no credentials: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
|
||||
|
||||
# given only GNU Wget
|
||||
nvm_has_executable() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }
|
||||
WGET_IS_GNU=1
|
||||
export WGET_IS_GNU
|
||||
nvm_download -L -C - --progress-bar "$URL" -o "$WORK/out" || die 'GNU Wget: nvm_download returned nonzero'
|
||||
assert_not_an_argument 'GNU Wget'
|
||||
EXPECTED="-c
|
||||
--progress=bar
|
||||
-O
|
||||
$WORK/out
|
||||
--input-file=-"
|
||||
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "GNU Wget: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
|
||||
[ "$(cat "$STDIN_LOG")" = "$URL" ] || die "GNU Wget: expected the URL on stdin, got >$(cat "$STDIN_LOG")<"
|
||||
|
||||
# and its exit code is kept
|
||||
FAKE_EXIT=8
|
||||
export FAKE_EXIT
|
||||
nvm_download -L -s "$URL" -o -
|
||||
EXIT_CODE=$?
|
||||
unset FAKE_EXIT
|
||||
[ "$EXIT_CODE" = 8 ] || die "GNU Wget: expected exit code 8, got $EXIT_CODE"
|
||||
|
||||
# given a header as well, which goes through the same-origin redirect check
|
||||
NVM_AUTH_HEADER='Bearer test-token'
|
||||
export NVM_AUTH_HEADER
|
||||
nvm_download -L -s "$URL" -o - || die 'GNU Wget with header: nvm_download returned nonzero'
|
||||
assert_not_an_argument 'GNU Wget with header'
|
||||
EXPECTED='--max-redirect=0
|
||||
-q
|
||||
-O
|
||||
-
|
||||
--input-file=-'
|
||||
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "GNU Wget with header: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
|
||||
[ "$(cat "$STDIN_LOG")" = "$URL" ] || die "GNU Wget with header: expected the URL on stdin, got >$(cat "$STDIN_LOG")<"
|
||||
unset NVM_AUTH_HEADER
|
||||
|
||||
# given a wget that reads no URL from stdin, like BusyBox's, the URL stays an argument
|
||||
unset WGET_IS_GNU
|
||||
nvm_download -L -s "$URL" -o - || die 'other wget: nvm_download returned nonzero'
|
||||
EXPECTED="-q
|
||||
${URL}
|
||||
-O
|
||||
-"
|
||||
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "other wget: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
|
||||
|
||||
cleanup
|
||||
@@ -4,7 +4,7 @@ WORK="${TMPDIR:-/tmp}/nvm_download_artifact_cache.$$"
|
||||
|
||||
cleanup () {
|
||||
command rm -rf "${WORK}"
|
||||
unset -f cleanup die download_offline nvm_get_mirror nvm_binary_available nvm_get_download_slug nvm_get_artifact_compression nvm_cache_dir nvm_get_checksum nvm_version_greater_than_or_equal_to nvm_download nvm_compare_checksum
|
||||
unset -f cleanup die download_offline nvm_get_mirror nvm_binary_available nvm_get_download_slug nvm_get_artifact_compression nvm_cache_dir nvm_get_checksum nvm_version_greater_than_or_equal_to nvm_download nvm_compute_checksum nvm_compare_checksum nvm_get_checksum_binary
|
||||
}
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
@@ -57,6 +57,22 @@ try download_offline
|
||||
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "empty checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"
|
||||
nvm_get_checksum() { nvm_echo 'fake_checksum'; }
|
||||
|
||||
# a download whose checksum cannot be computed has not been verified, so it must not be accepted either
|
||||
# (this still uses the real `nvm_compare_checksum`, so it runs before that is mocked)
|
||||
nvm_compute_checksum() { nvm_echo ''; }
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "unverifiable checksum: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
|
||||
[ ! -e "${TEST_CACHE_DIR}" ] || die "unverifiable checksum: expected ${TEST_CACHE_DIR} to be removed"
|
||||
try download_offline
|
||||
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "unverifiable checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"
|
||||
|
||||
# nor may a cached archive whose checksum cannot be computed be used
|
||||
command mkdir -p "${TEST_CACHE_DIR}"
|
||||
nvm_echo 'cached' > "${TEST_TARBALL}"
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "unverifiable cached archive: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
|
||||
[ ! -e "${TEST_CACHE_DIR}" ] || die "unverifiable cached archive: expected ${TEST_CACHE_DIR} to be removed"
|
||||
|
||||
# a download that fails its checksum must not be left in the cache either
|
||||
nvm_compare_checksum() { return 1; }
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
@@ -69,11 +85,52 @@ try download_offline
|
||||
nvm_compare_checksum() { return 0; }
|
||||
command mkdir -p "${TEST_CACHE_DIR}"
|
||||
nvm_echo 'partial' > "${TEST_TARBALL}.unverified"
|
||||
command mkdir -p "${TEST_TARBALL}.unverified.abc123"
|
||||
nvm_echo 'partial' > "${TEST_TARBALL}.unverified.abc123/${TEST_SLUG}.tar.gz"
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "${CAPTURED_EXIT_CODE}" = '0' ] || die "verified download: expected exit code 0, got ${CAPTURED_EXIT_CODE}"
|
||||
[ "${CAPTURED_STDOUT}" = "${TEST_TARBALL}" ] || die "verified download: expected >${TEST_TARBALL}<, got >${CAPTURED_STDOUT}<"
|
||||
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "verified download: expected the cached archive to hold only the new download, got >$(command cat "${TEST_TARBALL}")<"
|
||||
[ ! -e "${TEST_TARBALL}.unverified" ] || die 'verified download: expected no unverified download to remain in the cache'
|
||||
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "verified download: expected no unverified download to remain in the cache, got >$(command ls "${TEST_CACHE_DIR}")<"
|
||||
[ ! -e "${WORK}/downloaded-to-final-name" ] || die 'an unverified download was written to the final cache name, where an interrupted download would be left for --offline'
|
||||
|
||||
# without any checksum tool, nothing is downloaded and the verified cache is left alone
|
||||
nvm_get_checksum_binary() { return 1; }
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "no checksum tool: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
|
||||
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "no checksum tool: expected the cached archive to be left alone, got >$(command cat "${TEST_TARBALL}")<"
|
||||
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "no checksum tool: expected nothing to be downloaded, got >$(command ls "${TEST_CACHE_DIR}")<"
|
||||
nvm_get_checksum_binary() { nvm_echo 'sha256sum'; }
|
||||
|
||||
# a downloader left running by an earlier install, which opens its output only once a later install is downloading
|
||||
# (as curl does when the response body is late), must not be able to write to the archive that the later install verified
|
||||
command rm -rf "${WORK}/.cache"
|
||||
nvm_compare_checksum() { [ "$(command cat "$1")" = 'published' ]; }
|
||||
nvm_download() {
|
||||
while [ "$#" -gt 1 ] && [ "$1" != '-o' ]; do
|
||||
shift
|
||||
done
|
||||
if [ ! -e "${WORK}/first-download" ]; then
|
||||
nvm_echo "$2" > "${WORK}/first-download"
|
||||
nvm_download_artifact node binary std v20.0.0 > "${WORK}/second-install" 2>/dev/null
|
||||
nvm_echo "$?" >> "${WORK}/second-install"
|
||||
if [ -e "${WORK}/late-open" ]; then
|
||||
nvm_echo 'tampered' >&9
|
||||
exec 9>&-
|
||||
fi
|
||||
command cat "${TEST_TARBALL}" > "${WORK}/cached-after-late-write"
|
||||
else
|
||||
# a failed `exec` redirection would end the shell; where the path is gone, curl would fail to open it
|
||||
if [ -d "$(command dirname "$(command cat "${WORK}/first-download")")" ]; then
|
||||
exec 9> "$(command cat "${WORK}/first-download")"
|
||||
command touch "${WORK}/late-open"
|
||||
fi
|
||||
nvm_echo 'published' > "$2"
|
||||
fi
|
||||
}
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "$(command cat "${WORK}/second-install")" = "$(nvm_echo "${TEST_TARBALL}"; nvm_echo 0)" ] || die "late write: expected the second install to cache its download, got >$(command cat "${WORK}/second-install")<"
|
||||
[ "$(command cat "${WORK}/cached-after-late-write")" = 'published' ] || die "late write: expected the verified archive in the cache to stay as it was, got >$(command cat "${WORK}/cached-after-late-write")<"
|
||||
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "late write: expected the first install to reject its own download (exit code 6), got ${CAPTURED_EXIT_CODE}"
|
||||
|
||||
cleanup
|
||||
@@ -70,4 +70,12 @@ EXPECTED_OUTPUT=''
|
||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to give $EXPECTED_OUTPUT, got $OUTPUT"
|
||||
[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to exit with 0, got $EXIT_CODE"
|
||||
|
||||
# An unresolved installed-version check must not overwrite a caller's VERSION.
|
||||
VERSION='caller-value'
|
||||
nvm_resolve_alias() { [ "_${1-}" = '_nonexistent' ] || return 2; nvm_echo 'v123.4.5'; }
|
||||
nvm_ensure_version_installed nonexistent >/dev/null 2>&1 && die 'expected missing version failure'
|
||||
[ "${VERSION}" = 'caller-value' ] || die "VERSION leaked: ${VERSION}"
|
||||
unset -f nvm_resolve_alias
|
||||
unset VERSION
|
||||
|
||||
cleanup
|
||||
@@ -0,0 +1,155 @@
|
||||
#!/bin/sh
|
||||
|
||||
cleanup() {
|
||||
if [ -n "${TEST_DIR-}" ] && [ -d "${TEST_DIR}" ]; then
|
||||
rm -rf "${TEST_DIR}"
|
||||
fi
|
||||
}
|
||||
|
||||
die() { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TEST_DIR=''
|
||||
TEST_DIR="$(mktemp -d)" && [ -n "${TEST_DIR}" ] && [ -d "${TEST_DIR}" ] || { TEST_DIR=''; die 'unable to create a temporary directory'; }
|
||||
TEST_DIR="$(cd "${TEST_DIR}" && pwd -P)" && [ -n "${TEST_DIR}" ] || { TEST_DIR=''; die 'unable to resolve the temporary directory'; }
|
||||
OUTSIDE="${TEST_DIR}/outside"
|
||||
OUTSIDE_DIR="${TEST_DIR}/outside-dir"
|
||||
[ "${#OUTSIDE}" -lt 90 ] || die "temporary path too long for a ustar name field: ${OUTSIDE}"
|
||||
mkdir "${OUTSIDE_DIR}"
|
||||
|
||||
# args: value, width; writes the value NUL-padded to width
|
||||
field() {
|
||||
printf '%s' "$1"
|
||||
dd if=/dev/zero bs=1 count="$(( $2 - ${#1} ))" 2>/dev/null
|
||||
}
|
||||
|
||||
# `tar -c` strips a leading `/` and refuses some of these shapes,
|
||||
# so write the member bytes directly.
|
||||
# args: member name, typeflag, linkname, content; appends to ${TEST_DIR}/archive
|
||||
member() {
|
||||
{
|
||||
field "$1" 100
|
||||
field 0000755 8
|
||||
field 0000000 8
|
||||
field 0000000 8
|
||||
field "$(printf '%011o' "${#4}")" 12
|
||||
field 00000000000 12
|
||||
printf ' '
|
||||
printf '%s' "$2"
|
||||
field "$3" 100
|
||||
printf 'ustar'
|
||||
printf '\000'
|
||||
printf '00'
|
||||
field '' 247
|
||||
} > "${TEST_DIR}/header"
|
||||
printf '%06o\000 ' "$(od -An -v -tu1 "${TEST_DIR}/header" | awk '{ for (i = 1; i <= NF; i++) s += $i } END { print s }')" \
|
||||
| dd of="${TEST_DIR}/header" bs=1 seek=148 conv=notrunc 2>/dev/null
|
||||
cat "${TEST_DIR}/header" >> "${TEST_DIR}/archive"
|
||||
if [ -n "$4" ]; then
|
||||
field "$4" 512 >> "${TEST_DIR}/archive"
|
||||
fi
|
||||
}
|
||||
|
||||
# args: output .tar.gz
|
||||
finish() {
|
||||
dd if=/dev/zero bs=512 count=2 2>/dev/null >> "${TEST_DIR}/archive"
|
||||
gzip -n < "${TEST_DIR}/archive" > "$1"
|
||||
rm -f "${TEST_DIR}/archive"
|
||||
}
|
||||
|
||||
nlinks() { ls -ld "$1" | awk '{ print $2 }'; }
|
||||
|
||||
# args: name; extracts ${TEST_DIR}/name.tar.gz into ${TEST_DIR}/name, as callers do, stderr to ${TEST_DIR}/stderr
|
||||
extract() {
|
||||
mkdir -p "${TEST_DIR}/$1"
|
||||
nvm_extract_tarball linux v0.10.0 "${TEST_DIR}/$1.tar.gz" "${TEST_DIR}/$1" 2>"${TEST_DIR}/stderr"
|
||||
}
|
||||
|
||||
# args: name, expected stderr message
|
||||
expect_refused() {
|
||||
extract "$1" && die "$1: extraction succeeded"
|
||||
case "$(cat "${TEST_DIR}/stderr")" in
|
||||
*"$2"*) ;;
|
||||
*) die "$1: unexpected output >$(cat "${TEST_DIR}/stderr")<" ;;
|
||||
esac
|
||||
[ ! -e "${TEST_DIR}/$1.extract" ] || die "$1: staging directory left behind"
|
||||
}
|
||||
|
||||
printf 'ORIGINAL\n' > "${OUTSIDE}"
|
||||
|
||||
# a member that `--strip-components 1` would turn into an absolute path
|
||||
member 'n/' 5 '' ''
|
||||
member "n/${OUTSIDE}" 0 '' 'INJECTED'
|
||||
finish "${TEST_DIR}/member.tar.gz"
|
||||
extract member || die 'member: extraction failed'
|
||||
[ "$(cat "${OUTSIDE}")" = 'ORIGINAL' ] || die 'member: wrote outside its target'
|
||||
[ "$(cat "${TEST_DIR}/member${OUTSIDE}")" = 'INJECTED' ] || die 'member: not extracted inside its target'
|
||||
[ ! -e "${TEST_DIR}/member.extract" ] || die 'member: staging directory left behind'
|
||||
|
||||
# a regular file written through a symlink to an outside directory
|
||||
member 'n/' 5 '' ''
|
||||
member 'n/l' 2 "${OUTSIDE_DIR}" ''
|
||||
member 'n/l/written' 0 '' 'INJECTED'
|
||||
finish "${TEST_DIR}/through-symlink.tar.gz"
|
||||
extract through-symlink
|
||||
[ ! -e "${OUTSIDE_DIR}/written" ] || die 'through-symlink: wrote outside its target'
|
||||
|
||||
# hard links whose target is, or becomes, outside the target directory
|
||||
member 'n/' 5 '' ''
|
||||
member 'n/linked' 1 "n/${OUTSIDE}" ''
|
||||
finish "${TEST_DIR}/hardlink-stripped.tar.gz"
|
||||
|
||||
member 'n/' 5 '' ''
|
||||
member 'n/linked' 1 "${OUTSIDE}" ''
|
||||
finish "${TEST_DIR}/hardlink-absolute.tar.gz"
|
||||
|
||||
member 'n/' 5 '' ''
|
||||
member 'n/linked' 1 'n/../../outside' ''
|
||||
finish "${TEST_DIR}/hardlink-dotdot.tar.gz"
|
||||
|
||||
member 'n/' 5 '' ''
|
||||
member 'n/l' 2 "${TEST_DIR}" ''
|
||||
member 'n/linked' 1 'n/l/outside' ''
|
||||
finish "${TEST_DIR}/hardlink-through-symlink.tar.gz"
|
||||
|
||||
for name in hardlink-stripped hardlink-absolute hardlink-dotdot hardlink-through-symlink; do
|
||||
extract "${name}" && die "${name}: extraction succeeded"
|
||||
[ "$(nlinks "${OUTSIDE}")" = 1 ] || die "${name}: linked a file outside its target"
|
||||
[ ! -e "${TEST_DIR}/${name}.extract" ] || die "${name}: staging directory left behind"
|
||||
done
|
||||
|
||||
# a hard link inside the archive
|
||||
member 'n/' 5 '' ''
|
||||
member 'n/a' 0 '' 'a'
|
||||
member 'n/b' 1 'n/a' ''
|
||||
finish "${TEST_DIR}/hardlink-internal.tar.gz"
|
||||
expect_refused hardlink-internal 'hardlink-internal.tar.gz must not contain hard links'
|
||||
|
||||
# a legitimate archive: top-level directory renamed into place, dotfiles kept,
|
||||
# even when the environment asks `ls` to decorate its output
|
||||
mkdir -p "${TEST_DIR}/src/node-v0.10.0-linux-x64/bin" "${TEST_DIR}/legit/stale"
|
||||
printf 'node\n' > "${TEST_DIR}/src/node-v0.10.0-linux-x64/bin/node"
|
||||
printf 'hidden\n' > "${TEST_DIR}/src/node-v0.10.0-linux-x64/.hidden"
|
||||
(cd "${TEST_DIR}/src" && tar -czf "${TEST_DIR}/legit.tar.gz" node-v0.10.0-linux-x64) || die 'unable to create legit archive'
|
||||
CLICOLOR=1 CLICOLOR_FORCE=1 QUOTING_STYLE=shell-always LS_COLORS='di=01;34' extract legit || die "legit: extraction failed: $(cat "${TEST_DIR}/stderr")"
|
||||
[ "$(cat "${TEST_DIR}/legit/bin/node")" = 'node' ] || die 'legit: bin/node missing'
|
||||
[ "$(cat "${TEST_DIR}/legit/.hidden")" = 'hidden' ] || die 'legit: dotfile missing'
|
||||
[ ! -e "${TEST_DIR}/legit/stale" ] || die 'legit: stale target contents kept'
|
||||
[ ! -e "${TEST_DIR}/legit.extract" ] || die 'legit: staging directory left behind'
|
||||
|
||||
# more than one top-level entry
|
||||
mkdir -p "${TEST_DIR}/src/other"
|
||||
(cd "${TEST_DIR}/src" && tar -czf "${TEST_DIR}/two.tar.gz" node-v0.10.0-linux-x64 other) || die 'unable to create two-entry archive'
|
||||
expect_refused two 'two.tar.gz must contain exactly one top-level directory'
|
||||
|
||||
# a top-level symlink to a directory
|
||||
mkdir -p "${TEST_DIR}/link-src"
|
||||
ln -s "${TEST_DIR}/src/node-v0.10.0-linux-x64" "${TEST_DIR}/link-src/node-v0.10.0-linux-x64"
|
||||
(cd "${TEST_DIR}/link-src" && tar -czf "${TEST_DIR}/symlink.tar.gz" node-v0.10.0-linux-x64) || die 'unable to create symlink archive'
|
||||
expect_refused symlink 'symlink.tar.gz must contain exactly one top-level directory'
|
||||
|
||||
cleanup
|
||||
unset -f die cleanup field member finish nlinks extract expect_refused
|
||||
unset TEST_DIR OUTSIDE OUTSIDE_DIR name
|
||||
@@ -146,4 +146,107 @@ assert_uname_os 'FreeBSD host 14.0-STABLE' "${OS_RELEASE}" 'freebsd'
|
||||
printf 'NAME=Helios\nID=helios\n' > "${OS_RELEASE}"
|
||||
assert_uname_os 'SunOS host 5.11' "${OS_RELEASE}" 'sunos'
|
||||
|
||||
# a HarmonyOS kernel without the OpenHarmony userland is not an OS `nvm` knows
|
||||
assert_uname_os 'HarmonyOS localhost HongMeng Kernel 1.12.0 #1 SMP Tue Jul 14 09:40:51 UTC 2026 aarch64 Toybox' "${MISSING_OS_RELEASE}" ''
|
||||
|
||||
# openharmony on aarch64 composes the slug a mirror has to serve; this runs on every host,
|
||||
# and pins that `nvm_get_arch` gives `arm64`, not Alpine's `arm64-musl`
|
||||
NVM_MOCK_UNAME=aarch64
|
||||
export NVM_MOCK_UNAME
|
||||
SLUG="$(nvm_get_os() { nvm_echo openharmony; }; PATH="${MOCK_BIN}:${PATH}" nvm_get_download_slug node binary v22.22.0)"
|
||||
unset NVM_MOCK_UNAME
|
||||
[ "${SLUG}" = 'node-v22.22.0-openharmony-arm64' ] || die "nvm_get_download_slug produced \"${SLUG}\", expected \"node-v22.22.0-openharmony-arm64\""
|
||||
|
||||
# OpenHarmony detection via chroot
|
||||
|
||||
MOCKS_DIR="$(pwd)/../../mocks"
|
||||
|
||||
setup_chroot() {
|
||||
chroot_dir=$1
|
||||
param_mock=$2
|
||||
uname_mock=$3
|
||||
|
||||
# Directories
|
||||
mkdir -p "${chroot_dir}/etc"
|
||||
mkdir -p "${chroot_dir}/bin"
|
||||
mkdir -p "${chroot_dir}/usr/bin"
|
||||
mkdir -p "${chroot_dir}/lib64"
|
||||
mkdir -p "${chroot_dir}/dev"
|
||||
mkdir -p "${chroot_dir}/system/bin"
|
||||
|
||||
# Files and binaries
|
||||
cp ../../../nvm.sh "${chroot_dir}/"
|
||||
cp /bin/sh /usr/bin/dirname /usr/bin/grep "${chroot_dir}/usr/bin/"
|
||||
cp "${uname_mock}" "${chroot_dir}/usr/bin/uname"
|
||||
chmod +x "${chroot_dir}/usr/bin/uname"
|
||||
ln -sf ../usr/bin/sh "${chroot_dir}/bin/sh"
|
||||
|
||||
# Fake OpenHarmony param binary
|
||||
cp "${param_mock}" "${chroot_dir}/system/bin/param"
|
||||
chmod +x "${chroot_dir}/system/bin/param"
|
||||
|
||||
# Libraries
|
||||
for binary in /bin/sh /usr/bin/dirname /usr/bin/uname /usr/bin/grep; do
|
||||
for lib in $(ldd $binary | awk '{print $3}' | grep "^/"); do
|
||||
dir=$(dirname "${lib}")
|
||||
mkdir -p "${chroot_dir}${dir}"
|
||||
cp "${lib}" "${chroot_dir}${dir}/"
|
||||
done
|
||||
done
|
||||
|
||||
# Dynamic linker
|
||||
cp /lib64/ld-linux-x86-64.so.2 "${chroot_dir}/lib64/"
|
||||
|
||||
# /dev/null
|
||||
sudo mknod "${chroot_dir}/dev/null" c 1 3
|
||||
}
|
||||
|
||||
# The chroot fixtures assume a glibc x86-64 layout (`ldd`, a fixed dynamic-linker path
|
||||
# under /lib64), which macOS and musl Alpine do not have; skip the chroot checks there.
|
||||
if [ -f "/etc/alpine-release" ] || ! nvm_has ldd || [ ! -e /lib64/ld-linux-x86-64.so.2 ]; then
|
||||
echo "no glibc x86-64 layout; skipping chroot OpenHarmony checks"
|
||||
else
|
||||
CHROOT_UNAME_LINUX_PARAM_OPENHARMONY="${TEST_DIR}/uname_linux_param_openharmony"
|
||||
CHROOT_UNAME_LINUX_PARAM_OTHER="${TEST_DIR}/uname_linux_param_other"
|
||||
CHROOT_UNAME_HARMONYOS_PARAM_OPENHARMONY="${TEST_DIR}/uname_harmonyos_param_openharmony"
|
||||
CHROOT_UNAME_HARMONYOS_PARAM_OTHER="${TEST_DIR}/uname_harmonyos_param_other"
|
||||
CHROOT_UNAME_OSX_PARAM_OPENHARMONY="${TEST_DIR}/uname_osx_param_openharmony"
|
||||
|
||||
setup_chroot "${CHROOT_UNAME_LINUX_PARAM_OPENHARMONY}" "${MOCKS_DIR}/param_openharmony" "${MOCKS_DIR}/uname_linux_aarch64"
|
||||
setup_chroot "${CHROOT_UNAME_LINUX_PARAM_OTHER}" "${MOCKS_DIR}/param_other" "${MOCKS_DIR}/uname_linux_aarch64"
|
||||
setup_chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OPENHARMONY}" "${MOCKS_DIR}/param_openharmony" "${MOCKS_DIR}/uname_harmonyos_aarch64"
|
||||
setup_chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OTHER}" "${MOCKS_DIR}/param_other" "${MOCKS_DIR}/uname_harmonyos_aarch64"
|
||||
setup_chroot "${CHROOT_UNAME_OSX_PARAM_OPENHARMONY}" "${MOCKS_DIR}/param_openharmony" "${MOCKS_DIR}/uname_osx_amd64"
|
||||
|
||||
# Linux kernel with an OpenHarmony param -> openharmony
|
||||
OS_UNAME_LINUX_PARAM_OPENHARMONY=$(sudo chroot "${CHROOT_UNAME_LINUX_PARAM_OPENHARMONY}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
|
||||
if [ "${OS_UNAME_LINUX_PARAM_OPENHARMONY}" != "openharmony" ]; then
|
||||
die "nvm_get_os produced \"${OS_UNAME_LINUX_PARAM_OPENHARMONY}\", expected \"openharmony\""
|
||||
fi
|
||||
|
||||
# Linux kernel with an unrelated param stays linux
|
||||
OS_UNAME_LINUX_PARAM_OTHER=$(sudo chroot "${CHROOT_UNAME_LINUX_PARAM_OTHER}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
|
||||
if [ "${OS_UNAME_LINUX_PARAM_OTHER}" != "linux" ]; then
|
||||
die "nvm_get_os produced \"${OS_UNAME_LINUX_PARAM_OTHER}\", expected \"linux\""
|
||||
fi
|
||||
|
||||
# HarmonyOS kernel with an OpenHarmony param -> openharmony
|
||||
OS_UNAME_HARMONYOS_PARAM_OPENHARMONY=$(sudo chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OPENHARMONY}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
|
||||
if [ "${OS_UNAME_HARMONYOS_PARAM_OPENHARMONY}" != "openharmony" ]; then
|
||||
die "nvm_get_os produced \"${OS_UNAME_HARMONYOS_PARAM_OPENHARMONY}\", expected \"openharmony\""
|
||||
fi
|
||||
|
||||
# HarmonyOS kernel with an unrelated param stays empty
|
||||
OS_UNAME_HARMONYOS_PARAM_OTHER=$(sudo chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OTHER}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
|
||||
if [ "_${OS_UNAME_HARMONYOS_PARAM_OTHER}" != "_" ]; then
|
||||
die "nvm_get_os produced \"${OS_UNAME_HARMONYOS_PARAM_OTHER}\", expected \"\""
|
||||
fi
|
||||
|
||||
# an OS that `uname` already identified is never re-probed
|
||||
OS_UNAME_OSX_PARAM_OPENHARMONY=$(sudo chroot "${CHROOT_UNAME_OSX_PARAM_OPENHARMONY}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
|
||||
if [ "${OS_UNAME_OSX_PARAM_OPENHARMONY}" != "darwin" ]; then
|
||||
die "nvm_get_os produced \"${OS_UNAME_OSX_PARAM_OPENHARMONY}\", expected \"darwin\""
|
||||
fi
|
||||
fi
|
||||
|
||||
cleanup
|
||||
@@ -54,3 +54,30 @@ try_err nvm_install_latest_npm
|
||||
|
||||
EXPECTED="Unable to obtain npm version."
|
||||
[ "${CAPTURED_STDERR}" = "${EXPECTED}" ] || die "When node is system and npm is unavailable, expected >${EXPECTED}<; got >${CAPTURED_STDERR}"
|
||||
|
||||
nvm_ls_current() {
|
||||
node --version
|
||||
}
|
||||
node() {
|
||||
echo "${TEST_NODE_VERSION}"
|
||||
}
|
||||
npm() {
|
||||
case "${1-}" in
|
||||
--version) echo '10.9.0' ;;
|
||||
*) echo "npm $*" ;;
|
||||
esac
|
||||
}
|
||||
for TEST_NODE_VERSION in v21.7.3 v22.8.0; do
|
||||
OUTPUT="$(nvm_install_latest_npm | grep 'install -g')"
|
||||
[ "${OUTPUT}" = 'npm install -g npm@10' ] || die "on node ${TEST_NODE_VERSION}, expected npm@10; got >${OUTPUT}<"
|
||||
done
|
||||
|
||||
for TEST_NODE_VERSION in v20.17.0 v22.9.0 v22.22.1 v23.0.0 v24.14.1 v25.0.0; do
|
||||
OUTPUT="$(nvm_install_latest_npm | grep 'install -g')"
|
||||
[ "${OUTPUT}" = 'npm install -g npm@11' ] || die "on node ${TEST_NODE_VERSION}, expected npm@11; got >${OUTPUT}<"
|
||||
done
|
||||
|
||||
for TEST_NODE_VERSION in v22.22.2 v24.15.0 v26.0.0; do
|
||||
OUTPUT="$(nvm_install_latest_npm | grep 'install -g')"
|
||||
[ "${OUTPUT}" = 'npm install -g npm' ] || die "on node ${TEST_NODE_VERSION}, expected the latest npm; got >${OUTPUT}<"
|
||||
done
|
||||
@@ -42,9 +42,8 @@ nvm_is_valid_version v9.0.0-v8-canary201710301f8f3dcf36 || die "nvm_is_valid_ver
|
||||
nvm_is_valid_version v8.16.2-test201910293843c56739f || die "nvm_is_valid_version v8.16.2-test201910293843c56739f did not return 0"
|
||||
nvm_is_valid_version iojs-v3.2.1-nightly201508291a531b4e44 || die "nvm_is_valid_version iojs-v3.2.1-nightly201508291a531b4e44 did not return 0"
|
||||
|
||||
# `nvm_is_valid_version` must not fork a command for a plain version: mock
|
||||
# `awk`, which the version comparisons used, and fail if it runs. (Prereleases
|
||||
# still reach the comparison, which is why this checks a release version.)
|
||||
# `nvm_is_valid_version` must not fork a command, since it checks what a symlinked `.nvmrc` points at:
|
||||
# mock `awk`, which the version comparisons used, and fail if it runs.
|
||||
TEST_DIR="$(pwd)/nvm_is_valid_version_tmp"
|
||||
[ ! -e "${TEST_DIR}" ] && mkdir "${TEST_DIR}"
|
||||
MOCK_BIN="${TEST_DIR}/bin"
|
||||
@@ -58,5 +57,14 @@ chmod +x "${MOCK_BIN}/awk"
|
||||
|
||||
PATH="${MOCK_BIN}:${PATH}" nvm_is_valid_version v18.20.0 \
|
||||
|| die 'nvm_is_valid_version v18.20.0 forked a command'
|
||||
PATH="${MOCK_BIN}:${PATH}" nvm_is_valid_version v9.4.0-rc.1 \
|
||||
|| die 'nvm_is_valid_version v9.4.0-rc.1 forked a command'
|
||||
for VERSION in a.b.c-rc.1 1.2.3.4-rc.1 1..2-rc.1; do
|
||||
OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_is_valid_version "${VERSION}" 2>&1)" \
|
||||
&& die "nvm_is_valid_version ${VERSION} did not return 1"
|
||||
case "${OUTPUT}" in
|
||||
*'awk was called'*) die "nvm_is_valid_version ${VERSION} forked a command" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
rm -rf "${TEST_DIR}"
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
|
||||
die() { echo "$*" >&2; cleanup; exit 1; }
|
||||
cleanup() { rm -rf "${TEST_DIR}"; }
|
||||
|
||||
\. ../../../nvm.sh --no-use
|
||||
\. ../../common.sh
|
||||
|
||||
TEST_DIR="${PWD}/nvm_ls_hidden_noglob_tmp"
|
||||
NVM_DIR="${TEST_DIR}"
|
||||
export NVM_DIR
|
||||
mkdir -p "${NVM_DIR}/versions/node/.hidden" "${NVM_DIR}/versions/node/v24.13.0/bin" || die 'could not create fixture'
|
||||
make_echo "${NVM_DIR}/versions/node/v24.13.0/bin/node" 'v24.13.0' || die 'could not create fake node'
|
||||
|
||||
set -f
|
||||
try nvm_ls 24
|
||||
[ "_${CAPTURED_STDOUT}" = '_v24.13.0' ] || die "unexpected output with noglob: ${CAPTURED_STDOUT}"
|
||||
case "$-" in *f*) ;; *) die 'nvm_ls changed noglob';; esac
|
||||
set +f
|
||||
try nvm_ls
|
||||
case "${CAPTURED_STDOUT}" in *.hidden*) die "expected hidden entries to be skipped; got ${CAPTURED_STDOUT}" ;; esac
|
||||
rm -rf "${NVM_DIR}/versions/node/v24.13.0"
|
||||
try nvm_ls 24
|
||||
[ "_${CAPTURED_STDERR}" = '_' ] || die "unexpected error for hidden-only directory: ${CAPTURED_STDERR}"
|
||||
cleanup
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
rm -rf "${TEST_DIR-}"
|
||||
unset -f nvm_has_system_node nvm_has_system_iojs node
|
||||
unset TEST_DIR NVM_DIR SYSTEM_CHECKS OUTPUT
|
||||
}
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
# an isolated NVM_DIR with known versions
|
||||
TEST_DIR="${PWD}/nvm_ls_system_check_tmp"
|
||||
for VERSION in v20.0.0 v24.13.0; do
|
||||
mkdir -p "${TEST_DIR}/versions/node/${VERSION}/bin" || die "failed to create ${VERSION} dir"
|
||||
make_echo "${TEST_DIR}/versions/node/${VERSION}/bin/node" "${VERSION}" || die "failed to create ${VERSION} node binary"
|
||||
done
|
||||
unset VERSION
|
||||
NVM_DIR="${TEST_DIR}"
|
||||
|
||||
# each check runs `nvm deactivate` in a subshell, so count them
|
||||
SYSTEM_CHECKS=0
|
||||
nvm_has_system_node() { SYSTEM_CHECKS=$((SYSTEM_CHECKS + 1)); return 0; }
|
||||
nvm_has_system_iojs() { SYSTEM_CHECKS=$((SYSTEM_CHECKS + 1)); return 1; }
|
||||
node() { echo 'v18.0.0'; }
|
||||
|
||||
# a partial version never lists the system version, so it should not check for one
|
||||
nvm_ls 24 > "${TEST_DIR}/output" || die '`nvm_ls 24` failed'
|
||||
OUTPUT="$(cat "${TEST_DIR}/output")"
|
||||
[ "_${OUTPUT}" = '_v24.13.0' ] || die "\`nvm_ls 24\` should output >v24.13.0<; got >${OUTPUT}<"
|
||||
[ "${SYSTEM_CHECKS}" = 0 ] || die "\`nvm_ls 24\` should not check for a system version; checked ${SYSTEM_CHECKS} times"
|
||||
|
||||
nvm_ls iojs-v3 > "${TEST_DIR}/output"
|
||||
[ "${SYSTEM_CHECKS}" = 0 ] || die "\`nvm_ls iojs-v3\` should not check for a system version; checked ${SYSTEM_CHECKS} times"
|
||||
|
||||
# these patterns still list it
|
||||
nvm_ls > "${TEST_DIR}/output" || die '`nvm_ls` failed'
|
||||
OUTPUT="$(cat "${TEST_DIR}/output")"
|
||||
[ "_${OUTPUT}" = "_v20.0.0
|
||||
v24.13.0
|
||||
system v18.0.0" ] || die "\`nvm_ls\` should list the system version; got >${OUTPUT}<"
|
||||
[ "${SYSTEM_CHECKS}" != 0 ] || die '`nvm_ls` should check for a system version'
|
||||
|
||||
SYSTEM_CHECKS=0
|
||||
nvm_ls system > "${TEST_DIR}/output" || die '`nvm_ls system` failed'
|
||||
OUTPUT="$(cat "${TEST_DIR}/output")"
|
||||
[ "_${OUTPUT}" = '_system v18.0.0' ] || die "\`nvm_ls system\` should output >system v18.0.0<; got >${OUTPUT}<"
|
||||
[ "${SYSTEM_CHECKS}" != 0 ] || die '`nvm_ls system` should check for a system version'
|
||||
|
||||
cleanup
|
||||
@@ -0,0 +1,57 @@
|
||||
#!/bin/sh
|
||||
|
||||
WORK="$PWD/ls_remote-cntrl-work.$$"
|
||||
export HOME="$WORK/home"
|
||||
export NVM_DIR="$HOME/.nvm"
|
||||
|
||||
cleanup() {
|
||||
unset -f die cleanup nvm_download
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
mkdir -p "$NVM_DIR/alias"
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
ESC="$(printf '\033')"
|
||||
BEL="$(printf '\007')"
|
||||
|
||||
# a malicious/compromised mirror: the LTS codename field ($10) carries terminal
|
||||
# escape sequences (an OSC window-title write and an SGR color change);
|
||||
# a valid codename (Iron) is included to prove real rows are still listed;
|
||||
# so are a UTF-8 C1 CSI and a right-to-left override, which are not ASCII control characters
|
||||
nvm_download() {
|
||||
printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\tlts\tsecurity\n'
|
||||
printf 'v23.0.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\t-\t-\n'
|
||||
printf 'v22.0.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\tJod\033]0;PWNED\007\033[31m\t-\n'
|
||||
printf 'v21.0.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\tJod\302\23331m\t-\n'
|
||||
printf 'v20.1.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\tIron\342\200\256\t-\n'
|
||||
printf 'v20.0.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\tIron\t-\n'
|
||||
}
|
||||
|
||||
OUTPUT="$(nvm_ls_remote 2>&1)" || true
|
||||
case "${OUTPUT}" in
|
||||
*"${ESC}"* | *"${BEL}"*) die "nvm_ls_remote printed control characters from the mirror's LTS codename: $(printf '%s' "${OUTPUT}" | od -c | head -5)" ;;
|
||||
esac
|
||||
case "${OUTPUT}" in
|
||||
*'v21.0.0'* | *'v20.1.0'*) die "nvm_ls_remote listed a row with a malformed LTS codename: ${OUTPUT}" ;;
|
||||
*'v20.0.0'*'Iron'*'v23.0.0'*) ;;
|
||||
*) die "nvm_ls_remote did not list the valid rows: ${OUTPUT}" ;;
|
||||
esac
|
||||
if [ -f "$NVM_DIR/.lts-status" ] && nvm_grep -q "${ESC}" "$NVM_DIR/.lts-status"; then
|
||||
die 'control characters from the mirror were recorded in .lts-status'
|
||||
fi
|
||||
|
||||
# a checksum from the mirror is printed when it does not match; its control characters must not be
|
||||
printf 'tarball' > "$WORK/tarball"
|
||||
OUTPUT="$(nvm_compare_checksum "$WORK/tarball" "${ESC}]0;PWNED${BEL}${ESC}[31m" 2>&1)" && die 'nvm_compare_checksum accepted a mismatched checksum'
|
||||
case "${OUTPUT}" in
|
||||
*"${ESC}"* | *"${BEL}"*) die "nvm_compare_checksum printed control characters from the mirror's checksum: $(printf '%s' "${OUTPUT}" | od -c | head -5)" ;;
|
||||
*"Checksums do not match"*"?]0;PWNED??[31m' expected."*) ;;
|
||||
*) die "unexpected nvm_compare_checksum output: ${OUTPUT}" ;;
|
||||
esac
|
||||
|
||||
cleanup
|
||||
echo "nvm_ls_remote LTS codename control characters: passed"
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
# these mocks must not be recorded as the LTS status of $NVM_DIR's versions
|
||||
nvm_record_lts_status() { :; }
|
||||
|
||||
nvm_download() {
|
||||
cat "$PWD/mocks/nodejs.org-dist-index.tab"
|
||||
}
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
# no codename contains these characters literally, though most match them as a regex
|
||||
for NVM_LTS in . 'i.on' 'ar.*' '(' '^i' 'n$'; do
|
||||
try nvm_ls_remote
|
||||
[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "\`NVM_LTS='${NVM_LTS}' nvm_ls_remote\` should output N/A; got >$CAPTURED_STDOUT<"
|
||||
[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "\`NVM_LTS='${NVM_LTS}' nvm_ls_remote\` should exit 3; got $CAPTURED_EXIT_CODE"
|
||||
done
|
||||
unset NVM_LTS
|
||||
|
||||
# a name matches every codename that contains it
|
||||
OUTPUT="$(NVM_LTS=on nvm_ls_remote)"
|
||||
CODENAMES="$(nvm_echo "$OUTPUT" | command awk '{ print $2 }' | command sort -u | command tr '\n' ' ')"
|
||||
[ "_$CODENAMES" = "_Argon Boron Carbon Iron " ] || die "\`NVM_LTS=on nvm_ls_remote\` should list the Argon, Boron, Carbon and Iron lines; got >$CODENAMES<"
|
||||
|
||||
for NVM_LTS in iron ir; do
|
||||
OUTPUT="$(nvm_ls_remote)"
|
||||
[ -n "$OUTPUT" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` output nothing"
|
||||
OTHER_LINES="$(nvm_echo "$OUTPUT" | command awk '$2 != "Iron"')"
|
||||
[ -z "$OTHER_LINES" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` listed versions outside the Iron line: >$OTHER_LINES<"
|
||||
done
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/bin/sh
|
||||
# Mocks are called indirectly by the sourced nvm functions.
|
||||
# shellcheck disable=SC2329
|
||||
|
||||
die () { echo "$@"; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh --no-use
|
||||
\. ../../common.sh
|
||||
|
||||
nvm_record_lts_status() { :; }
|
||||
export NVM_NODEJS_ORG_MIRROR='https://nodejs.org/dist'
|
||||
export NVM_IOJS_ORG_MIRROR='https://iojs.org/dist'
|
||||
|
||||
# A failed request may have already written part of index.tab to stdout.
|
||||
# Neither an empty response nor a partial one should look like a successful list.
|
||||
for FLAVOR in node iojs; do
|
||||
for PARTIAL in no yes; do
|
||||
nvm_download() {
|
||||
if [ "${PARTIAL}" = yes ]; then
|
||||
cat "${PWD}/mocks/nodejs.org-dist-index.tab"
|
||||
fi
|
||||
return 60
|
||||
}
|
||||
|
||||
try nvm_ls_remote_index_tab "${FLAVOR}" std ''
|
||||
[ "${CAPTURED_EXIT_CODE}" = 3 ] || die "${FLAVOR}: failed download should return 3"
|
||||
[ "${CAPTURED_STDOUT}" = 'N/A' ] || die "${FLAVOR}: failed download must not print partial versions"
|
||||
|
||||
MIRROR="$(nvm_get_mirror "${FLAVOR}" std)"
|
||||
try_err nvm_ls_remote_index_tab "${FLAVOR}" std ''
|
||||
[ "${CAPTURED_STDERR}" = "Failed to download version list from ${MIRROR}/index.tab" ] \
|
||||
|| die "${FLAVOR}: missing download error: ${CAPTURED_STDERR}"
|
||||
done
|
||||
done
|
||||
|
||||
# A working mirror should still provide its versions when the other one fails,
|
||||
# but the failure must remain visible on stderr.
|
||||
nvm_download() {
|
||||
case "$*" in
|
||||
*https://nodejs.org/dist/index.tab*) return 60 ;;
|
||||
*) cat "${PWD}/mocks/iojs.org-dist-index.tab" ;;
|
||||
esac
|
||||
}
|
||||
try nvm_remote_versions
|
||||
[ "${CAPTURED_EXIT_CODE}" = 3 ] || die 'partial list should preserve the failure status'
|
||||
[ "${CAPTURED_STDOUT}" = "$(cat "${PWD}/mocks/nvm_ls_remote_iojs.txt")" ] \
|
||||
|| die 'the working io.js mirror should still provide its versions'
|
||||
try_err nvm_remote_versions
|
||||
[ "${CAPTURED_STDERR}" = 'Failed to download version list from https://nodejs.org/dist/index.tab' ] \
|
||||
|| die 'partial list should report the failed mirror'
|
||||
|
||||
# Custom mirrors may contain credentials; the new diagnostic must redact them.
|
||||
export NVM_NODEJS_ORG_MIRROR='https://user:password@example.com/dist'
|
||||
nvm_download() { return 60; }
|
||||
try_err nvm_ls_remote
|
||||
[ "${CAPTURED_STDERR}" = 'Failed to download version list from https://***@example.com/dist/index.tab' ] \
|
||||
|| die 'the download error should redact mirror credentials'
|
||||
|
||||
unset -f nvm_download nvm_record_lts_status
|
||||
@@ -35,6 +35,44 @@ for TARGET in .npmrc .git-credentials; do
|
||||
[ -z "${CAPTURED_STDOUT}" ] || die "expected no version from a symlink to ${TARGET}; got \`${CAPTURED_STDOUT}\`"
|
||||
done
|
||||
|
||||
# every local user can read a command's arguments, so the content is not passed in them either
|
||||
MOCK_BIN="${TEST_DIR}/bin"
|
||||
mkdir -p "${MOCK_BIN}" || die 'failed to create the mock bin dir'
|
||||
for COMMAND in grep sed awk tr; do
|
||||
{
|
||||
echo '#!/bin/sh'
|
||||
printf '%s\n' 'printf "%s\n" "$*" >> "${0%/*}/../arguments"'
|
||||
echo 'PATH="${PATH#*:}"'
|
||||
echo 'exec "${0##*/}" "$@"'
|
||||
} > "${MOCK_BIN}/${COMMAND}"
|
||||
chmod +x "${MOCK_BIN}/${COMMAND}"
|
||||
done
|
||||
printf 'SECRET=\n' > "${TEST_DIR}/home/wg.key"
|
||||
printf 'a=b\nSECRET=c\nSECRET=d\n' > "${TEST_DIR}/home/.env"
|
||||
printf 'a.b.c-SECRET\n' > "${TEST_DIR}/home/token"
|
||||
printf '1.2.3.4-SECRET\n' > "${TEST_DIR}/home/token2"
|
||||
|
||||
for TARGET in wg.key .env token token2; do
|
||||
rm -f "${NVMRC}"
|
||||
ln -s "../home/${TARGET}" "${NVMRC}" || die 'failed to create the symlink'
|
||||
|
||||
: > "${TEST_DIR}/arguments"
|
||||
CAPTURED_STDERR="$(set +x; PATH="${MOCK_BIN}:${PATH}" nvm_process_nvmrc "${NVMRC}" 2>&1 >/dev/null)" \
|
||||
&& die "expected a symlink to ${TARGET} to be invalid"
|
||||
[ "${CAPTURED_STDERR}" = "invalid .nvmrc! '${NVMRC}' is a symbolic link, so it must contain a version or an alias name; its content is not shown." ] || die "expected the symlink error for ${TARGET}; got \`${CAPTURED_STDERR}\`"
|
||||
[ -s "${TEST_DIR}/arguments" ] || die 'expected the mocked commands to record their arguments'
|
||||
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "expected a symlink to ${TARGET} to not put its content in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
|
||||
done
|
||||
|
||||
# nor when the content is valid
|
||||
printf 'SECRET=\n20\n' > "${TEST_DIR}/home/accepted"
|
||||
rm -f "${NVMRC}"
|
||||
ln -s ../home/accepted "${NVMRC}" || die 'failed to create the symlink'
|
||||
: > "${TEST_DIR}/arguments"
|
||||
CAPTURED_STDOUT="$(set +x; PATH="${MOCK_BIN}:${PATH}" nvm_process_nvmrc "${NVMRC}" 2>/dev/null)" || die 'expected a symlink to a valid version file to be valid'
|
||||
[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` from a symlink to a valid version file; got \`${CAPTURED_STDOUT}\`"
|
||||
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "expected a symlink to a valid version file to not put its content in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
|
||||
|
||||
nvm alias nvmrc_symlink_alias v0.0.1 > /dev/null 2>&1 || die 'failed to create an alias'
|
||||
|
||||
# a deliberate symlink to another version file, in or above the project, still works
|
||||
|
||||
Executable
+78
@@ -0,0 +1,78 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
rm -rf "${TEST_DIR-}"
|
||||
unset TEST_DIR SWAP_FROM SWAP_TO SWAP_DONE
|
||||
}
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
TEST_DIR="${PWD}/nvm_process_nvmrc_swap_tmp"
|
||||
mkdir -p "${TEST_DIR}/home" "${TEST_DIR}/project" "${TEST_DIR}/bin" || die 'failed to create test dirs'
|
||||
printf 'https://user:SECRET@github.com\n' > "${TEST_DIR}/home/.git-credentials"
|
||||
NVMRC="${TEST_DIR}/project/.nvmrc"
|
||||
|
||||
# `cat` is the first command `nvm_process_nvmrc` runs:
|
||||
# the mock swaps `.nvmrc` once, as another user who can write the directory could, then records its arguments
|
||||
export SWAP_FROM SWAP_TO SWAP_DONE
|
||||
SWAP_TO="${NVMRC}"
|
||||
SWAP_DONE="${TEST_DIR}/swapped"
|
||||
{
|
||||
echo '#!/bin/sh'
|
||||
printf '%s\n' '[ -e "${SWAP_DONE}" ] || { : > "${SWAP_DONE}"; command mv -f "${SWAP_FROM}" "${SWAP_TO}"; }'
|
||||
} > "${TEST_DIR}/bin/cat"
|
||||
for COMMAND in cat grep sed awk tr find ls; do
|
||||
{
|
||||
[ "${COMMAND}" = cat ] || echo '#!/bin/sh'
|
||||
printf '%s\n' 'printf "%s\n" "$*" >> "${0%/*}/../arguments"'
|
||||
echo 'PATH="${PATH#*:}"'
|
||||
echo 'exec "${0##*/}" "$@"'
|
||||
} >> "${TEST_DIR}/bin/${COMMAND}"
|
||||
chmod +x "${TEST_DIR}/bin/${COMMAND}"
|
||||
done
|
||||
|
||||
check() {
|
||||
: > "${TEST_DIR}/arguments"
|
||||
rm -f "${SWAP_DONE}"
|
||||
CAPTURED_EXIT_CODE=0
|
||||
CAPTURED_STDOUT="$(set +x; PATH="${TEST_DIR}/bin:${PATH}" nvm_process_nvmrc "${NVMRC}" 2> "${TEST_DIR}/stderr")" || CAPTURED_EXIT_CODE=$?
|
||||
CAPTURED_STDERR="$(command cat "${TEST_DIR}/stderr")"
|
||||
[ -e "${SWAP_DONE}" ] || die "$1: expected the mocked \`cat\` to swap the file"
|
||||
case "${CAPTURED_STDOUT}${CAPTURED_STDERR}" in
|
||||
*SECRET*) die "$1: expected the content to not be shown; got \`${CAPTURED_STDOUT}${CAPTURED_STDERR}\`" ;;
|
||||
esac
|
||||
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "$1: expected the content to not be in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
|
||||
}
|
||||
|
||||
# a regular file, swapped for a symlink before it is read
|
||||
rm -f "${NVMRC}"
|
||||
printf 'v20.0.0\n' > "${NVMRC}"
|
||||
SWAP_FROM="${TEST_DIR}/project/link"
|
||||
ln -s ../home/.git-credentials "${SWAP_FROM}" || die 'failed to create the symlink'
|
||||
check 'a regular file swapped for a symlink'
|
||||
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected the regular file's version; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
[ "${CAPTURED_STDOUT}" = v20.0.0 ] || die "expected the regular file's version; got \`${CAPTURED_STDOUT}\`"
|
||||
|
||||
# a symlink, swapped for a regular file before it is checked
|
||||
rm -f "${NVMRC}"
|
||||
ln -s ../home/.git-credentials "${NVMRC}" || die 'failed to create the symlink'
|
||||
SWAP_FROM="${TEST_DIR}/project/regular"
|
||||
printf 'v20.0.0\n' > "${SWAP_FROM}"
|
||||
check 'a symlink swapped for a regular file'
|
||||
[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a symlink swapped for a regular file to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
|
||||
# a hard link to another file is treated like a symlink
|
||||
rm -f "${NVMRC}"
|
||||
ln "${TEST_DIR}/home/.git-credentials" "${NVMRC}" || die 'failed to create the hard link'
|
||||
try_err nvm_process_nvmrc "${NVMRC}"
|
||||
[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a hard link to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
case "${CAPTURED_STDERR}" in
|
||||
*SECRET*) die "expected a hard link's content to not be shown; got \`${CAPTURED_STDERR}\`" ;;
|
||||
esac
|
||||
|
||||
cleanup
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
# keys that only match each other as a regex are distinct
|
||||
for CONTENT in "$(printf '22\nnode-dist=x\nnode.dist=y')" "$(printf '22\nabc=x\na.c=y')" "$(printf '22\nab=x\na+b=y')"; do
|
||||
try nvm_process_nvmrc_content "${CONTENT}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = '0' ] || die "distinct keys were rejected as duplicates in >${CONTENT}<; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
[ "${CAPTURED_STDOUT}" = '22' ] || die "expected the version 22 from >${CONTENT}<; got >${CAPTURED_STDOUT}<"
|
||||
done
|
||||
|
||||
# keys with regex metacharacters are not passed to grep
|
||||
try_err nvm_process_nvmrc_content "$(printf '22\na(=x\nb[=y')"
|
||||
[ "${CAPTURED_EXIT_CODE}" = '0' ] || die "keys with regex metacharacters were rejected; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
[ -z "${CAPTURED_STDERR}" ] || die "expected no errors for keys with regex metacharacters; got >${CAPTURED_STDERR}<"
|
||||
|
||||
# duplicates are still rejected, including keys with regex metacharacters
|
||||
for CONTENT in "$(printf '22\na=1\na=2')" "$(printf '22\na.c=1\na.c=2')" "$(printf '22\n[x=1\n[x=2')"; do
|
||||
try_err nvm_process_nvmrc_content "${CONTENT}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = '1' ] || die "duplicate keys were accepted in >${CONTENT}<; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
case "${CAPTURED_STDERR}" in
|
||||
'invalid .nvmrc!'*) ;;
|
||||
*) die "expected the invalid .nvmrc message for >${CONTENT}<; got >${CAPTURED_STDERR}<" ;;
|
||||
esac
|
||||
done
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
for KEY in a a.b 'a*' '(' '[' 'a\' 'a b'; do
|
||||
CONTENT="20
|
||||
${KEY}=1
|
||||
other=2
|
||||
${KEY} = 3"
|
||||
|
||||
try_err nvm_process_nvmrc_content "${CONTENT}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected a duplicate key \`${KEY}\` to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
case "${CAPTURED_STDERR}" in
|
||||
*'invalid .nvmrc!'*) ;;
|
||||
*) die "expected the invalid .nvmrc message for a duplicate key \`${KEY}\`; got >${CAPTURED_STDERR}<" ;;
|
||||
esac
|
||||
|
||||
try nvm_process_nvmrc_content "${CONTENT}"
|
||||
[ -z "${CAPTURED_STDOUT}" ] || die "expected no version for a duplicate key \`${KEY}\`; got \`${CAPTURED_STDOUT}\`"
|
||||
done
|
||||
|
||||
# keys are compared as they are written, not as patterns
|
||||
for KEY in b ab A a. '*' '.*' '?' 'a|b' '[a]' 'a b' 'b a' 'b a'; do
|
||||
for CONTENT in "20
|
||||
a=1
|
||||
${KEY}=2" "20
|
||||
${KEY}=1
|
||||
a=2"; do
|
||||
try_err nvm_process_nvmrc_content "${CONTENT}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected the keys \`a\` and \`${KEY}\` to be distinct; got exit code ${CAPTURED_EXIT_CODE}"
|
||||
case "${CAPTURED_STDERR}" in
|
||||
*'invalid .nvmrc!'*) die "expected no error for the keys \`a\` and \`${KEY}\`; got >${CAPTURED_STDERR}<" ;;
|
||||
esac
|
||||
|
||||
try nvm_process_nvmrc_content "${CONTENT}"
|
||||
[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` with the keys \`a\` and \`${KEY}\`; got \`${CAPTURED_STDOUT}\`"
|
||||
done
|
||||
done
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/bin/sh
|
||||
|
||||
ORIG_NVM_DIR="${NVM_DIR}"
|
||||
TMP_DIR=''
|
||||
|
||||
cleanup () {
|
||||
case "${TMP_DIR}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
NVM_DIR="${ORIG_NVM_DIR}"
|
||||
export NVM_DIR
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
NVM_DIR="${TMP_DIR}/nvm"
|
||||
export NVM_DIR
|
||||
mkdir -p "${NVM_DIR}" || die 'could not create NVM_DIR'
|
||||
|
||||
[ "_$(nvm_resolve_lts_name on)" = '_on' ] || die "without recorded LTS status, the name should be kept; got >$(nvm_resolve_lts_name on)<"
|
||||
|
||||
row () {
|
||||
printf '%s\t2024-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2"
|
||||
}
|
||||
# not newest first, so the newest codename has to come from the versions
|
||||
{ row v20.19.0 Iron; row v26.12.1 -; row v24.11.0 Krypton; row v4.9.1 Argon; row v24.0.0 -; row v22.11.0 Jod; row v8.17.0 Carbon; } | nvm_record_lts_status || die 'could not record LTS status'
|
||||
|
||||
check () {
|
||||
ACTUAL="$(nvm_resolve_lts_name "$1")"
|
||||
[ "_${ACTUAL}" = "_$2" ] || die "\`nvm_resolve_lts_name '$1'\` should print >$2<; got >${ACTUAL}<"
|
||||
}
|
||||
|
||||
# the newest codename containing the name
|
||||
check on krypton
|
||||
check o krypton
|
||||
check ar carbon
|
||||
check arg argon
|
||||
check ir iron
|
||||
check iron iron
|
||||
check j jod
|
||||
# not a regex, and not a codename yet, so kept as given
|
||||
check . .
|
||||
check 'i.on' 'i.on'
|
||||
check '(' '('
|
||||
check nosuch nosuch
|
||||
# names nvm resolves some other way
|
||||
check '*' '*'
|
||||
check -1 -1
|
||||
check '' ''
|
||||
# LTS names are lowercase; an uppercase one is kept, for `nvm_normalize_lts` to reject
|
||||
check Iron Iron
|
||||
|
||||
cleanup
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
\. ../../../nvm.sh
|
||||
|
||||
yes_creds() {
|
||||
nvm_url_has_credentials "${1}" || die "nvm_url_has_credentials '${1}': expected success"
|
||||
}
|
||||
|
||||
no_creds() {
|
||||
! nvm_url_has_credentials "${1}" || die "nvm_url_has_credentials '${1}': expected failure"
|
||||
}
|
||||
|
||||
yes_creds 'https://user:pw@mirror.example/dist'
|
||||
yes_creds 'http://user:pw@127.0.0.1:8080/dist/index.tab'
|
||||
yes_creds 'https://user@mirror.example'
|
||||
yes_creds 'https://user:pw@[::1]:8080/dist'
|
||||
yes_creds 'https://user:p@ss@mirror.example/dist'
|
||||
yes_creds 'https://user:pw@mirror.example/a"b\c'
|
||||
|
||||
no_creds 'https://nodejs.org/dist/index.tab'
|
||||
no_creds 'https://mirror.example/dist/@scope/index.tab'
|
||||
no_creds 'https://mirror.example/user:pw@elsewhere.example/dist'
|
||||
no_creds ''
|
||||
no_creds '-o'
|
||||
no_creds 'user:pw@mirror.example/dist'
|
||||
no_creds 'ftp://user:pw@mirror.example/dist'
|
||||
no_creds '/tmp/user:pw@mirror.example/node.tar.xz'
|
||||
no_creds 'https://user:pw@mirror.example/a b'
|
||||
no_creds "$(printf 'https://user:pw@mirror.example/a\nb')"
|
||||
no_creds "$(printf 'https://user:pw@mirror.example/a\rb')"
|
||||
no_creds "$(printf 'https://user:pw@mirror.example/a\033b')"
|
||||
@@ -78,3 +78,65 @@ for PATTERN in v1.2 v1 1.2.3 v1.2.3- v1.2.3-.x v1.2.3-a..b v1.2.3.4 v1.2.3.; do
|
||||
[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_version $PATTERN did not exit 0; got $CAPTURED_EXIT_CODE"
|
||||
done
|
||||
unset -f nvm_is_version_installed
|
||||
|
||||
# `node`/`stable` is the newest installed version, from one listing, once that is 1.0 or later
|
||||
nvm_ls() {
|
||||
case "$1" in
|
||||
node) printf 'v0.12.18\nv20.0.0\nv22.1.0\n' ;;
|
||||
*) die "\"nvm_version node\" must list the installed versions only once; got \"nvm_ls $1\"" ;;
|
||||
esac
|
||||
}
|
||||
for PATTERN in node node- stable; do
|
||||
try nvm_version "$PATTERN"
|
||||
[ "_$CAPTURED_STDOUT" = "_v22.1.0" ] || die "\"nvm_version $PATTERN\" did not return the newest installed version; got $CAPTURED_STDOUT"
|
||||
[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "\"nvm_version $PATTERN\" did not exit 0; got $CAPTURED_EXIT_CODE"
|
||||
done
|
||||
|
||||
# a system node, which `nvm_ls` lists last, is not the newest installed version
|
||||
nvm_ls() {
|
||||
case "$1" in
|
||||
node) printf 'v20.0.0\nv22.1.0\nsystem v19.0.0\n' ;;
|
||||
*) die "\"nvm_version node\" with a system node must list the installed versions only once; got \"nvm_ls $1\"" ;;
|
||||
esac
|
||||
}
|
||||
try nvm_version node
|
||||
[ "_$CAPTURED_STDOUT" = "_v22.1.0" ] || die '"nvm_version node" with a system node did not skip it; got '"$CAPTURED_STDOUT"
|
||||
|
||||
# with only 0.x versions installed, `stable` is the newest even minor, so it falls back to `nvm_ls stable`
|
||||
nvm_ls() {
|
||||
case "$1" in
|
||||
node) printf 'v0.10.48\nv0.11.16\n' ;;
|
||||
*) echo "pattern: $1" ;;
|
||||
esac
|
||||
}
|
||||
try nvm_version node
|
||||
[ "_$CAPTURED_STDOUT" = "_pattern: stable" ] || die '"nvm_version node" with only 0.x versions did not fall back to "nvm_ls stable"; got '"$CAPTURED_STDOUT"
|
||||
|
||||
# nothing installed falls back too, so the result and exit code are unchanged
|
||||
nvm_ls() {
|
||||
case "$1" in
|
||||
node) echo 'N/A'; return 3 ;;
|
||||
*) echo 'N/A'; return 3 ;;
|
||||
esac
|
||||
}
|
||||
try nvm_version node
|
||||
[ "_$CAPTURED_STDOUT" = "_N/A" ] || die '"nvm_version node" with nothing installed did not return N/A; got '"$CAPTURED_STDOUT"
|
||||
[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die '"nvm_version node" with nothing installed did not exit 3; got '"$CAPTURED_EXIT_CODE"
|
||||
|
||||
# an alias named `stable` takes precedence over the newest installed version
|
||||
nvm_ls() {
|
||||
case "$1" in
|
||||
node) printf 'v20.0.0\nv22.1.0\n' ;;
|
||||
*) echo "pattern: $1" ;;
|
||||
esac
|
||||
}
|
||||
OLD_NVM_DIR="${NVM_DIR}"
|
||||
NVM_DIR="$(mktemp -d)"
|
||||
mkdir -p "${NVM_DIR}/alias"
|
||||
echo v20.0.0 > "${NVM_DIR}/alias/stable"
|
||||
try nvm_version node
|
||||
rm -rf "${NVM_DIR}"
|
||||
NVM_DIR="${OLD_NVM_DIR}"
|
||||
[ "_$CAPTURED_STDOUT" = "_pattern: stable" ] || die '"nvm_version node" did not let an alias named "stable" take precedence; got '"$CAPTURED_STDOUT"
|
||||
|
||||
unset -f nvm_ls
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
cd "${ORIG_PWD}" || :
|
||||
PATH="${REAL_PATH:-${PATH}}"
|
||||
nvm deactivate > /dev/null 2>&1
|
||||
rm -rf "${TEST_DIR-}" "${NVM_DIR}/${TEST_VERSION}"
|
||||
unset TEST_DIR TEST_VERSION ORIG_PWD REAL_PATH SWAP_TARGET
|
||||
}
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
ORIG_PWD="${PWD}"
|
||||
TEST_VERSION='v0.2.4'
|
||||
make_fake_node "${TEST_VERSION}" || die 'failed to create a fake node'
|
||||
|
||||
TEST_DIR="${PWD}/nvm_write_nvmrc_replace_tmp"
|
||||
mkdir -p "${TEST_DIR}/project" "${TEST_DIR}/bin" "${TEST_DIR}/dir" || die 'failed to create test dirs'
|
||||
|
||||
# stand-ins for `tee` and `mv` that turn .nvmrc into a symlink just before the real command runs,
|
||||
# as another user who can write the directory could
|
||||
for CMD in tee mv; do
|
||||
printf '%s\n' '#!/bin/sh' \
|
||||
'rm -rf "${PWD}/.nvmrc" && ln -s "${SWAP_TARGET}" "${PWD}/.nvmrc" || exit 99' \
|
||||
'PATH="${REAL_PATH}"' \
|
||||
"exec ${CMD} \"\$@\"" > "${TEST_DIR}/bin/${CMD}"
|
||||
chmod +x "${TEST_DIR}/bin/${CMD}"
|
||||
done
|
||||
|
||||
cd "${TEST_DIR}/project" || die 'failed to enter the project dir'
|
||||
export REAL_PATH="${PATH}"
|
||||
|
||||
# 1. a symlink to a file, swapped in after the check, does not get written through
|
||||
echo 'KEEPME' > "${TEST_DIR}/target"
|
||||
export SWAP_TARGET="${TEST_DIR}/target"
|
||||
echo 'v0.0.1' > .nvmrc
|
||||
PATH="${TEST_DIR}/bin:${REAL_PATH}" nvm_write_nvmrc "${TEST_VERSION}" > /dev/null 2>&1
|
||||
PATH="${REAL_PATH}"
|
||||
[ "$(cat "${TEST_DIR}/target")" = 'KEEPME' ] || die "expected a symlink swapped in after the check to not be written through; target is now \`$(cat "${TEST_DIR}/target")\`"
|
||||
|
||||
# 2. nor does a symlink to a directory get the file moved into it
|
||||
rm -rf .nvmrc
|
||||
export SWAP_TARGET="${TEST_DIR}/dir"
|
||||
echo 'v0.0.1' > .nvmrc
|
||||
PATH="${TEST_DIR}/bin:${REAL_PATH}" nvm_write_nvmrc "${TEST_VERSION}" > /dev/null 2>&1
|
||||
PATH="${REAL_PATH}"
|
||||
[ -z "$(ls -A "${TEST_DIR}/dir")" ] || die "expected nothing to be written into a symlinked directory; found \`$(ls -A "${TEST_DIR}/dir")\`"
|
||||
[ "$(cat "${TEST_DIR}/target")" = 'KEEPME' ] || die 'expected the target to still be unchanged'
|
||||
|
||||
# 3. a hard link to .nvmrc keeps its content
|
||||
rm -rf .nvmrc
|
||||
echo 'KEEPME' > "${TEST_DIR}/hardlinked"
|
||||
ln "${TEST_DIR}/hardlinked" .nvmrc || die 'failed to create the hard link'
|
||||
try_err nvm_write_nvmrc "${TEST_VERSION}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected exit code 0; got ${CAPTURED_EXIT_CODE}: ${CAPTURED_STDERR}"
|
||||
[ "$(cat .nvmrc)" = "${TEST_VERSION}" ] || die "expected .nvmrc to contain ${TEST_VERSION}; got \`$(cat .nvmrc)\`"
|
||||
[ "$(cat "${TEST_DIR}/hardlinked")" = 'KEEPME' ] || die "expected a hard link to .nvmrc to be left alone; got \`$(cat "${TEST_DIR}/hardlinked")\`"
|
||||
|
||||
# 4. a new .nvmrc gets its mode from the umask
|
||||
rm -f .nvmrc
|
||||
(
|
||||
umask 027
|
||||
nvm_write_nvmrc "${TEST_VERSION}" > /dev/null 2>&1
|
||||
) || die 'expected writing a new .nvmrc to succeed'
|
||||
MODE="$(ls -l .nvmrc | cut -c1-10)"
|
||||
[ "${MODE}" = '-rw-r-----' ] || die "expected .nvmrc to have mode -rw-r----- under umask 027; got ${MODE}"
|
||||
|
||||
# 5. a directory named .nvmrc is an error, and nothing is moved into it
|
||||
rm -f .nvmrc
|
||||
mkdir .nvmrc
|
||||
try_err nvm_write_nvmrc "${TEST_VERSION}"
|
||||
[ "${CAPTURED_EXIT_CODE}" = 3 ] || die "expected exit code 3 for a directory .nvmrc; got ${CAPTURED_EXIT_CODE}"
|
||||
[ "${CAPTURED_STDERR}" = "Warning: Unable to write version number (${TEST_VERSION}) to .nvmrc" ] || die "expected the unable-to-write warning; got \`${CAPTURED_STDERR}\`"
|
||||
[ -z "$(ls -A .nvmrc)" ] || die "expected nothing to be moved into a directory .nvmrc; found \`$(ls -A .nvmrc)\`"
|
||||
rmdir .nvmrc
|
||||
|
||||
# 6. no temporary directories are left behind
|
||||
LEFTOVER="$(ls -A "${NVM_DIR}" | grep '^\.nvmrc\.')"
|
||||
[ -z "${LEFTOVER}" ] || die "expected no temporary directories left in \$NVM_DIR; found \`${LEFTOVER}\`"
|
||||
|
||||
cleanup
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
|
||||
NVM_ENV=testing \. ../../install.sh
|
||||
|
||||
die() { echo "$*" >&2; exit 1; }
|
||||
TEST_ROOT="$(mktemp -d)" || die 'could not create test directory'
|
||||
trap 'rm -rf "${TEST_ROOT}"' EXIT HUP INT TERM
|
||||
SOURCE="${TEST_ROOT}/source"
|
||||
git init -q "${SOURCE}" || die 'could not initialize source'
|
||||
git -C "${SOURCE}" -c user.name=test -c user.email=test@example.invalid commit --allow-empty -qm fixture || die 'could not commit fixture'
|
||||
git -C "${SOURCE}" tag v0.0.1 || die 'could not tag fixture'
|
||||
nvm_source() { printf '%s\n' "${SOURCE}"; }
|
||||
NVM_INSTALL_VERSION=v0.0.1
|
||||
export NVM_INSTALL_VERSION
|
||||
|
||||
NVM_DIR="${TEST_ROOT}/fresh"
|
||||
export NVM_DIR
|
||||
install_nvm_from_git || die 'fresh install failed'
|
||||
[ "$(git -C "${NVM_DIR}" remote get-url origin)" = "${SOURCE}" ] || die 'fresh install did not retain origin'
|
||||
|
||||
NVM_DIR="${TEST_ROOT}/existing"
|
||||
export NVM_DIR
|
||||
git init -q "${NVM_DIR}" || die 'could not initialize existing install'
|
||||
git -C "${NVM_DIR}" remote add fork "${TEST_ROOT}/wrong" || die 'could not add fork'
|
||||
git -C "${NVM_DIR}" remote add origin "${SOURCE}" || die 'could not add origin'
|
||||
install_nvm_from_git || die 'existing install did not select matching origin'
|
||||
[ "$(git -C "${NVM_DIR}" remote get-url fork)" = "${TEST_ROOT}/wrong" ] || die 'fork remote changed'
|
||||
|
||||
git -C "${NVM_DIR}" remote set-url origin "${TEST_ROOT}/wrong-origin" || die 'could not change origin'
|
||||
git -C "${NVM_DIR}" remote add nvm-origin "${TEST_ROOT}/occupied" || die 'could not occupy alternate name'
|
||||
install_nvm_from_git || die 'existing install did not add a matching remote'
|
||||
[ "$(git -C "${NVM_DIR}" remote get-url nvm-origin-1)" = "${SOURCE}" ] || die 'new remote missing'
|
||||
[ "$(git -C "${NVM_DIR}" remote get-url origin)" = "${TEST_ROOT}/wrong-origin" ] || die 'origin remote changed'
|
||||
@@ -4,13 +4,13 @@ cleanup () {
|
||||
rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1
|
||||
unset npm_config_prefix
|
||||
|
||||
rm -f npm
|
||||
[ -z "${FAKE_NPM_DIR-}" ] || rm -rf "${FAKE_NPM_DIR}"
|
||||
[ -z "${WINDOWS_NPM_DIR-}" ] || rm -rf "${WINDOWS_NPM_DIR}"
|
||||
PATH="$ORIGINAL_PATH"
|
||||
NVM_DIR="$ORIGINAL_NVM_DIR"
|
||||
|
||||
unset -f setup cleanup die
|
||||
unset message ORIGINAL_PATH ORIGINAL_NVM_DIR WINDOWS_NPM_DIR
|
||||
unset message ORIGINAL_PATH ORIGINAL_NVM_DIR FAKE_NPM_DIR WINDOWS_NPM_DIR
|
||||
}
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
@@ -55,9 +55,10 @@ message=$(nvm_check_global_modules)
|
||||
[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module; got:\n${message}"
|
||||
|
||||
# Faking the absence of npm
|
||||
PATH=".:$PATH"
|
||||
touch npm
|
||||
chmod +x npm
|
||||
FAKE_NPM_DIR="$(mktemp -d)"
|
||||
touch "${FAKE_NPM_DIR}/npm"
|
||||
chmod +x "${FAKE_NPM_DIR}/npm"
|
||||
PATH="${FAKE_NPM_DIR}:$PATH"
|
||||
|
||||
message=$(nvm_check_global_modules)
|
||||
[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm was unavailable; got:\n${message}"
|
||||
@@ -83,5 +84,20 @@ nvm_is_wsl() { return 1; }
|
||||
message=$(nvm_check_global_modules)
|
||||
[ -n "$message" ] || die 'nvm_check_global_modules should still warn about a Windows npm outside WSL, where it is the system node'
|
||||
|
||||
# npm must not apply the npmrc of the project that `$PWD` is in
|
||||
cat <<'SH' >"${FAKE_NPM_DIR}/npm"
|
||||
#!/bin/sh
|
||||
case "$1" in
|
||||
--version) echo '10.0.0' ;;
|
||||
list) printf '%s\n' 'npm@10.0.0' "└── cwd@[$(pwd)]" ;;
|
||||
esac
|
||||
SH
|
||||
PATH="${FAKE_NPM_DIR}:${ORIGINAL_PATH}"
|
||||
message=$(nvm_check_global_modules)
|
||||
case "$message" in
|
||||
*"cwd@[/]"*) ;;
|
||||
*) die "nvm_check_global_modules should run npm from \`/\`; got:\n${message}" ;;
|
||||
esac
|
||||
|
||||
|
||||
cleanup
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
if [ "$#" -eq 2 ] && [ "$1" = "get" ] && [ "$2" = "const.ohos.fullname" ]; then
|
||||
echo "OpenHarmony-6.1.0.31"
|
||||
fi
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
if [ "$#" -eq 2 ] && [ "$1" = "get" ] && [ "$2" = "const.ohos.fullname" ]; then
|
||||
echo "NotOpenHarmony-1.2.3"
|
||||
fi
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
if [ "_$1" = "_-m" ]; then
|
||||
echo "aarch64"
|
||||
else
|
||||
echo "HarmonyOS localhost HongMeng Kernel 1.12.0 #1 SMP Tue Jul 14 09:40:51 UTC 2026 aarch64 Toybox"
|
||||
fi
|
||||
Reference in new issue
Block a user