From 28c59a2e491e5d9c255d4ad85a8aaf106bbde49f Mon Sep 17 00:00:00 2001 From: zidhannnn Date: Sun, 24 May 2026 19:37:34 +0700 Subject: [PATCH] Add BATS smoke tests --- .travis.yml | 8 ++++++++ Makefile | 11 +++++++++++ README.md | 10 ++++++++++ test/node-reinstall.bats | 30 ++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 .travis.yml create mode 100644 test/node-reinstall.bats diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..13594c6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: bash + +before_install: + - git clone --depth 1 https://github.com/bats-core/bats-core.git /tmp/bats-core + - sudo /tmp/bats-core/install.sh /usr/local + +script: + - make test diff --git a/Makefile b/Makefile index 3b5077a..066c8a8 100644 --- a/Makefile +++ b/Makefile @@ -15,3 +15,14 @@ readme: echo "## Commands" >> README.md echo '' >> README.md ./node-reinstall -h | sed -n -e '/Commands:/,// p' | tail -n +3 >> README.md + +test: + @if command -v bats >/dev/null 2>&1; then \ + bats test/*.bats; \ + elif [ -x test/bats/bin/bats ]; then \ + test/bats/bin/bats test/*.bats; \ + else \ + echo "BATS is required to run the test suite." >&2; \ + echo "Install bats-core or clone it to test/bats." >&2; \ + exit 127; \ + fi diff --git a/README.md b/README.md index 93ebe77..d3e8df6 100644 --- a/README.md +++ b/README.md @@ -100,3 +100,13 @@ Whenever you feel like you need to completely re-install Node and NPM, simply ex node-reinstall --nvm re-install using stable nvm - the default node-reinstall --nvm-latest re-install using latest nvm - creationix/nvm:master node-reinstall 5.0.0 specify a default node version + +## Testing + +The test suite uses [BATS](https://github.com/bats-core/bats-core) for non-destructive smoke tests. Install BATS locally, then run: + +``` +make test +``` + +The current tests only exercise safe paths such as help, version, and invalid option handling. They do not run the reinstall flow or delete local files. diff --git a/test/node-reinstall.bats b/test/node-reinstall.bats new file mode 100644 index 0000000..6604126 --- /dev/null +++ b/test/node-reinstall.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats + +setup() { + REPO_ROOT="$(cd "$BATS_TEST_DIRNAME/.." && pwd)" + SCRIPT="$REPO_ROOT/node-reinstall" +} + +@test "help flag prints usage without running reinstall steps" { + run "$SCRIPT" --help + + [ "$status" -eq 0 ] + [[ "$output" == *"Usage:"* ]] + [[ "$output" == *"--nvm-latest"* ]] +} + +@test "version flag prints the script version" { + expected="$(grep '^VERSION=' "$SCRIPT" | sed -E 's/VERSION="([^"]+)"/\1/')" + + run "$SCRIPT" --version + + [ "$status" -eq 0 ] + [ "$output" = "$expected" ] +} + +@test "unknown flags fail before destructive reinstall steps" { + run "$SCRIPT" --does-not-exist + + [ "$status" -eq 1 ] + [[ "$output" == *"Unknown option"* ]] +}