From 4e2e713c028b3bce2f6705c53b25a619a5841c36 Mon Sep 17 00:00:00 2001 From: Partiban Ramasamy Date: Wed, 1 Jul 2020 11:26:39 +0100 Subject: [PATCH 1/2] add: assert_file_exist & assert_file_not_exist methods --- README.md | 2 ++ assert.sh | 32 ++++++++++++++++++++++ test_assert.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0193cab..3a26590 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ assert_eq "hello" "world" * `assert_ge` checks whether the first param is greator than or equal to the second one. * `assert_lt` checks whether the first param is less than the second one. * `assert_le` checks whether the first param is less than or equal to the second one. +* `assert_file_exist` checks whether the first param is an existing regular file. +* `assert_file_not_exist` checks whether the first param is not an existing regular file. ### How to write tests diff --git a/assert.sh b/assert.sh index 32e7da5..136be4b 100755 --- a/assert.sh +++ b/assert.sh @@ -310,3 +310,35 @@ assert_le() { return 1 fi } + +assert_file_exist() { + local first="$1" + local msg + + if [ "$#" -ge 2 ]; then + msg="$2" + fi + + if [[ -f "$first" ]]; then + return 0 + else + [ "${#msg}" -gt 0 ] && log_failure "$first ! exist :: $msg" || true + return 1 + fi +} + +assert_file_not_exist() { + local first="$1" + local msg + + if [ "$#" -ge 2 ]; then + msg="$2" + fi + + if [[ ! -f "$first" ]]; then + return 0 + else + [ "${#msg}" -gt 0 ] && log_failure "$first exists :: $msg" || true + return 1 + fi +} diff --git a/test_assert.sh b/test_assert.sh index eab0e30..030bdc7 100755 --- a/test_assert.sh +++ b/test_assert.sh @@ -465,6 +465,77 @@ test_assert_le() { fi } +test_assert_file_exist() { + log_header "Test :: assert_file_exist" + + local tmp_existing_file="tmp_test_file" + local tmp_non_existent_fle="tmp_should_not_exist_file" + local tmp_existing_dir="tmp_test_dir" + + # ci-before + echo "A test file" > "$tmp_existing_file" + mkdir "$tmp_existing_dir" + + assert_file_exist "$tmp_existing_file" + if [ "$?" == 0 ]; then + log_success "assert_file_exist returns 0 if param is existing file" + else + log_failure "assert_file_exist does not work" + fi + + assert_file_exist "$tmp_non_existent_fle" + if [ "$?" == 1 ]; then + log_success "assert_file_exist returns 1 if param is not existing path" + else + log_failure "assert_file_exist does not work" + fi + + assert_file_exist "$tmp_existing_dir" + if [ "$?" == 1 ]; then + log_success "assert_file_exist returns 1 if param is a existing directory" + else + log_failure "assert_file_exist does not work" + fi + + # ci-after + rm -rf "$tmp_existing_file" "$tmp_existing_dir" +} + +test_assert_file_not_exist() { + log_header "Test :: assert_file_not_exist" + + local tmp_existing_file="tmp_test_file" + local tmp_non_existent_fle="tmp_should_not_exist_file" + local tmp_existing_dir="tmp_test_dir" + + # ci-before + echo "A test file" > "$tmp_existing_file" + mkdir "$tmp_existing_dir" + + assert_file_not_exist "$tmp_non_existent_fle" + if [ "$?" == 0 ]; then + log_success "assert_file_not_exist returns 0 if param is non existing file" + else + log_failure "assert_file_not_exist does not work" + fi + + assert_file_not_exist "$tmp_existing_dir" + if [ "$?" == 0 ]; then + log_success "assert_file_not_exist returns 1 if param is a existing directory" + else + log_failure "assert_file_not_exist does not work" + fi + + assert_file_not_exist "$tmp_existing_file" + if [ "$?" == 1 ]; then + log_success "assert_file_not_exist returns 1 if param is a existing file" + else + log_failure "assert_file_not_exist does not work" + fi + + # ci-after + rm -rf "$tmp_existing_file" "$tmp_existing_dir" +} # test calls @@ -483,4 +554,5 @@ test_assert_gt test_assert_ge test_assert_lt test_assert_le - +test_assert_file_exist +test_assert_file_not_exist \ No newline at end of file From 9ca73cc22cf94a826ade223242a3de9fedf8df68 Mon Sep 17 00:00:00 2001 From: Partiban Ramasamy Date: Wed, 1 Jul 2020 14:54:16 +0100 Subject: [PATCH 2/2] fix: assert_contain & test_not_contain for empty strings --- assert.sh | 18 ++++++++++++++++-- test_assert.sh | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/assert.sh b/assert.sh index 136be4b..14b25fe 100755 --- a/assert.sh +++ b/assert.sh @@ -214,10 +214,15 @@ assert_contain() { return 0; fi + if [ -z "${haystack:+x}" ] && [ ! -z "${needle:+x}" ]; then + [ "${#msg}" -gt 0 ] && log_failure "'$haystack' doesn't contain '$needle' :: $msg" || true + return 1; + fi + if [ -z "${haystack##*$needle*}" ]; then return 0 else - [ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg" || true + [ "${#msg}" -gt 0 ] && log_failure "'$haystack' doesn't contain '$needle' :: $msg" || true return 1 fi } @@ -231,6 +236,15 @@ assert_not_contain() { msg="$3" fi + if [ -z "${haystack:+x}" ] && [ -z "${needle:+x}" ]; then + [ "${#msg}" -gt 0 ] && log_failure "'$haystack' contains '$needle' :: $msg" || true + return 1; + fi + + if [ -z "${haystack:+x}" ]; then + return 0; + fi + if [ -z "${needle:+x}" ]; then return 0; fi @@ -238,7 +252,7 @@ assert_not_contain() { if [ "${haystack##*$needle*}" ]; then return 0 else - [ "${#msg}" -gt 0 ] && log_failure "$haystack contains $needle :: $msg" || true + [ "${#msg}" -gt 0 ] && log_failure "'$haystack' contains '$needle' :: $msg" || true return 1 fi } diff --git a/test_assert.sh b/test_assert.sh index 030bdc7..862d84a 100755 --- a/test_assert.sh +++ b/test_assert.sh @@ -292,6 +292,27 @@ test_assert_contain() { else log_failure "assert_contain does not work" fi + + assert_contain "" "needle" + if [ "$?" == 1 ]; then + log_success "assert_contain returns 1 if the haystack is an empty string" + else + log_failure "assert_contain does not work" + fi + + assert_contain "haystack" "" + if [ "$?" == 0 ]; then + log_success "assert_contain returns 0 if the needle is an empty string" + else + log_failure "assert_contain does not work" + fi + + assert_contain "" "" + if [ "$?" == 0 ]; then + log_success "assert_contain returns 0 if the haystack and needle are empty strings" + else + log_failure "assert_contain does not work" + fi } test_assert_not_contain() { @@ -359,6 +380,28 @@ test_assert_not_contain() { else log_failure "assert_not_contain does not work" fi + +# assert_not_contain haystack needle + assert_not_contain "" "needle" + if [ "$?" == 0 ]; then + log_success "assert_not_contain returns 0 if the haystack is an empty string" + else + log_failure "assert_not_contain does not work" + fi + + assert_not_contain "haystack" "" + if [ "$?" == 0 ]; then + log_success "assert_not_contain returns 1 if the needle is an empty string" + else + log_failure "assert_not_contain does not work" + fi + + assert_not_contain "" "" + if [ "$?" == 1 ]; then + log_success "assert_not_contain returns 1 if the haystack and needle are empty strings" + else + log_failure "assert_not_contain does not work" + fi } test_assert_gt() { @@ -555,4 +598,4 @@ test_assert_ge test_assert_lt test_assert_le test_assert_file_exist -test_assert_file_not_exist \ No newline at end of file +test_assert_file_not_exist