Skip to content

Commit

Permalink
Test improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
mklement0 committed Mar 12, 2021
1 parent 479c59c commit c1cf904
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
$(if $(findstring /,$(MAKEFILE_LIST)),$(error Please only invoke this makefile from the directory it resides in))
# Run all shell commands with bash.
SHELL := bash
export N_PREFIX :=
# Add the following to the PATH:
# !! Note that this extended path only takes effect in (a) recipe commands that are (b) true shell commands (not optimized away) - when in doubt, simply append ';'
# !! To also use the extended path in $(shell ...) function calls, use $(shell PATH="$(PATH)" ...).
export PATH := ./node_modules/.bin:$(PATH) # !! So as to make this work in PowerShell too, use './' to start the path, NOT $(PWD)
# * ./node_modules/.bin, so that helper npm modules used by this Makefile, such as `json`, are found.
# * !! Additionally, we add $N_PREFIX/bin to the path to make sure that `node` is available for targets such as `verinfo`
# !! ?? Inexplicably, this $PATH addition, which the caller already does see, is NOT available when shell commands are run here. Why?
export PATH := $(N_PREFIX)/bin:./node_modules/.bin:$(PATH) # !! So as to make this work in PowerShell too, use './' to start the path, NOT $(PWD)
# Sanity check: git repo must exist.
$(if $(shell [[ -d .git ]] && echo ok),,$(error No git repo found in current dir. Please at least initialize one with 'git init'))
# Sanity check: make sure dev dependencies (and npm) are installed - skip this check only for certain generic targets (':' is the pseudo target used by the `list` target's recipe.)
Expand Down Expand Up @@ -135,8 +137,9 @@ update-checksums:
@util/update-checksums

# make release [VER=<newVerSpec>] [NOTEST=1]
# Increments the version number, runs tests, then commits and tags, pushes to origin, prompts to publish to the npm-registry; NOTEST=1 skips tests.
# VER=<newVerSpec> is mandatory, unless the version number in package.json is ahead of the latest Git version tag.
# Increments the version number, runs tests, then commits and tags, pushes to origin, prompts to publish to the npm-registry, unless the package is *private*;
# NOTEST=1 skips tests.
# VER=<newVerSpec> may be passed on the command line; otherwise, it is prompted for.
.PHONY: release
release: _need-origin _need-npm-credentials _need-master-branch _need-clean-ws-or-no-untracked-files version test
@newVer=`json -f package.json version` || exit; [[ $$newVer == *-* ]] && isPreRelease=1 || isPreRelease=0; \
Expand Down
9 changes: 6 additions & 3 deletions bin/n-install
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ kMIN_BASH_VERSION='3.2' # due to use of =~, we require at least 3.2

##### IMPORTANT: These names must, at least in part, be kept in sync with their counterparts in 'n-update' and 'n-uninstall'.
kINSTALLER_NAME=n-install # This script's name; note that since we'll typically be running via curl directly from GitHub, using $(basename "$BASH_SOURCE") to determine the name is not an option.
# Note: The actual *installation* URL is http://git.io/n-install which redirects to https://raw.githubusercontent.com/mklement0/n-install/stable/bin/n-install
kTHIS_REPO_URL='https://git.io/n-install-repo' # This script's source repository - SHORT, git.io-based form.
kTHIS_REPO_URL_LONG='https://github.com/mklement0/n-install' # This script's source repository in LONG form - needed for deriving raw.githubusercontent.com URLs from it.
kPREFIX_DIR=${N_PREFIX:-$HOME/n} # The target prefix directory, inside which both a dir. for n itself and the active node version's dirs. will be located.
Expand All @@ -35,9 +36,11 @@ kHELPER_SCRIPTS=(
"$kUPDATE_SCRIPT"
"$kUNINSTALL_SCRIPT"
)
mainOrStable='stable'
[[ -n $N_INSTALL_TEST_OVERRIDE_SHELL_INIT_FILE ]] && mainOrStable='main' # override for testing: use the latest rather than the officially stable code.
kHELPER_SCRIPT_URLS=(
"${kTHIS_REPO_URL_LONG/\/\/github.com\////raw.githubusercontent.com/}/stable/bin/$kUPDATE_SCRIPT"
"${kTHIS_REPO_URL_LONG/\/\/github.com\////raw.githubusercontent.com/}/stable/bin/$kUNINSTALL_SCRIPT"
"${kTHIS_REPO_URL_LONG/\/\/github.com\////raw.githubusercontent.com/}/$mainOrStable/bin/$kUPDATE_SCRIPT"
"${kTHIS_REPO_URL_LONG/\/\/github.com\////raw.githubusercontent.com/}/$mainOrStable/bin/$kUNINSTALL_SCRIPT"
)
# SHA-256 checksum for the helper scripts.
# !! These checksums must be updated whenever `n-update` and `n-uninstall`
Expand Down Expand Up @@ -714,7 +717,7 @@ if [[ -d $N_PREFIX ]]; then
didExist=1
isDirEmpty "$N_PREFIX" || die - 3 <<EOF
Target directory '$N_PREFIX' already exists and is not empty.
Make sure it is completely empty, then try again.
Remove it or make sure it is completely empty, then try again.
EOF

