Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moar tests #14

Merged
merged 10 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/nodenv-doctor
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ 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 ] || {
echo -n "You can install Node versions like so: "
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: "
Expand Down
99 changes: 95 additions & 4 deletions test/doctor.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

load test_helper

@test "reports missing bin in PATH" {
@test "reports bin in PATH - missing" {
run nodenv-doctor

assert_failure
assert_line "Checking for \`nodenv' in PATH: not found"
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
Expand All @@ -20,20 +20,111 @@ 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

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
}
37 changes: 31 additions & 6 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}