From 91d634d576a5107c2da6563d5357546544045b3b Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Fri, 17 Nov 2017 00:01:51 +0200 Subject: [PATCH] Add some very simple tests --- .gitignore | 1 - .travis.yml | 10 ++++++++- tests/run.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100755 tests/run.sh diff --git a/.gitignore b/.gitignore index b27b657..f61e320 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -tests .*/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 27091a4..a0315aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,16 @@ services: before_install: - docker pull koalaman/shellcheck:latest +jobs: + include: + - stage: test + script: + - docker run -v $(pwd):/scripts --workdir=/scripts koalaman/shellcheck:latest $(grep -lE '^#!/.+(ba)?sh$' $(git ls-files)) + - + script: + - docker build -t ebooktools/scripts:latest . + - docker run ebooktools/scripts:latest tests/run.sh script: - - docker run -v $(pwd):/scripts --workdir=/scripts koalaman/shellcheck:latest $(grep -lE '^#!/.+(ba)?sh$' $(git ls-files)) matrix: fast_finish: true \ No newline at end of file diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..cc66f55 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash + +set -eEuo pipefail + +DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")") +EXIT_CODE=0 + +# shellcheck source=./lib.sh +. "$DIR/../lib.sh" + +assert_eq() { + local expected="$1" got="$2" exit_immediately="${3:-false}" + echo -n "[line $(caller)] " + if [[ "$expected" != "$got" ]]; then + echo "ERROR: expected '$expected' but got '$got'" + EXIT_CODE=11 + if [[ "$exit_immediately" == "true" ]]; then + exit "$EXIT_CODE" + fi + else + echo "OK" + fi +} + +expect_err() { + local expr_to_eval="$1" exit_immediately="${2:-false}" + echo -n "[line $(caller)] " + if [[ "$($expr_to_eval >/dev/null 2>&1 && echo success)" != "" ]]; then + echo "ERROR: expected error for expression '$expr_to_eval'" + EXIT_CODE=12 + if [[ "$exit_immediately" == "true" ]]; then + exit "$EXIT_CODE" + fi + else + echo "OK" + fi +} + +# Test to_lower +assert_eq "lower" "$(echo 'lower' | to_lower)" +assert_eq "upper" "$(echo 'UPPER' | to_lower)" +assert_eq "utf-8 кирилица" "$(echo 'UTF-8 КИРИЛИЦА' | to_lower)" + +# Test uniq_no_sort +assert_eq '' "$(echo -n | uniq_no_sort)" +assert_eq 'no newline' "$(echo -n 'no newline' | uniq_no_sort)" +assert_eq \ + $'zzz\naaa\nbbb' \ + "$(echo $'zzz\nzzz\naaa\nzzz\naaa\nbbb\nbbb' | uniq_no_sort)" + +# Test str_concat +expect_err 'str_concat' # The function needs at least 1 argument +assert_eq "testmest123" "$(str_concat "" test mest 123)" +assert_eq $'aa\nbb' "$(str_concat $'\n' aa bb)" + +# Test find_isbns +assert_eq "" "$(echo -n | find_isbns)" +assert_eq "" "$(echo "invalid isbn 1122334455" | find_isbns)" +assert_eq "076532637X" "$(echo "just an isbn 076532637X in some text" | find_isbns)" +assert_eq "075640407X,9780756404079" "$(echo "075640407X (ISBN13: 9780756404079)" | find_isbns)" +assert_eq "9781610391849,1610391845" "$(echo "crazy!978-16–103⁻918 49 16 10—39¯1845-z" | find_isbns)" + +exit "$EXIT_CODE" \ No newline at end of file