From 64b6a939c1624ba2cad396b0d7ffd58938031ed6 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Sun, 11 Aug 2019 17:46:05 -0400 Subject: [PATCH] Merge pull request #13 from nodenv/tests Tests (rebased from commit 0495a2e60b83797a8a7fb6ab69d92664167ee8be) --- package-lock.json | 22 ++++++++++++++++++++++ package.json | 5 +++++ test/doctor.bats | 39 +++++++++++++++++++++++++++++++++++++++ test/test_helper.bash | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100755 test/doctor.bats create mode 100644 test/test_helper.bash diff --git a/package-lock.json b/package-lock.json index 20da670..154c860 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,28 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@nodenv/nodenv": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@nodenv/nodenv/-/nodenv-1.3.0.tgz", + "integrity": "sha512-Vs9W+e8hMOoLwEFGF3LGB7CO3n7eK5Du2HZoOadMAMxPlD1Cdq0vlJgtWFxIcXxICKrjXCiVYU2eS6ICV0u5qw==", + "dev": true + }, + "bats": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/bats/-/bats-1.1.0.tgz", + "integrity": "sha512-1pA29OhDByrUtAXX+nmqZxgRgx2y8PvuZzbLJVjd2dpEDVDvz0MjcBMdmIPNq5lC+tG53G+RbeRsbIlv3vw7tg==", + "dev": true + }, + "bats-assert": { + "version": "github:jasonkarns/bats-assert-1#027f1cf7d4266c7516674a0a6f77c3bbb48d8b42", + "from": "github:jasonkarns/bats-assert-1", + "dev": true + }, + "bats-support": { + "version": "github:jasonkarns/bats-support#d140a65044b2d6810381935ae7f0c94c7023c8c3", + "from": "github:jasonkarns/bats-support", + "dev": true + }, "brew-publish": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/brew-publish/-/brew-publish-2.3.1.tgz", diff --git a/package.json b/package.json index 4e12576..e6066c1 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": [ diff --git a/test/doctor.bats b/test/doctor.bats new file mode 100755 index 0000000..44a855f --- /dev/null +++ b/test/doctor.bats @@ -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" +} diff --git a/test/test_helper.bash b/test/test_helper.bash new file mode 100644 index 0000000..2b794e8 --- /dev/null +++ b/test/test_helper.bash @@ -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" +}