diff --git a/bin/nodenv-doctor b/bin/nodenv-doctor index 873e5c9..e38b1ee 100755 --- a/bin/nodenv-doctor +++ b/bin/nodenv-doctor @@ -129,8 +129,8 @@ else fi echo -n "Counting installed Node versions: " -num_rubies="$(nodenv versions --bare | wc -l)" -if [ "$num_rubies" -eq 0 ]; then +num_nodes="$(nodenv versions --bare | wc -l)" +if [ "$num_nodes" -eq 0 ]; then printc yellow "none\n" echo "There aren't any Node versions installed under \`$NODENV_ROOT/versions'." | indent [ "$num_installs" -eq 0 ] || { @@ -138,7 +138,7 @@ if [ "$num_rubies" -eq 0 ]; then printc bright "nodenv install 2.2.4\n" } | indent else - printc green "%d versions\n" "$num_rubies" + printc green "%d versions\n" "$num_nodes" fi echo -n "Auditing installed plugins: " diff --git a/test/doctor.bats b/test/doctor.bats index 44a855f..7dfbf34 100755 --- a/test/doctor.bats +++ b/test/doctor.bats @@ -2,7 +2,7 @@ load test_helper -@test "reports missing bin in PATH" { +@test "reports bin in PATH - missing" { run nodenv-doctor assert_failure @@ -10,7 +10,7 @@ load test_helper assert_line " Please refer to https://github.com/nodenv/nodenv#installation" } -@test "reports missing bin in PATH despite ~/.nodenv" { +@test "reports bin in PATH - missing, despite ~/.nodenv" { with_nodenv_in_home run nodenv-doctor @@ -20,7 +20,7 @@ load test_helper assert_line " You seem to have nodenv installed in \`$HOME/.nodenv/bin', but that" } -@test "reports successful bin in PATH" { +@test "reports bin in PATH - OK" { with_nodenv run nodenv-doctor @@ -28,12 +28,103 @@ load test_helper assert_line "Checking for \`nodenv' in PATH: $PWD/node_modules/.bin/nodenv" } -@test "reports multiple bins in PATH" { +@test "reports bin in PATH - multiple" { with_nodenv with_nodenv_in_home PATH="$HOME/.nodenv/bin:$PATH" run nodenv-doctor + assert_failure assert_line "Checking for \`nodenv' in PATH: multiple" } + +@test "reports shims in PATH - missing" { + with_nodenv + + run nodenv-doctor + + assert_failure + assert_line "Checking for nodenv shims in PATH: not found" +} + +@test "reports shims in PATH - OK" { + with_nodenv + with_nodenv_shims + + run nodenv-doctor + + assert_line "Checking for nodenv shims in PATH: OK" +} + +@test "reports nodenv-install - missing" { + with_nodenv + + run nodenv-doctor + + assert_failure + assert_line "Checking \`nodenv install' support: not found" +} + +@test "reports nodenv-install - OK" { + with_nodenv + with_nodenv_plugin nodenv-install + + run nodenv-doctor + + assert_line -p "Checking \`nodenv install' support: $NODENV_ROOT/plugins/nodenv-install/bin/nodenv-install" +} + +@test "reports nodenv-install - multiple" { + with_nodenv + with_nodenv_plugin nodenv-install + with_nodenv_plugin other-plugin nodenv-install + + run nodenv-doctor + + assert_failure + assert_line "Checking \`nodenv install' support: multiple" +} + +@test "reports nodes - zero" { + with_nodenv + + run nodenv-doctor + + assert_line "Counting installed Node versions: none" +} + +@test "reports nodes - some" { + with_nodenv + with_nodes 10.2.3 12.5.6 + + run nodenv-doctor + + assert_line "Counting installed Node versions: 2 versions" +} + +@test "reports plugins - OK" { + with_nodenv + + run nodenv-doctor + + assert_line "Auditing installed plugins: OK" +} + +@test "reports clean setup" { + with_nodenv + with_nodenv_shims + with_nodenv_plugin nodenv-install + with_nodes 10.2.3 + + run nodenv-doctor + + assert_success + assert_output - -e <<-OUT +Checking for \`nodenv' in PATH: $PWD/node_modules/.bin/nodenv +Checking for nodenv shims in PATH: OK +Checking \`nodenv install' support: $NODENV_ROOT/plugins/nodenv-install/bin/nodenv-install \(.*\) +Counting installed Node versions: 1 versions +Auditing installed plugins: OK +OUT +} diff --git a/test/test_helper.bash b/test/test_helper.bash index 2b794e8..18917fc 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -7,18 +7,22 @@ if [ -z "$TEST_DIR" ]; then TEST_DIR="$(mktemp -d "${TEST_DIR}.XXX" 2>/dev/null || echo "$TEST_DIR")" export TEST_DIR - export HOME="${TEST_DIR}/home" - - PATH=/usr/bin:/bin:/usr/sbin:/sbin - PATH="${BATS_TEST_DIRNAME}/../bin:$PATH" - export PATH - for nodenv_var in $(env 2>/dev/null | grep '^NODENV_' | cut -d= -f1); do unset "$nodenv_var" done unset nodenv_var + + export HOME="${TEST_DIR}/home" + export NODENV_ROOT="${TEST_DIR}/root" + PATH=/usr/bin:/bin:/usr/sbin:/sbin + PATH="${BATS_TEST_DIRNAME}/../bin:$PATH" + export PATH fi +teardown() { + rm -rf "${TEST_DIR:?}" +} + with_nodenv_in_home() { local nodenv="$PWD/node_modules/.bin/nodenv" @@ -30,3 +34,24 @@ with_nodenv_in_home() { with_nodenv() { PATH="$PWD/node_modules/.bin:$PATH" } + +with_nodenv_shims() { + local shims_path="$NODENV_ROOT/shims" + mkdir -p "$shims_path" + PATH="$shims_path:$PATH" +} + +with_nodenv_plugin() { + local name=$1 + local bin=${2:-$1} + local path="$NODENV_ROOT/plugins/$name/bin" + mkdir -p "$path" + ln -sf "$(command -v grep)" "$path/$bin" # using grep b/c it supports '--version' +} + +with_nodes() { + local path="$NODENV_ROOT/versions" + for node in "$@"; do + mkdir -p "$path/$node" + done +}