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..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 } @@ -310,3 +324,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..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() { @@ -465,6 +508,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 +597,5 @@ test_assert_gt test_assert_ge test_assert_lt test_assert_le - +test_assert_file_exist +test_assert_file_not_exist