From 59adcab8b4d7d57233150b3252c65cc7ce9ebe56 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sat, 11 Apr 2020 14:10:29 +1200 Subject: [PATCH] Add auto label to read version from .node-version (#616) * Add support for auto, reading version from .node-version * Make version unique in tests * Add support for auto doing a lookup for unresolved version * Make auto description more consistent in style with other labels. --- CHANGELOG.md | 6 +++ README.md | 4 ++ bin/n | 34 ++++++++++++---- test/tests/run-which.bats | 3 +- test/tests/version-resolve.bats | 69 +++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 test/tests/version-resolve.bats diff --git a/CHANGELOG.md b/CHANGELOG.md index 09fcd80..e36d39d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] (date goes here) +## [6.5.0] (2020-04-11) + +### Added + +- specify `auto` to read the target version from a ` .node-version` file + ## [6.4.0] (2020-03-10) ### Added diff --git a/README.md b/README.md index 0e41b3c..8d1ac2e 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,10 @@ There are labels for two especially useful versions: - `lts`: newest Long Term Support official release - `latest`, `current`: newest official release + +There is a label to read the target version from a file, on the first line: + +- `auto`: read version from `.node-version` file There is support for release streams: diff --git a/bin/n b/bin/n index d415241..efc6be8 100755 --- a/bin/n +++ b/bin/n @@ -40,7 +40,7 @@ function echo_red() { # Setup and state # -VERSION="6.4.1-0" +VERSION="6.5.0" N_PREFIX="${N_PREFIX-/usr/local}" N_PREFIX=${N_PREFIX%/} @@ -334,6 +334,7 @@ Versions: 4.9.1, 8, v6.1 Numeric versions lts Newest Long Term Support official release latest, current Newest official release + auto Read version from .node-version boron, carbon Codenames for release streams and nightly, chakracore-release/latest, rc/10 et al @@ -890,19 +891,37 @@ function tarball_url() { echo "${g_mirror_url}/v${version}/node-v${version}-$(display_tarball_platform).tar.${ext}" } +# +# Synopsis: display_auto_version +# + +function display_auto_version() { + local filename=".node-version" + [[ -e "${filename}" ]] || abort "auto version file not found (${filename})" + # read returns a non-zero status but does still work if there is no line ending + <"${filename}" read -r version + # trim possible trailing \d from a Windows created file + version="${version%%[[:space:]]}" + echo "${version}" +} + # # Synopsis: display_latest_resolved_version version # function display_latest_resolved_version() { - local version=${1#node/} - if is_exact_numeric_version "${version}"; then + local version=${1} + if [[ "${version}" = "auto" ]]; then + version="$(display_auto_version)" || return 2 + fi + simple_version=${version#node/} # Only place supporting node/ [sic] + if is_exact_numeric_version "${simple_version}"; then # Just numbers, already resolved, no need to lookup first. - version="${version#v}" - echo "${version}" + simple_version="${simple_version#v}" + echo "${simple_version}" else - # Complicated recognising exact version, KISS and awlays lookup for now. - N_MAX_REMOTE_MATCHES=1 display_remote_versions "$1" + # Complicated recognising exact version, KISS and lookup. + N_MAX_REMOTE_MATCHES=1 display_remote_versions "$version" fi } @@ -1295,6 +1314,7 @@ else lsr|ls-remote|list-remote) shift; display_remote_versions "$1"; exit ;; uninstall) uninstall_installed; exit ;; i|install) shift; install "$1"; exit ;; + N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION) shift; display_latest_resolved_version "$1"; exit ;; *) install "$1"; exit ;; esac shift diff --git a/test/tests/run-which.bats b/test/tests/run-which.bats index ee7af52..57ce371 100644 --- a/test/tests/run-which.bats +++ b/test/tests/run-which.bats @@ -5,7 +5,8 @@ load shared-functions function setup() { unset_n_env # fixed directory so can reuse the two installs - export N_PREFIX="${TMPDIR}/n/test/run-which" + tmpdir="${TMPDIR:-/tmp}" + export N_PREFIX="${tmpdir}/n/test/run-which" # beforeAll # See https://github.com/bats-core/bats-core/issues/39 if [[ "${BATS_TEST_NUMBER}" -eq 1 ]] ; then diff --git a/test/tests/version-resolve.bats b/test/tests/version-resolve.bats new file mode 100644 index 0000000..89e3223 --- /dev/null +++ b/test/tests/version-resolve.bats @@ -0,0 +1,69 @@ +#!/usr/bin/env bats + +# Note: full semver is resolved without lookup, so can use arbitrary versions for testing like 999.999.999 + +load shared-functions + + +function setup() { + unset_n_env + tmpdir="${TMPDIR:-/tmp}" + export MY_DIR="${tmpdir}/n/test/run-version-resolve" + mkdir -p "${MY_DIR}" +} + +@test "auto, missing file" { + cd "${MY_DIR}" + rm -f .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -ne 0 ] +} + +@test "auto, no eol" { + cd "${MY_DIR}" + printf "101.0.1" > .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -eq 0 ] + [ "$output" = "101.0.1" ] +} + +@test "auto, unix eol" { + cd "${MY_DIR}" + printf "101.0.2\n" > .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -eq 0 ] + [ "$output" = "101.0.2" ] +} + +@test "auto, Windows eol" { + cd "${MY_DIR}" + printf "101.0.3\r\n" > .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -eq 0 ] + [ "$output" = "101.0.3" ] +} + +@test "auto, leading v" { + cd "${MY_DIR}" + printf "v101.0.4\n" > .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -eq 0 ] + [ "$output" = "101.0.4" ] +} + +@test "auto, first line only" { + cd "${MY_DIR}" + printf "101.0.5\nmore text\n" > .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -eq 0 ] + [ "$output" = "101.0.5" ] +} + +@test "auto, lookup" { + # Check normal resolving, which is allowed but not required for MVP .node-version + cd "${MY_DIR}" + printf "4.9\n" > .node-version + run n N_MOCK_DISPLAY_LATEST_RESOLVED_VERSION auto + [ "$status" -eq 0 ] + [ "$output" = "4.9.1" ] +}