Skip to content

Commit

Permalink
Add support for opt-in download to exec and run (tj#685)
Browse files Browse the repository at this point in the history
* Rework argument parsing to allow options after exec and run

* Add option for run and exec to download missing version

* Reword download to cover wider functionality

* Be consistent with inner spacing of arrays

* Add download test for run and exec
  • Loading branch information
shadowspawn authored Sep 10, 2021
1 parent 84ae1d7 commit c32652f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
34 changes: 23 additions & 11 deletions bin/n
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ if [ -n "$HTTP_USER" ];then
if [ -z "$HTTP_PASSWORD" ]; then
abort "Must specify HTTP_PASSWORD when supplying HTTP_USER"
fi
CURL_OPTIONS+=("-u $HTTP_USER:$HTTP_PASSWORD")
WGET_OPTIONS+=("--http-password=$HTTP_PASSWORD"
"--http-user=$HTTP_USER")
CURL_OPTIONS+=( "-u $HTTP_USER:$HTTP_PASSWORD" )
WGET_OPTIONS+=( "--http-password=$HTTP_PASSWORD"
"--http-user=$HTTP_USER" )
elif [ -n "$HTTP_PASSWORD" ]; then
abort "Must specify HTTP_USER when supplying HTTP_PASSWORD"
fi
Expand All @@ -128,7 +128,7 @@ g_active_node=
# set by various lookups to allow mixed logging and return value from function, especially for engine and node
g_target_node=

ACTIVATE=true
DOWNLOAD=false # set to opt-out of activate (install), and opt-in to download (run, exec)
ARCH=
SHOW_VERBOSE_LOG="true"

Expand Down Expand Up @@ -387,7 +387,7 @@ Options:
-h, --help Display help information
-p, --preserve Preserve npm and npx during install of Node.js
-q, --quiet Disable curl output. Disable log messages processing "auto" and "engine" labels.
-d, --download Download only
-d, --download Download if necessary, and don't make active
-a, --arch Override system architecture
--all ls-remote displays all matches instead of last 20
--insecure Turn off certificate checking for https requests (may be needed from behind a proxy server)
Expand Down Expand Up @@ -764,7 +764,7 @@ install() {

if test -d "$dir"; then
if [[ ! -e "$dir/n.lock" ]] ; then
if "$ACTIVATE" ; then
if [[ "$DOWNLOAD" == "false" ]] ; then
activate "${g_mirror_folder_name}/${version}"
fi
exit
Expand Down Expand Up @@ -792,7 +792,7 @@ install() {

disable_pax_mprotect bin/node

if "$ACTIVATE" ; then
if [[ "$DOWNLOAD" == "false" ]]; then
activate "${g_mirror_folder_name}/$version"
fi
}
Expand Down Expand Up @@ -893,6 +893,9 @@ function find_cached_version() {

update_mirror_settings_for_version "$1"
g_cached_version="${CACHE_DIR}/${g_mirror_folder_name}/${version}"
if [[ ! -d "${g_cached_version}" && "${DOWNLOAD}" == "true" ]]; then
install "${version}"
fi
[[ -d "${g_cached_version}" ]] || abort "'$1' (${version}) not in downloads cache"
}

Expand Down Expand Up @@ -1527,14 +1530,15 @@ function show_diagnostics() {
# which feel pretty natural.

unprocessed_args=()
positional_arg="false"

while [[ $# -ne 0 ]]; do
case "$1" in
--all) N_MAX_REMOTE_MATCHES=32000 ;;
-V|--version) display_n_version ;;
-h|--help|help) display_help; exit ;;
-q|--quiet) set_quiet ;;
-d|--download) ACTIVATE=false ;;
-d|--download) DOWNLOAD="true" ;;
--insecure) set_insecure ;;
-p|--preserve) N_PRESERVE_NPM="true" ;;
--no-preserve) N_PRESERVE_NPM="" ;;
Expand All @@ -1544,9 +1548,17 @@ while [[ $# -ne 0 ]]; do
--stable) display_remote_versions lts; exit ;; # [sic] old terminology
--lts) display_remote_versions lts; exit ;;
-a|--arch) shift; set_arch "$1";; # set arch and continue
exec) unprocessed_args=("$@"); break ;;
run|as|use) unprocessed_args=("$@"); break ;;
*) unprocessed_args+=( "$1" ) ;;
exec|run|as|use)
unprocessed_args+=( "$1" )
positional_arg="true"
;;
*)
if [[ "${positional_arg}" == "true" ]]; then
unprocessed_args+=( "$@" )
break
fi
unprocessed_args+=( "$1" )
;;
esac
shift
done
Expand Down
26 changes: 16 additions & 10 deletions test/tests/run-which.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ function setup_file() {
export N_PREFIX="${tmpdir}/n/test/run-which"
n --download 4.9.1
n --download lts
# using "latest" for download tests with run and exec
}

function teardown_file() {
rm -rf "${N_PREFIX}"
}


@test "setupAll for run/which/exec # (2 installs)" {
# Dummy test so setupAll displayed while running first setup
[ -d "${N_PREFIX}/n/versions/node/4.9.1" ]
Expand All @@ -37,19 +37,16 @@ function teardown_file() {
assert_equal "$output" "${N_PREFIX}/n/versions/node/4.9.1/bin/node"
}


@test "n bin v4.9.1" {
output="$(n bin v4.9.1)"
assert_equal "$output" "${N_PREFIX}/n/versions/node/4.9.1/bin/node"
}


@test "n which argon" {
output="$(n which argon)"
assert_equal "$output" "${N_PREFIX}/n/versions/node/4.9.1/bin/node"
}


@test "n which lts" {
output="$(n which lts)"
local LTS_VERSION="$(display_remote_version lts)"
Expand All @@ -64,26 +61,29 @@ function teardown_file() {
assert_equal "$output" "v4.9.1"
}


@test "n run lts" {
output="$(n run lts --version)"
local LTS_VERSION="$(display_remote_version lts)"
assert_equal "$output" "v${LTS_VERSION}"
}


@test "n use 4" {
output="$(n use 4 --version)"
assert_equal "$output" "v4.9.1"
}


@test "n as 4" {
output="$(n as 4 --version)"
assert_equal "$output" "v4.9.1"
}


@test "n run --download latest" {
n rm latest || true
n run --download latest --version
output="$(n run latest --version)"
local LATEST_VERSION="$(display_remote_version latest)"
assert_equal "$output" "v${LATEST_VERSION}"
}


# n exec
Expand All @@ -93,15 +93,21 @@ function teardown_file() {
assert_equal "$output" "v4.9.1"
}


@test "n exec 4 npm" {
output="$(n exec 4 npm --version)"
assert_equal "$output" "2.15.11"
}


@test "n exec lts" {
output="$(n exec lts node --version)"
local LTS_VERSION="$(display_remote_version lts)"
assert_equal "$output" "v${LTS_VERSION}"
}

@test "n exec -d latest" {
n rm latest || true
n exec -d latest node --version
output="$(n exec latest node --version)"
local LATEST_VERSION="$(display_remote_version latest)"
assert_equal "$output" "v${LATEST_VERSION}"
}

0 comments on commit c32652f

Please sign in to comment.