Skip to content

Commit

Permalink
Merge pull request #13 from nodenv/tests
Browse files Browse the repository at this point in the history
Tests

(rebased from commit 0495a2e)
  • Loading branch information
jasonkarns committed Feb 27, 2023
1 parent bafd754 commit 64b6a93
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"bin"
],
"scripts": {
"test": "bats ${CI:+--tap} test",
"posttest": "npm run lint",
"lint": "git ls-files bin test/*.*sh | xargs shellcheck",
"postversion": "npm publish",
Expand All @@ -29,6 +30,10 @@
"publish:github": "script/publish/github"
},
"devDependencies": {
"@nodenv/nodenv": "^1.3.0",
"bats": "^1.1.0",
"bats-assert": "github:jasonkarns/bats-assert-1",
"bats-support": "github:jasonkarns/bats-support",
"brew-publish": "^2.3.1"
},
"keywords": [
Expand Down
39 changes: 39 additions & 0 deletions test/doctor.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bats

load test_helper

@test "reports missing bin in PATH" {
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" {
with_nodenv_in_home

run nodenv-doctor

assert_failure
assert_line "Checking for \`nodenv' in PATH: not found"
assert_line " You seem to have nodenv installed in \`$HOME/.nodenv/bin', but that"
}

@test "reports successful bin in PATH" {
with_nodenv

run nodenv-doctor

assert_line "Checking for \`nodenv' in PATH: $PWD/node_modules/.bin/nodenv"
}

@test "reports multiple bins in PATH" {
with_nodenv
with_nodenv_in_home
PATH="$HOME/.nodenv/bin:$PATH"

run nodenv-doctor

assert_line "Checking for \`nodenv' in PATH: multiple"
}
32 changes: 32 additions & 0 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load ../node_modules/bats-support/load
load ../node_modules/bats-assert/load

# guard against executing this block twice due to bats internals
if [ -z "$TEST_DIR" ]; then
TEST_DIR="${BATS_TMPDIR}/nodenv"
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
fi

with_nodenv_in_home() {
local nodenv="$PWD/node_modules/.bin/nodenv"

mkdir -p "$HOME/.nodenv/bin"
cd "$HOME/.nodenv/bin" || return
ln -sf "$nodenv" nodenv
}

with_nodenv() {
PATH="$PWD/node_modules/.bin:$PATH"
}

0 comments on commit 64b6a93

Please sign in to comment.