Skip to content

Commit

Permalink
add script_run, script_load and file_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimhellum committed Dec 31, 2024
1 parent 313aacf commit e11803b
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 0 deletions.
100 changes: 100 additions & 0 deletions lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,103 @@ fail() {
echo "$message" >&2
exit 1
}

#######################################
# Gets the directory of the current script.
# Globals:
# BASH_SOURCE
# Arguments:
# None
# Outputs:
# Writes the directory path to stdout.
# Returns:
# 0 (true) if the directory is found, 1 (false) otherwise.
#######################################
script_dir() {
realpath "$(dirname "${BASH_SOURCE[0]}")"
}

#######################################
# Looks up a file in predefined directories.
# Globals:
# PATH
# Arguments:
# filename (string): The name of the file to look up.
# Outputs:
# Writes the file path to stdout if found.
# Returns:
# 0 (true) if the file is found, 1 (false) otherwise.
#######################################
file_lookup() {
local filename="$1"
local search_dirs=(
"$(script_dir)"
"$PWD"
)

# Add directories from PATH to search_dirs
for dir in ${PATH//:/ }; do
search_dirs+=("$dir")
done

for dir in "${search_dirs[@]}"; do
local filepath="$dir/$filename"
if [ -f "$filepath" ]; then
echo "$filepath"
return 0
fi
done

return 1
}

#######################################
# Runs a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to run.
# ... (any): Additional arguments to pass to the script.
# Outputs:
# Writes the script output to stdout.
# Returns:
# 0 (true) if the script runs successfully, 1 (false) otherwise.
#######################################
script_run() {
local filename="$1"
local output status
if filepath=$(file_lookup "$filename"); then
shift
output="$("$filepath" "$@" 2>&1)"
status=$?

# make output and status available to other scripts
export RUN_OUTPUT="$output"
export RUN_STATUS="$status"

echo "$output"
return $status
else
return 1
fi
}

#######################################
# Loads a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to load.
# Outputs:
# None
# Returns:
# 0 (true) if the script is loaded successfully, 1 (false) otherwise.
#######################################
script_load() {
local filename="$1"
if filepath=$(file_lookup "$filename"); then
source "$filepath"
else
return 1
fi
}
100 changes: 100 additions & 0 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,103 @@ fail() {
echo "$message" >&2
exit 1
}

#######################################
# Gets the directory of the current script.
# Globals:
# BASH_SOURCE
# Arguments:
# None
# Outputs:
# Writes the directory path to stdout.
# Returns:
# 0 (true) if the directory is found, 1 (false) otherwise.
#######################################
script_dir() {
realpath "$(dirname "${BASH_SOURCE[0]}")"
}

#######################################
# Looks up a file in predefined directories.
# Globals:
# PATH
# Arguments:
# filename (string): The name of the file to look up.
# Outputs:
# Writes the file path to stdout if found.
# Returns:
# 0 (true) if the file is found, 1 (false) otherwise.
#######################################
file_lookup() {
local filename="$1"
local search_dirs=(
"$(script_dir)"
"$PWD"
)

# Add directories from PATH to search_dirs
for dir in ${PATH//:/ }; do
search_dirs+=("$dir")
done

for dir in "${search_dirs[@]}"; do
local filepath="$dir/$filename"
if [ -f "$filepath" ]; then
echo "$filepath"
return 0
fi
done

return 1
}

#######################################
# Runs a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to run.
# ... (any): Additional arguments to pass to the script.
# Outputs:
# Writes the script output to stdout.
# Returns:
# 0 (true) if the script runs successfully, 1 (false) otherwise.
#######################################
script_run() {
local filename="$1"
local output status
if filepath=$(file_lookup "$filename"); then
shift
output="$("$filepath" "$@" 2>&1)"
status=$?

# make output and status available to other scripts
export RUN_OUTPUT="$output"
export RUN_STATUS="$status"

echo "$output"
return $status
else
return 1
fi
}

#######################################
# Loads a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to load.
# Outputs:
# None
# Returns:
# 0 (true) if the script is loaded successfully, 1 (false) otherwise.
#######################################
script_load() {
local filename="$1"
if filepath=$(file_lookup "$filename"); then
source "$filepath"
else
return 1
fi
}
34 changes: 34 additions & 0 deletions tests/helpers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,37 @@ setup() {
assert_failure
assert_output ""
}

# Test: script_dir should return the directory of the script
@test "script_dir returns the directory of the script" {
run script_dir
assert_success
assert_line --partial "utils-sh"
}

# Test: file_lookup should return 0 if the command exists
@test "file_lookup returns 0 if the command exists" {
run file_lookup "bats"
assert_success
}

# Test: file_lookup should return 1 if the command does not exist
@test "file_lookup returns 1 if the command does not exist" {
run file_lookup "nonexistentcommand"
assert_failure
}

# Test: script_run should execute a command and return its output
@test "script_run executes a command and returns its output" {
run script_run echo "Hello"
assert_success
assert_output "Hello"
}

# Test: script_load should source a file and make its functions available
@test "script_load sources a file and makes its functions available" {
run script_load "../src/helpers.sh"
assert_success
run is_set "Hello"
assert_success
}

0 comments on commit e11803b

Please sign in to comment.