fi
Expand Down
27 changes: 17 additions & 10 deletions test/8 Run successful and failed installations and updates
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ PATH=${PWD%%/test*}/bin:$PATH
# Test override: Skip the check for an existing n / npm / node installation
export N_INSTALL_TEST_OVERRIDE_SKIP_EXISTING_INSTALLATION_TEST=1

# Create a temp. dir.
tmpDir=$(mktemp -d -t XXXX) || die # Works on both macOS and Linux; note: dir. name will have random extension on macOS (e.g., '.../XXXX.bJViLcM3') and none on Linux (e.g., '.../vXDA')
tmpPrefixDir="$tmpDir/n"

# Helper function for error reporting.
die() { (( $# > 0 )) && echo "ERROR: $*" >&2; exit 1; }

Expand All @@ -28,10 +32,9 @@ runUpdate() {
#### !! Must be kept in sync with master copies in 'n-install'.
kTHIS_REPO_URL_LONG='https://github.com/mklement0/n-install' # This script's source repository in LONG form - needed for deriving raw.githubusercontent.com URLs from it.
####

# Create a temp. dir.
tmpDir=$(mktemp -d -t XXXX) || die # Works on both macOS and Linux; note: dir. name will have random extension on macOS (e.g., '.../XXXX.bJViLcM3') and none on Linux (e.g., '.../vXDA')
tmpPrefixDir="$tmpDir/n"
# Derive the URL and use the *master* branch (not the 'stable' label), so as to use the most recent code in prepraration for a new release.
# !! By contrast, https://git.io/n-install points to https://raw.githubusercontent.com/mklement0/n-install/stable/bin/n-install i.e. the *stable* label.
url="${kTHIS_REPO_URL_LONG/\/\/github.com\////raw.githubusercontent.com/}/master/bin/n-install"

# ----------- INSTALLATION

Expand Down Expand Up @@ -70,8 +73,8 @@ for i in {1..6}; do
nodeShouldInstall=1
;;
6) # unattended installation DIRECTLY FROM GITHUB - !! This may fail if the latest changes have not yet been pushed.
descr='unattended: default installation DIRECTLY FROM GITHUB (may fail if the latest changes have not yet been pushed)'
N_PREFIX="$tmpPrefixDir" N_INSTALL_TEST_OVERRIDE_SHELL_INIT_FILE="$tmpDir/initFile" bash <(curl -L "${kTHIS_REPO_URL_LONG/\/\/github.com\////raw.githubusercontent.com/}/master/bin/n-install") -y - && ok=1
descr="unattended: default installation DIRECTLY FROM GITHUB (NOTE: may fail if the latest changes have not yet been pushed with \`make push\`): $url"
curl -L "$url" | N_PREFIX="$tmpPrefixDir" N_INSTALL_TEST_OVERRIDE_SHELL_INIT_FILE="$tmpDir/initFile" bash -s -- -y - && ok=1
nodeShouldInstall=0
;;
*)
Expand All @@ -81,22 +84,26 @@ for i in {1..6}; do

(( ok )) || die "Installation failed unexpectedly: $descr; inspect the target dir.: '$tmpDir'"

# Spot-check for crucial files and dirs:
# --
# Spot-check for crucial files and dirs in the local install dir:
# --

# n's files and helper scripts
# Check for expected presence of n's files and helper scripts
f='n/.repo/.git'; [[ -d $tmpPrefixDir/$f ]] || dieNotFound
f='bin/n'; [[ -x $tmpPrefixDir/$f ]] || dieNotFound

# helper scripts
# Check for expected presence of helper scripts
for f in 'bin/n-update' 'bin/n-uninstall'; do
[[ -x $tmpPrefixDir/$f ]] || dieNotFound
done

# Node.js executables
# Check for expected presence of Node.js executables
for f in 'bin/node' 'bin/npm'; do
[[ -x $tmpPrefixDir/$f ]] && { if (( ! nodeShouldInstall )); then dieUnexpectedlyFound; fi; } || { if (( nodeShouldInstall )); then dieNotFound; fi; }
done

# --

done

# ----------- UPDATING N
Expand Down
1 change: 1 addition & 0 deletions test/9 Run successful and failed uninstallations
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ runUninstall() {

#### !! Must be kept in sync with master copies in 'n-install'.
kINSTALLER_NAME='n-install' # The name of the script that installed n.
# Note: The actual *installation* URL is http://git.io/n-install which redirects to https://raw.githubusercontent.com/mklement0/n-install/stable/bin/n-install
kTHIS_REPO_URL='http://git.io/n-install-repo' # This script's source repository - SHORT, git.io-based form.
kTHIS_REPO_URL_LONG='https://github.com/mklement0/n-install' # This script's source repository in LONG form - needed for deriving raw.githubusercontent.com URLs from it.
kINIT_FILE_LINE_ID=" # Added by $kINSTALLER_NAME (see $kTHIS_REPO_URL)." # The string that identifies the line added by us.
Expand Down

0 comments on commit c1cf904

Please sign in to comment.