diff --git a/tests/test_directory_functions.sh b/tests/test_directory_functions.sh new file mode 100755 index 0000000000000000000000000000000000000000..f6c49bfe93888f5383227ced3a1adcc547d0d248 --- /dev/null +++ b/tests/test_directory_functions.sh @@ -0,0 +1,165 @@ +#!/usr/bin/env bash +### + # Tests for the "Directory Functions" section of the utils script. + ## + + +setup () { + current_dir="$(cd "$(dirname "$(pwd)")"; pwd -P)/$(basename "$(pwd)")" + absolute_path=/etc/systemd/system + absolute_parent=/etc/systemd + symlink_path=/etc/httpd/modules + abs_symlink_path=/usr/lib/httpd/modules +} + + +@test "Directory Function - get_absolute_path - Relative Current Dir" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run test. + get_absolute_path ./tests + [[ "${return_value}" == "${current_dir}" ]] +} + + +@test "Directory Function - get_absolute_path - Relative Current File" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run test. + get_absolute_path ./tests/test_directory_functions.sh + [[ "${return_value}" == "${current_dir}/test_directory_functions.sh" ]] +} + + +@test "Directory Function - get_absolute_path - Absolute Dir" { + # Import utils script. + source ../utils.sh + + get_absolute_path ${absolute_path} + [[ "${return_value}" == "${absolute_path}" ]] +} + + +@test "Directory Function - get_absolute_path - Relative Parent Dir" { + # Import utils script. + source ../utils.sh + + cd ${absolute_path} + get_absolute_path ../ + [[ "${return_value}" == "${absolute_parent}" ]] +} + + +@test "Directory Function - get_absolute_path - Relative Self Dir" { + # Import utils script. + source ../utils.sh + + cd ${absolute_path} + get_absolute_path ./ + [[ "${return_value}" == "${absolute_path}" ]] +} + + +@test "Directory Function - get_absolute_path - Relative Child Dir" { + # Import utils script. + source ../utils.sh + + cd ${absolute_parent} + get_absolute_path ./system + [[ "${return_value}" == "${absolute_path}" ]] +} + + +@test "Directory Function - get_absolute_path - Symlink" { + # Import utils script. + source ../utils.sh + + # I'm not sure if this symlink path exists in most linux distros, but I'm hoping it does. + get_absolute_path ${symlink_path} + echo "symlink_path: ${symlink_path}" + echo "abs_symlink_path: ${abs_symlink_path}" + echo "return_value: ${return_value}" + [[ "${return_value}" == "${abs_symlink_path}" ]] +} + + +@test "Directory Function - get_directory_name - Absolute Dir" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run test. + get_directory_name ${current_dir} + [[ "${return_value}" == "tests" ]] +} + + +@test "Directory Function - get_directory_name - Relative Dir" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run test. + cd ${current_dir} + get_directory_name ./ + [[ "${return_value}" == "tests" ]] +} + + +@test "Directory Function - parse_file_name - Self" { + # Import utils script. + source ../utils.sh + + # Run test. + parse_file_name ./tests/test_directory_functions.sh + [[ "${file_name}" == "test_directory_functions" ]] + [[ "${file_extension}" == ".sh" ]] +} + + +@test "Directory Function - parse_file_name - Multi-Part Extension" { + # Import utils script. + source ../utils.sh + + # Run test with basic extension. + touch ./tests/aaa.b + parse_file_name ./tests/aaa.b + [[ "${file_name}" == "aaa" ]] + [[ "${file_extension}" == ".b" ]] + rm ./tests/aaa.b + + # Run test with double extension. + touch ./tests/aaa.b.c + parse_file_name ./tests/aaa.b.c + [[ "${file_name}" == "aaa" ]] + [[ "${file_extension}" == ".b.c" ]] + rm ./tests/aaa.b.c + + # Run test with triple extension. + touch ./tests/aaa.b.c.d + parse_file_name ./tests/aaa.b.c.d + [[ "${file_name}" == "aaa" ]] + [[ "${file_extension}" == ".b.c.d" ]] + rm ./tests/aaa.b.c.d + + # Run test with quadruple extension. + touch ./tests/aaa.b.c.d.e + parse_file_name ./tests/aaa.b.c.d.e + [[ "${file_name}" == "aaa" ]] + [[ "${file_extension}" == ".b.c.d.e" ]] + rm ./tests/aaa.b.c.d.e + + # Probably safe to assume it works for all further n extension lengths. +}