From 9c2656312c179c26757ec3e899a928d64b642d2b Mon Sep 17 00:00:00 2001 From: Ethan P Date: Sat, 7 Sep 2019 15:00:36 -0700 Subject: [PATCH] Updated test system to be a little more straightforward --- test/run.sh | 101 +++++++++++++++++++++--- test/util/{test.sh => test-exec.sh} | 2 +- test/util/{runner.sh => test-runner.sh} | 0 3 files changed, 91 insertions(+), 12 deletions(-) rename test/util/{test.sh => test-exec.sh} (98%) rename test/util/{runner.sh => test-runner.sh} (100%) diff --git a/test/run.sh b/test/run.sh index 75920d5..badc669 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,21 +1,100 @@ #!/usr/bin/env bash +# ----------------------------------------------------------------------------- +# bat-extras | Copyright (C) 2019 eth-p | MIT License +# +# Repository: https://github.com/eth-p/bat-extras +# Issues: https://github.com/eth-p/bat-extras/issues +# ----------------------------------------------------------------------------- HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -FAIL=false -ACTION="snapshot-test" +LIB="${HERE}/../lib" +source "${LIB}/print.sh" +# ----------------------------------------------------------------------------- + +# Runs a test. +# +# Arguments: +# 1 -- The test name. +# 2 -- The test action. +# +# Returns: +# 0 -- The test passed. +# 1 -- The test failed. +run_test() { + local test_name="$1" + local test_action="$2" + + bash "$HERE/util/test-exec.sh" "$test_action" "$test_name" + return $? +} + +# Runs all tests. +# +# Arguments: +# 1 -- The test action. +# +# Variables: +# RESULT -- "pass" or "fail". +# RESULT_PASS -- The number of tests that passed. +# RESULT_FAIL -- The number of tests that failed. +run_all_tests() { + local test_action="$1" + + RESULT="pass" + RESULT_PASS=0 + RESULT_FAIL=0 + + for test in "$HERE"/tests/*.sh; do + if run_test "$(basename "$test" .sh)" "$test_action"; then + ((RESULT_PASS++)) + else + ((RESULT_FAIL++)) + RESULT="fail" + fi + done + + if [ "$RESULT" = "fail" ]; then + return 1 + else + return 0 + fi +} + +# Displays a summary of the tests run. +display_test_summary() { + local tpc + local tfc + + [[ "$RESULT_PASS" -gt 0 ]] && tpc="GREEN" || tpc="CLEAR"; + [[ "$RESULT_FAIL" -gt 0 ]] && tfc="RED" || tfc="CLEAR"; + + printc "%{YELLOW}RESULT: %{${tpc}}%s%{YELLOW} passed, %{${tfc}}%s%{YELLOW} failed.%{CLEAR}\n" \ + "$RESULT_PASS" \ + "$RESULT_FAIL" +} + +# ----------------------------------------------------------------------------- if [ -n "$1" ]; then - ACTION="$1" + run_all_tests "$1" + display_test_summary + exit $? fi -for test in "$HERE"/tests/*.sh; do - test_name="$(basename "$test" .sh)" - bash "$HERE/util/test.sh" "$ACTION" "$test_name" - if [ $? -ne 0 ]; then - FAIL=true +# Run all actions. +FINAL_RESULT="pass" +for action in "snapshot-test" "consistency-test"; do + printc "%{CYAN}Running %{CLEAR}%s%{CYAN} tests:%{CLEAR}\n" "$action" + run_all_tests "$action" + display_test_summary + printf "\n" + + if [[ "$RESULT" = "fail" ]]; then + FINAL_RESULT=fail fi done -if [ "$FAIL" = true ]; then - exit 1 -fi +case "$FINAL_RESULT" in + pass) exit 0;; + fail) exit 1;; +esac diff --git a/test/util/test.sh b/test/util/test-exec.sh similarity index 98% rename from test/util/test.sh rename to test/util/test-exec.sh index 5b52a07..64d26c3 100755 --- a/test/util/test.sh +++ b/test/util/test-exec.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -RUNNER="${HERE}/runner.sh" +RUNNER="${HERE}/test-runner.sh" TEST_DATA="${HERE}/../data" # Test data. diff --git a/test/util/runner.sh b/test/util/test-runner.sh similarity index 100% rename from test/util/runner.sh rename to test/util/test-runner.sh