refactor: split test_lib.sh into test_utils.sh for basic functionalities and test_lib.sh

Signed-off-by: joshjms <joshjms1607@gmail.com>

fix shell scripts

Signed-off-by: joshjms <joshjms1607@gmail.com>
This commit is contained in:
joshjms 2025-03-20 00:14:49 +08:00
parent 495176a033
commit cd077a806d
2 changed files with 94 additions and 90 deletions

View File

@ -2,6 +2,8 @@
set -euo pipefail
source ./scripts/test_utils.sh
ROOT_MODULE="go.etcd.io/etcd"
if [[ "$(go list)" != "${ROOT_MODULE}/v3" ]]; then
@ -15,96 +17,6 @@ function set_root_dir {
set_root_dir
#### Convenient IO methods #####
COLOR_RED='\033[0;31m'
COLOR_ORANGE='\033[0;33m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTCYAN='\033[0;36m'
COLOR_BLUE='\033[0;94m'
COLOR_MAGENTA='\033[95m'
COLOR_BOLD='\033[1m'
COLOR_NONE='\033[0m' # No Color
function log_error {
>&2 echo -n -e "${COLOR_BOLD}${COLOR_RED}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_warning {
>&2 echo -n -e "${COLOR_ORANGE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_callout {
>&2 echo -n -e "${COLOR_LIGHTCYAN}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_cmd {
>&2 echo -n -e "${COLOR_BLUE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_success {
>&2 echo -n -e "${COLOR_GREEN}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_info {
>&2 echo -n -e "${COLOR_NONE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
# From http://stackoverflow.com/a/12498485
function relativePath {
# both $1 and $2 are absolute paths beginning with /
# returns relative path to $2 from $1
local source=$1
local target=$2
local commonPart=$source
local result=""
while [[ "${target#"$commonPart"}" == "${target}" ]]; do
# no match, means that candidate common part is not correct
# go up one level (reduce common part)
commonPart="$(dirname "$commonPart")"
# and record that we went back, with correct / handling
if [[ -z $result ]]; then
result=".."
else
result="../$result"
fi
done
if [[ $commonPart == "/" ]]; then
# special case for root (no common path)
result="$result/"
fi
# since we now have identified the common part,
# compute the non-common part
local forwardPart="${target#"$commonPart"}"
# and now stick all parts together
if [[ -n $result ]] && [[ -n $forwardPart ]]; then
result="$result$forwardPart"
elif [[ -n $forwardPart ]]; then
# extra slash removal
result="${forwardPart:1}"
fi
echo "$result"
}
#### Discovery of files/packages within a go module #####
# go_srcs_in_module

92
scripts/test_utils.sh Normal file
View File

@ -0,0 +1,92 @@
#!/usr/bin/env bash
set -euo pipefail
#### Convenient IO methods #####
export COLOR_RED='\033[0;31m'
export COLOR_ORANGE='\033[0;33m'
export COLOR_GREEN='\033[0;32m'
export COLOR_LIGHTCYAN='\033[0;36m'
export COLOR_BLUE='\033[0;94m'
export COLOR_BOLD='\033[1m'
export COLOR_MAGENTA='\033[95m'
export COLOR_NONE='\033[0m' # No Color
function log_error {
>&2 echo -n -e "${COLOR_BOLD}${COLOR_RED}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_warning {
>&2 echo -n -e "${COLOR_ORANGE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_callout {
>&2 echo -n -e "${COLOR_LIGHTCYAN}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_cmd {
>&2 echo -n -e "${COLOR_BLUE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_success {
>&2 echo -n -e "${COLOR_GREEN}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
function log_info {
>&2 echo -n -e "${COLOR_NONE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
# From http://stackoverflow.com/a/12498485
function relativePath {
# both $1 and $2 are absolute paths beginning with /
# returns relative path to $2 from $1
local source=$1
local target=$2
local commonPart=$source
local result=""
while [[ "${target#"$commonPart"}" == "${target}" ]]; do
# no match, means that candidate common part is not correct
# go up one level (reduce common part)
commonPart="$(dirname "$commonPart")"
# and record that we went back, with correct / handling
if [[ -z $result ]]; then
result=".."
else
result="../$result"
fi
done
if [[ $commonPart == "/" ]]; then
# special case for root (no common path)
result="$result/"
fi
# since we now have identified the common part,
# compute the non-common part
local forwardPart="${target#"$commonPart"}"
# and now stick all parts together
if [[ -n $result ]] && [[ -n $forwardPart ]]; then
result="$result$forwardPart"
elif [[ -n $forwardPart ]]; then
# extra slash removal
result="${forwardPart:1}"
fi
echo "$result"
}