diff --git a/package.json b/package.json index 15463fe..66fec23 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "fix:markdown": "npx markdownlint -df .", "lint:prettier": "npx prettier . --check", "fix:prettier": "npx prettier . --write", - "test": "echo \"Error: no test specified\" && exit 0" + "test": "bats tests/dfm" }, "repository": { "type": "git", @@ -29,6 +29,7 @@ "devDependencies": { "@ivuorinen/base-configs": "^2.0.0", "@types/node": "^24.0.1", + "bats": "^1.12.0", "typescript": "^5.8.3" }, "packageManager": "yarn@1.22.22" diff --git a/tests/dfm/main.bats b/tests/dfm/main.bats new file mode 100644 index 0000000..d3647b9 --- /dev/null +++ b/tests/dfm/main.bats @@ -0,0 +1,34 @@ +setup() { + PROJECT_ROOT="$BATS_TEST_DIRNAME/../.." +} + +@test "list_available_commands shows commands" { + run bash -c "export DFM_CMD_DIR=$PROJECT_ROOT/local/dfm/cmd; export DFM_LIB_DIR=$PROJECT_ROOT/local/dfm/lib; export TEMP_DIR=\$(mktemp -d); source $PROJECT_ROOT/local/dfm/lib/common.sh; source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; main::list_available_commands" + [ "$status" -eq 0 ] + echo "$output" | grep -q "Available commands" + echo "$output" | grep -q "install" +} + +@test "interactive confirm returns 0 on yes" { + run bash -c "source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; utils::interactive::confirm \"Proceed?\" <<< \"y\"; echo \$?" + [ "$status" -eq 0 ] + [ "${lines[-1]}" = "0" ] +} + +@test "interactive confirm returns 1 on no" { + run bash -c "source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; utils::interactive::confirm \"Proceed?\" <<< \"n\"; echo \$?" + [ "$status" -eq 0 ] + [ "${lines[-1]}" = "1" ] +} + +@test "execute_command runs function" { + run bash -c "export DFM_CMD_DIR=$PROJECT_ROOT/local/dfm/cmd; export DFM_LIB_DIR=$PROJECT_ROOT/local/dfm/lib; export TEMP_DIR=\$(mktemp -d); source $PROJECT_ROOT/local/dfm/lib/common.sh; source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; main::execute_command install fonts" + [ "$status" -eq 0 ] + echo "$output" | grep -q "Installing fonts" +} + +@test "execute_command fails on missing function" { + run bash -c "export DFM_CMD_DIR=$PROJECT_ROOT/local/dfm/cmd; export DFM_LIB_DIR=$PROJECT_ROOT/local/dfm/lib; export TEMP_DIR=\$(mktemp -d); source $PROJECT_ROOT/local/dfm/lib/common.sh; source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; main::execute_command install nofunc >/dev/null 2>&1; echo \$?" + [ "$status" -eq 0 ] + [ "${lines[-1]}" = "1" ] +}