From 3c14e434c44dcb8ea2a39cea30d0ddcf39e0c276 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 4 Nov 2020 21:13:46 -0500 Subject: [PATCH] Improve directory handling functions --- readme.md | 1 + tests/test_directory_functions.sh | 65 +++++++++++++++++++++++++++++-- utils.sh | 48 ++++++++++++++++++----- 3 files changed, 101 insertions(+), 13 deletions(-) diff --git a/readme.md b/readme.md index 08d59f4..a060582 100644 --- a/readme.md +++ b/readme.md @@ -56,6 +56,7 @@ These execute automatically on script import. ### Directory Functions * `get_absolute_path` - Gets absolute path of passed location. +* `get_directory_path` - Gets absolute folder path of current terminal location or passed location. * `get_directory_name` - Gets directory name of current terminal location or passed location. * `parse_file_name` - Determines `file_name` and `file_extension` of passed file location. diff --git a/tests/test_directory_functions.sh b/tests/test_directory_functions.sh index f6c49bf..56f7a4a 100755 --- a/tests/test_directory_functions.sh +++ b/tests/test_directory_functions.sh @@ -84,13 +84,58 @@ setup () { # 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_path - No Input" { + # Import utils script. + source ../utils.sh + cd ./tests/ + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run test. + get_directory_path + [[ "${return_value}" == "${current_dir}" ]] +} + + +@test "Directory Function - get_directory_path - Dir Input" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run local test. + get_directory_path ${current_dir} + [[ "${return_value}" == "${current_dir}" ]] + + # Run secondary test. + get_directory_path ${absolute_path} + [[ "${return_value}" == "${absolute_path}" ]] +} + + +@test "Directory Function - get_directory_path - File Input" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run local test. + get_directory_path ${current_dir}/test_directory_functions.sh + [[ "${return_value}" == "${current_dir}" ]] + + # Run secondary test. + get_directory_path "/etc/fstab" + [[ "${return_value}" == "/etc" ]] +} + + @test "Directory Function - get_directory_name - Absolute Dir" { # Import utils script. source ../utils.sh @@ -118,6 +163,20 @@ setup () { } +@test "Directory Function - get_directory_name - File" { + # Import utils script. + source ../utils.sh + + # Check current dir. + [[ "${current_dir}" == *"/bash_utils/tests" ]] + + # Run test. + cd ${current_dir} + get_directory_name ./test_directory_functions.sh + [[ "${return_value}" == "tests" ]] +} + + @test "Directory Function - parse_file_name - Self" { # Import utils script. source ../utils.sh diff --git a/utils.sh b/utils.sh index 8e1fb54..96b6b7b 100755 --- a/utils.sh +++ b/utils.sh @@ -6,7 +6,7 @@ # and checking current user value, and prompts of yes/no user input. # # https://git.brandon-rodriguez.com/scripts/bash/utils - # Version 1.2 + # Version 1.3 ## @@ -226,7 +226,7 @@ function get_absolute_path () { if [[ -f ${1} ]] then # Handle for file. - return_value="$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1")" + return_value="$(cd "$(dirname "${1}")"; pwd -P)/$(basename "${1}")" elif [[ -d ${1} ]] then @@ -261,19 +261,18 @@ function get_absolute_path () { ### - # Returns name of current directory. - # If arg is passed, then returns base name of directory path, or parent directory of file path. + # If no arg is provided, returns full path of current directory. + # If arg is passed, then returns absolute path if was directory, or full path of parent directory if was file. # # Return Variable(s): return_value ## -function get_directory_name () { +function get_directory_path () { # Check number of passed function args. if [[ ${#} == 0 ]] then # No args passed. Handle for current directory. - return_value=$(pwd) - return_value=${return_value##*/} + get_absolute_path ./ # Handle for one arg. elif [[ ${#} == 1 ]] @@ -285,13 +284,11 @@ function get_directory_name () { # Handle for file. get_absolute_path ${1} return_value=${return_value%/*} - return_value=${return_value##*/} elif [[ -d ${1} ]] then # Handle for directory. get_absolute_path ${1} - return_value=${return_value##*/} else echo -e "${text_red}Passed value ( ${1} ) is not a valid file or directory.${text_reset}" @@ -300,7 +297,38 @@ function get_directory_name () { # Handle for too many args. else - echo -e "${text_red}Too many args passed. Expected one arg, received ${#}.${text_reset}" + echo -e "${text_red}Too many args passed. Expected zero or one args, received ${#}.${text_reset}" + exit 1 + fi +} + + +### + # If no arg is provided, then returns name of current directory. + # If arg is passed, then returns name of directory, or parent directory of file. + # + # Return Variable(s): return_value + ## +function get_directory_name () { + + # Check number of passed function args. + if [[ ${#} == 0 ]] + then + # No args passed. Pass current directory to parent function. + get_directory_path ./ + return_value=${return_value##*/} + + # Handle for one arg. + elif [[ ${#} == 1 ]] + then + + # Pass provided value to parent function. + get_directory_path ${1} + return_value=${return_value##*/} + + # Handle for too many args. + else + echo -e "${text_red}Too many args passed. Expected zero or one args, received ${#}.${text_reset}" exit 1 fi } -- GitLab