Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
fb7a8a20d2 | |||
4e2c4cbf9b | |||
6b6fc3309e | |||
d6c6baa920 | |||
bcea07f134 | |||
aaa796d4b4 | |||
7ae0f2de57 | |||
e709e4b56a | |||
171e538c37 | |||
124ae12aaf | |||
eb626d0f90 | |||
bf303a608b | |||
470a8daba6 | |||
2787968487 | |||
ac939e3bb9 | |||
21024f24af |
@@ -1,8 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
## Todo
|
||||
## [0.3](https://winklerfamilie.eu/git/efelon/rdlink/releases/tag/v0.3) - 2022-03-23
|
||||
|
||||
- [ ] Tests on different platforms (e.g. busybox)
|
||||
* Improving test output to include rdlink version and test environment
|
||||
* New script `test/savetest.sh` to produce reports which can be added to the release
|
||||
|
||||
## [0.2](https://winklerfamilie.eu/git/efelon/rdlink/releases/tag/v0.2) - 2022-03-22
|
||||
|
||||
|
@@ -5,6 +5,8 @@ Replicate `readlink -f` as bash script using mainly dependencies available on mo
|
||||
* `readlink` (without options)
|
||||
* `cd`
|
||||
* `pwd -P`
|
||||
* `dirname`
|
||||
* `basename`
|
||||
* `test -e` (as `[ -e <file> ]`)
|
||||
|
||||
# Known issues
|
||||
|
119
build.sh
119
build.sh
@@ -1,22 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
build::rdlink() {
|
||||
readonly build_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||
readonly rdlinkRelease="${build_dir}/release/rdlink.sh"
|
||||
readonly color_green='\033[1;32m'
|
||||
readonly color_red='\033[1;31m'
|
||||
readonly color_less='\033[0m'
|
||||
readonly shellcheck_cmd="$(command -v shellcheck)"
|
||||
readonly fileHeader="#!/usr/bin/env bash
|
||||
|
||||
# rdlink [OPTIONS] <PATH>
|
||||
# [OPTION]
|
||||
# -- : End of options marker
|
||||
# -* : Other options are ignored
|
||||
#
|
||||
# License: GNU GPL V3 or later
|
||||
# Author: Martin Winkler
|
||||
#
|
||||
build::addLicense() {
|
||||
local target="${1:?}"
|
||||
local licenseText="#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
@@ -31,51 +17,118 @@ build::rdlink() {
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"
|
||||
printf '%s' "${licenseText}" >> "${target}"
|
||||
}
|
||||
|
||||
build::headerRdlink() {
|
||||
local target="${1:?}"
|
||||
local fileHeader="#!/usr/bin/env bash
|
||||
|
||||
# rdlink.sh [OPTIONS] <PATH>
|
||||
# [OPTION]
|
||||
# -- : End of options marker
|
||||
# -* : Other options are ignored
|
||||
#
|
||||
# Version: $(git describe --long --tags --always --dirty)
|
||||
# Author: Martin Winkler
|
||||
# Origin: https://winklerfamilie.eu/git/efelon/rdlink.git
|
||||
"
|
||||
printf '%s' "${fileHeader}" > "${target}"
|
||||
build::addLicense "${target}"
|
||||
}
|
||||
|
||||
build::headerStarter() {
|
||||
local target="${1:?}"
|
||||
local fileHeader="#!/usr/bin/env bash
|
||||
|
||||
# starter.sh
|
||||
#
|
||||
# Start or source your application at the end to have the
|
||||
# environment variable \$_origin available.
|
||||
#
|
||||
# Version: $(git describe --long --tags --always --dirty)
|
||||
# Author: Martin Winkler
|
||||
# Origin: https://winklerfamilie.eu/git/efelon/rdlink.git
|
||||
"
|
||||
printf '%s' "${fileHeader}" > "${target}"
|
||||
build::addLicense "${target}"
|
||||
}
|
||||
|
||||
build::rdlink() {
|
||||
readonly build_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||
readonly rdlinkRelease="${build_dir}/release/rdlink.sh"
|
||||
readonly starterRelease="${build_dir}/release/starter.sh"
|
||||
readonly color_green='\033[1;32m'
|
||||
readonly color_red='\033[1;31m'
|
||||
readonly color_less='\033[0m'
|
||||
readonly shellcheck_cmd="$(command -v shellcheck)"
|
||||
local testresult_graphic=
|
||||
testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓
|
||||
testresult_graphic_fail="${color_red}✗${color_less}" # alt. symbol ≠
|
||||
|
||||
if ! mkdir -p "$(dirname -- "${rdlinkRelease}")" ; then
|
||||
printf " [ERROR] Cannot create release directory\n"
|
||||
printf ' [ERROR] Cannot create release directory\n'
|
||||
return 1
|
||||
fi
|
||||
|
||||
printf "# Building rdlink release\n"
|
||||
printf '# Building releases based on rdlink.sh\n'
|
||||
|
||||
# Write header
|
||||
printf "%s" "${fileHeader}" > "${rdlinkRelease}"
|
||||
|
||||
printf " Extracting script part and removing debug information..."
|
||||
build::headerRdlink "${rdlinkRelease}"
|
||||
printf ' (Extracting script part and removing debug information)\n'
|
||||
printf ' Building release/rdlink.sh...'
|
||||
# Get essential script content
|
||||
# * extract script part
|
||||
# * remove script marker
|
||||
# * remove debug function calls
|
||||
# * remove debug options from rdlink
|
||||
# * replace multiple empty lines with one
|
||||
#if cat "${build_dir}/rdlink.sh" \
|
||||
# | sed -n '/### Script.*/,/### Script EOF/ p' \
|
||||
# | sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \
|
||||
# | sed '/^$/N; /^\n$/D' \
|
||||
# >> "${rdlinkRelease}" 2>/dev/null ; then
|
||||
if sed -n '/### Script.*/,/### Script EOF/ p' < "${build_dir}/rdlink.sh" \
|
||||
| sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \
|
||||
| sed '/^$/N; /^\n$/D' \
|
||||
>> "${rdlinkRelease}" 2>/dev/null ; then
|
||||
printf "%b" "${testresult_graphic}\n"
|
||||
printf '%b' "${testresult_graphic}\n"
|
||||
else
|
||||
printf "%b" "${testresult_graphic_fail}\n"
|
||||
printf '%b' "${testresult_graphic_fail}\n"
|
||||
return 1
|
||||
fi
|
||||
|
||||
{
|
||||
build::headerStarter "${starterRelease}"
|
||||
printf ' Building release/starter.sh...'
|
||||
if sed -n '/### Script.*/,/### Script Starter EOF/ p' < "${build_dir}/rdlink.sh" \
|
||||
| sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \
|
||||
| sed '/^$/N; /^\n$/D' \
|
||||
>> "${starterRelease}" 2>/dev/null ; then
|
||||
printf '%b' "${testresult_graphic}\n"
|
||||
else
|
||||
printf '%b' "${testresult_graphic_fail}\n"
|
||||
return 1
|
||||
fi
|
||||
|
||||
printf '_origin="$(cd "$(dirname -- "$(rl::rdlink "${BASH_SOURCE[0]}")")" >/dev/null 2>&1 && pwd)"\n' >> "${starterRelease}"
|
||||
printf 'export _origin\n\n' >> "${starterRelease}"
|
||||
|
||||
printf '# Uncomment one of the next lines and replace "tostart.sh" with your script\n' >> "${starterRelease}"
|
||||
printf '# to have _origin available even on platforms not supporting readlink -f.\n\n' >> "${starterRelease}"
|
||||
printf '#bash "${_origin}/tostart.sh"\n' >> "${starterRelease}"
|
||||
printf '#. "${_origin}/tostart.sh"\n' >> "${starterRelease}"
|
||||
printf '#"${_origin}/tostart.sh"\n' >> "${starterRelease}"
|
||||
}
|
||||
|
||||
if [[ "${shellcheck_cmd}" ]] ; then
|
||||
printf "%b\n" " Running shellcheck...\033[1A\033[s\n"
|
||||
printf '%b\n' " Running shellcheck for rdlink...\033[1A\033[s\n"
|
||||
if "${shellcheck_cmd}" "${rdlinkRelease}" ; then
|
||||
printf "%b" "\033[u${testresult_graphic}\n"
|
||||
printf '%b' "\033[u${testresult_graphic}\n"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
printf '%b\n' " Running shellcheck for starter...\033[1A\033[s\n"
|
||||
if "${shellcheck_cmd}" "${starterRelease}" ; then
|
||||
printf '%b' "\033[u${testresult_graphic}\n"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
chmod +x "${rdlinkRelease}"
|
||||
return 0
|
||||
}
|
||||
|
45
rdlink.sh
45
rdlink.sh
@@ -24,7 +24,7 @@ function rl::log () {
|
||||
local log_line=""
|
||||
|
||||
while IFS=$'\n' read -r log_line; do
|
||||
printf "%s [%9s] %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
|
||||
printf '%s [%9s] %s\n' "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
|
||||
done <<< "${@:-}"
|
||||
}
|
||||
emergency() { rl::log emergency "${@}"; exit 1; }
|
||||
@@ -39,13 +39,16 @@ msg() { echo "$@"; true; }
|
||||
|
||||
### Script
|
||||
rl::rdlink() {
|
||||
local subject=
|
||||
local subject="${1:-}"
|
||||
local work=
|
||||
|
||||
info "Processing: $*"
|
||||
info "Processing: ${subject}"
|
||||
info " with pwd: $(pwd)"
|
||||
|
||||
subject="$(rl::cleanpath "${1:-}")" || true
|
||||
# Remove multiple /
|
||||
while [[ "${subject:-}" = *"//"* ]]; do
|
||||
subject="${subject/\/\///}"
|
||||
done
|
||||
|
||||
# Follow multiple symlinks
|
||||
while subject="$(rl::quicklink "${subject}")" ; do
|
||||
@@ -60,7 +63,7 @@ rl::rdlink() {
|
||||
# (e.g. /proc/**/cwd)
|
||||
if [ -L "${subject}" ] ; then
|
||||
info " rl::rdlink exit - can't access link ${subject}"
|
||||
printf "\n"
|
||||
printf '\n'
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
@@ -68,13 +71,13 @@ rl::rdlink() {
|
||||
# Empty output if (dirname $subject) is not a valid path
|
||||
if ! work="$(rl::canon "${subject}")" ; then
|
||||
info " rl::rdlink exit - invalid path ${work}"
|
||||
printf "\n"
|
||||
printf '\n'
|
||||
return 1
|
||||
else
|
||||
subject="${work}"
|
||||
fi
|
||||
|
||||
printf "%s\n" "${subject}"
|
||||
printf '%s\n' "${subject}"
|
||||
}
|
||||
|
||||
rl::quicklink() {
|
||||
@@ -85,7 +88,7 @@ rl::quicklink() {
|
||||
|
||||
# Check if current candidate is a symlink
|
||||
if ! subject=$(readlink -- "${1:-}"); then
|
||||
printf -- "%s\n" "${1:-}"
|
||||
printf -- '%s\n' "${1:-}"
|
||||
return 1
|
||||
fi
|
||||
info " rl::quicklink symlink ${1} -> ${subject}"
|
||||
@@ -97,7 +100,7 @@ rl::quicklink() {
|
||||
info " rl::quicklink relative link resolved: ${subject}"
|
||||
fi
|
||||
|
||||
printf "%s\n" "${subject}"
|
||||
printf '%s\n' "${subject}"
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -124,7 +127,7 @@ rl::canon() {
|
||||
bname="$(basename -- "${start}")"
|
||||
|
||||
# Special: / produces //
|
||||
[[ "${work}" == "/" ]] && work=
|
||||
[[ "${work}" = "/" ]] && work=
|
||||
|
||||
subject="${work}${bname:+"/${bname}"}"
|
||||
info " rl::canon valid parent: ${subject}"
|
||||
@@ -156,26 +159,10 @@ rl::canon() {
|
||||
fi
|
||||
done
|
||||
|
||||
printf -- "%s\n" "${subject}"
|
||||
printf -- '%s\n' "${subject}"
|
||||
return ${retval}
|
||||
}
|
||||
|
||||
rl::cleanpath() {
|
||||
local work=
|
||||
local rex_tmp=
|
||||
|
||||
info "Cleaning path... ${1:-}"
|
||||
work="${1:-}"
|
||||
|
||||
# Remove multiple /
|
||||
while [[ "${work:-}" = *"//"* ]]; do
|
||||
work="${work/\/\///}"
|
||||
done
|
||||
|
||||
info " rl::cleanpath result: ${work}"
|
||||
printf -- "%s\n" "${work}"
|
||||
}
|
||||
|
||||
rl::normalize() {
|
||||
local work=
|
||||
|
||||
@@ -204,9 +191,11 @@ rl::normalize() {
|
||||
fi
|
||||
|
||||
info " rl::normalize result: ${work}"
|
||||
printf -- "%s\n" "${work}"
|
||||
printf -- '%s\n' "${work}"
|
||||
}
|
||||
|
||||
### Script Starter EOF
|
||||
|
||||
rl::main() {
|
||||
local file=
|
||||
local arg=
|
||||
|
1
test/.gitignore
vendored
Normal file
1
test/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
results
|
151
test/distro.sh
Normal file
151
test/distro.sh
Normal file
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env sh
|
||||
# Detects which OS and if it is Linux then it will detect which Linux Distribution.
|
||||
|
||||
OS=`uname -s`
|
||||
REV=`uname -r`
|
||||
MACH=`uname -m`
|
||||
|
||||
if [ "${OS}" = "SunOS" ] ; then
|
||||
OS=Solaris
|
||||
ARCH=`uname -p`
|
||||
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
|
||||
|
||||
elif [ "${OS}" = "AIX" ] ; then
|
||||
OSSTR="${OS} `oslevel` (`oslevel -r`)"
|
||||
|
||||
elif [ "${OS}" = "Linux" ] ; then
|
||||
KERNEL=`uname -r`
|
||||
|
||||
if [ -f /etc/fedora-release ]; then
|
||||
DIST=$(cat /etc/fedora-release | awk '{print $1}')
|
||||
REV=`cat /etc/fedora-release | sed s/.*release\ // | sed s/\ .*//`
|
||||
|
||||
elif [ -f /etc/redhat-release ] ; then
|
||||
DIST=$(cat /etc/redhat-release | awk '{print $1}')
|
||||
if [ "${DIST}" = "CentOS" ]; then
|
||||
DIST="CentOS"
|
||||
IGNORE_OS_RELEASE=1 # https://bugs.centos.org/view.php?id=8359
|
||||
elif [ "${DIST}" = "CloudLinux" ]; then
|
||||
DIST="CloudLinux"
|
||||
elif [ "${DIST}" = "Mandriva" ]; then
|
||||
DIST="Mandriva"
|
||||
PSEUDONAME=`cat /etc/mandriva-release | sed s/.*\(// | sed s/\)//`
|
||||
REV=`cat /etc/mandriva-release | sed s/.*release\ // | sed s/\ .*//`
|
||||
elif [ -f /etc/oracle-release ]; then
|
||||
DIST="Oracle"
|
||||
elif [ -f /etc/rockstor-release ]; then
|
||||
DIST="Rockstor"
|
||||
else
|
||||
DIST="RedHat"
|
||||
fi
|
||||
|
||||
PSEUDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
|
||||
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
|
||||
|
||||
elif [ -f /etc/mandrake-release ] ; then
|
||||
DIST='Mandrake'
|
||||
PSEUDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
|
||||
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
|
||||
|
||||
elif [ -f /etc/devuan_version ] ; then
|
||||
DIST="Devuan `cat /etc/devuan_version`"
|
||||
REV=""
|
||||
|
||||
elif [ -f /etc/debian_version ] ; then
|
||||
DIST="Debian `cat /etc/debian_version`"
|
||||
REV=""
|
||||
IGNORE_OS_RELEASE=1
|
||||
if [ -f /usr/bin/lsb_release ] ; then
|
||||
ID=`lsb_release -i | awk -F ':' '{print $2}' | sed 's/ //g'`
|
||||
fi
|
||||
if [ "${ID}" = "Raspbian" ] ; then
|
||||
DIST="Raspbian `cat /etc/debian_version`"
|
||||
fi
|
||||
if [ -f /usr/bin/pveversion ]; then
|
||||
DIST="${DIST}/PVE `/usr/bin/pveversion | cut -d '/' -f 2`"
|
||||
fi
|
||||
if [ -f /usr/bin/pmgversion ]; then
|
||||
# pmgversion requires root permissions to run, please add NOPASSWD setting to visudo.
|
||||
DIST="${DIST}/PMG `sudo /usr/bin/pmgversion | cut -d '/' -f 2`"
|
||||
fi
|
||||
if [ -f /etc/dogtag ]; then
|
||||
DIST=`cat /etc/dogtag`
|
||||
fi
|
||||
|
||||
elif [ -f /etc/gentoo-release ] ; then
|
||||
DIST="Gentoo"
|
||||
REV=$(tr -d '[[:alpha:]]' </etc/gentoo-release | tr -d " ")
|
||||
|
||||
elif [ -f /etc/arch-release ] ; then
|
||||
DIST="Arch Linux"
|
||||
REV="" # Omit version since Arch Linux uses rolling releases
|
||||
IGNORE_LSB=1 # /etc/lsb-release would overwrite $REV with "rolling"
|
||||
|
||||
elif [ -f /etc/photon-release ] ; then
|
||||
DIST=$(head -1 < /etc/photon-release)
|
||||
REV=$(sed -n -e 's/^.*PHOTON_BUILD_NUMBER=//p' /etc/photon-release)
|
||||
IGNORE_LSB=1 # photon os does not have /etc/lsb-release nor lsb_release
|
||||
|
||||
elif [ -f /etc/openwrt_version ] ; then
|
||||
DIST="OpenWrt"
|
||||
REV=$(cat /etc/openwrt_version)
|
||||
|
||||
elif [ -f /etc/pld-release ] ; then
|
||||
DIST=$(cat /etc/pld-release)
|
||||
REV=""
|
||||
|
||||
elif [ -f /etc/SuSE-release ] ; then
|
||||
DIST=$(echo SLES $(grep VERSION /etc/SuSE-release | cut -d = -f 2 | tr -d " "))
|
||||
REV=$(echo SP$(grep PATCHLEVEL /etc/SuSE-release | cut -d = -f 2 | tr -d " "))
|
||||
fi
|
||||
|
||||
if [ -x "$(command -v awk)" ]; then # some distros do not ship with awk
|
||||
if [ "`uname -a | awk '{print $(NF)}'`" = "DD-WRT" ] ; then
|
||||
DIST="dd-wrt"
|
||||
fi
|
||||
if [ "`uname -a | awk '{print $(NF)}'`" = "ASUSWRT-Merlin" ] ; then
|
||||
DIST="ASUSWRT-Merlin"
|
||||
REV=`nvram show | grep buildno= | egrep -o '[0-9].[0-9].[0-9]'` > /dev/null 2>&1
|
||||
fi
|
||||
fi
|
||||
|
||||
# try standardized os version methods
|
||||
if [ -f /etc/os-release -a "${IGNORE_OS_RELEASE}" != 1 ] ; then
|
||||
. /etc/os-release
|
||||
STD_DIST="$NAME"
|
||||
STD_REV="$VERSION_ID"
|
||||
elif [ -f /etc/lsb-release -a "${IGNORE_LSB}" != 1 ] ; then
|
||||
STD_DIST=$(lsb_release -si)
|
||||
STD_REV=$(lsb_release -sr)
|
||||
fi
|
||||
if [ -n "${STD_DIST}" ]; then
|
||||
DIST="${STD_DIST}"
|
||||
fi
|
||||
if [ -n "${STD_REV}" ]; then
|
||||
REV="${STD_REV}"
|
||||
fi
|
||||
|
||||
if [ -n "${REV}" ]; then
|
||||
OSSTR="${DIST} ${REV}"
|
||||
else
|
||||
OSSTR="${DIST}"
|
||||
fi
|
||||
|
||||
elif [ "${OS}" = "Darwin" ] ; then
|
||||
if [ -f /usr/bin/sw_vers ] ; then
|
||||
OSSTR=`/usr/bin/sw_vers|grep -v Build|sed 's/^.*:.//'| tr "\n" ' '`
|
||||
fi
|
||||
|
||||
elif [ "${OS}" = "FreeBSD" ] ; then
|
||||
if [ -f /etc/version ] ; then
|
||||
DIST=$(cat /etc/version | cut -d'-' -f 1)
|
||||
if [ "${DIST}" = "FreeNAS" ]; then
|
||||
OSSTR=`cat /etc/version | cut -d' ' -f 1`
|
||||
fi
|
||||
else
|
||||
OSSTR=`/usr/bin/uname -mior`
|
||||
fi
|
||||
fi
|
||||
|
||||
echo ${OSSTR}
|
||||
|
25
test/getenv.sh
Executable file
25
test/getenv.sh
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
get_os_version() {
|
||||
readonly getenv_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||
local utils_version=
|
||||
local os_version=
|
||||
|
||||
os_version="$(bash "${getenv_dir}/distro.sh" | sed 's/ /_/g; s/\//-/g')"
|
||||
|
||||
bash_version="$(bash --version | sed -n '/bash, [Vv]ersion/p' | sed 's/.*[Vv]ersion \([0-9.]\+\).*/\1/g')"
|
||||
|
||||
if utils_version="$(readlink --version 2>&1 | grep "BusyBox")" ; then
|
||||
utils_version="$(sed 's/\(BusyBox v[0-9\.]\+\).*/\1/g' <<<${utils_version})"
|
||||
elif utils_version="$(readlink --version 2>&1 | grep "GNU core")" ; then
|
||||
utils_version="$(sed 's/.*\(GNU [[:alnum:]]\+\). \([0-9\.]\+\).*/\1 \2/g' <<<${utils_version})"
|
||||
else
|
||||
utils_version="utils_unkown"
|
||||
fi
|
||||
# Replace " " with _
|
||||
utils_version="${utils_version// /_}"
|
||||
|
||||
echo "${os_version:-"$(uname -s)"}_${bash_version:+bash_${bash_version}}_${utils_version}"
|
||||
}
|
||||
|
||||
get_os_version
|
24
test/savetest.sh
Executable file
24
test/savetest.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
savetest() {
|
||||
local target_file=
|
||||
local environment=
|
||||
local failed_tests=0
|
||||
|
||||
readonly savetest_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||
|
||||
target_dir="${savetest_dir}/results"
|
||||
mkdir -p "${target_dir}"
|
||||
|
||||
environment="$("${savetest_dir}/getenv.sh")"
|
||||
target_file="${target_dir}/${environment:-"Unknown_Environemnt"}_result.log"
|
||||
|
||||
printf "# Test environment\n\n %s\n\n" "${environment}" >"${target_file}"
|
||||
|
||||
"${savetest_dir}/test_rdlink.sh" -a >>"${target_file}" 2>&1
|
||||
failed_tests="$?"
|
||||
(( failed_tests )) && printf "Failed tests: %s\n" "$failed_tests"
|
||||
return $failed_tests
|
||||
}
|
||||
|
||||
savetest
|
@@ -17,17 +17,19 @@
|
||||
# Internal tests will be executed if no arguments are found
|
||||
|
||||
readonly test_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||
readonly tool_rdlink="${test_dir}/../release/rdlink.sh"
|
||||
readonly tool_rdlink="${test_dir}/../rdlink.sh"
|
||||
readonly tool_readlink="$(command -v readlink) -f --"
|
||||
readonly build_cmd="${test_dir}/../build.sh"
|
||||
readonly tool_rdlink_version="$(git describe --long --tags --always --dirty)"
|
||||
|
||||
readonly config_path_width=45
|
||||
|
||||
readonly color_red='\033[1;31m'
|
||||
readonly color_yellow='\033[1;33m'
|
||||
readonly color_green='\033[1;32m'
|
||||
readonly color_less='\033[0m'
|
||||
color_red= ; [ -t 1 ] && color_red='\033[1;31m'
|
||||
color_yellow= ; [ -t 1 ] && color_yellow='\033[1;33m'
|
||||
color_green= ; [ -t 1 ] && color_green='\033[1;32m'
|
||||
color_blue= ; [ -t 1 ] && color_blue='\033[1;34m'
|
||||
color_less= ; [ -t 1 ] && color_less='\033[0m'
|
||||
|
||||
flag_title=0
|
||||
flag_runall=0
|
||||
flag_extendedOutput=0
|
||||
flag_verbose=0
|
||||
@@ -40,6 +42,149 @@ tocompete=()
|
||||
|
||||
. "${test_dir}/totest.sh"
|
||||
|
||||
rl::test() {
|
||||
local i=0
|
||||
local path=
|
||||
local arraywalker=
|
||||
local firstelement=
|
||||
local testend=0
|
||||
local arg=
|
||||
local excludemsg=
|
||||
local tests_success=0
|
||||
local tests_failed=0
|
||||
local tests_skipped=0
|
||||
|
||||
for arg in "$@"; do
|
||||
case "$1" in
|
||||
--) ## End of options
|
||||
shift && break ;;
|
||||
-a|--run-all) ## Run all even if tests fail
|
||||
flag_runall=1
|
||||
shift ;;
|
||||
-x|--extended-output)
|
||||
flag_extendedOutput=1
|
||||
shift ;;
|
||||
-xd|--verbose)
|
||||
flag_extendedOutput=1
|
||||
flag_verbose=1
|
||||
shift ;;
|
||||
-oa|--only-assert)
|
||||
flag_onlyassert=1
|
||||
shift ;;
|
||||
-oc|--only-compete)
|
||||
flag_onlycompete=1
|
||||
shift ;;
|
||||
-e|--error)
|
||||
flag_printerror=1
|
||||
shift ;;
|
||||
-*|--*)
|
||||
printf "Invalid argument\n"
|
||||
exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check if assertion tests need to be performed
|
||||
(( ! flag_onlycompete )) && [[ ! "$@" ]] && toassert_init
|
||||
|
||||
# Compare against expected result
|
||||
if (( ${#toassert[@]} )) ; then
|
||||
rl::printTitle
|
||||
printf "\n## Assertion tests (\"Expected path\" == rdlink)\n"
|
||||
fi
|
||||
for testarray in "${toassert[@]}"; do
|
||||
i=0
|
||||
arraywalker="$testarray"[@]
|
||||
for path in "${!arraywalker}"; do
|
||||
if (( ! i )); then
|
||||
# Print title in array element 0
|
||||
printf "### %b\n" "${color_yellow}${path}${color_less}"
|
||||
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
||||
|
||||
elif [ -z "$firstelement" ]; then
|
||||
# Save first element for string compare
|
||||
firstelement="${path}"
|
||||
|
||||
elif excludemsg="$(toexclude "${firstelement}")"; then
|
||||
# Current path is excluded
|
||||
(( tests_skipped++ ))
|
||||
rl::printPath "$(( i/2 ))" "${firstelement}" && printf " %b skip (%s)\n" "🛇" "${excludemsg}"
|
||||
else
|
||||
# Execute test case
|
||||
if ! rl::testcmp "$(( i/2 ))" "${firstelement}" \
|
||||
"${path}" "$($tool_rdlink -- "${firstelement}")"; then
|
||||
(( tests_failed++ ))
|
||||
# Run all tests if option -a is pressend
|
||||
(( ! $flag_runall )) && testend=1 && break
|
||||
else
|
||||
(( tests_success++ ))
|
||||
fi
|
||||
|
||||
firstelement=
|
||||
fi
|
||||
((i++))
|
||||
done
|
||||
(( testend )) && break
|
||||
done
|
||||
(( testend )) && rl::printTestSummary $tests_success $tests_failed $tests_skipped && return 1
|
||||
|
||||
# Initialize competition tests
|
||||
(( ! flag_onlyassert )) && tocompete_init
|
||||
|
||||
# Only run `compete_args` if arguments are available
|
||||
if [[ "$@" ]]; then
|
||||
compete_args=( "Tests from command line" "$@" )
|
||||
tocompete=(compete_args)
|
||||
fi
|
||||
|
||||
# Compare output of rdlink and readlink -f
|
||||
if (( ${#tocompete[@]} )) ; then
|
||||
rl::printTitle
|
||||
printf "\n## Competition tests (readlink -f == rdlink)\n"
|
||||
fi
|
||||
for testarray in "${tocompete[@]}"; do
|
||||
i=0
|
||||
arraywalker="$testarray"[@]
|
||||
for path in "${!arraywalker}"; do
|
||||
|
||||
if (( ! i )); then
|
||||
# Print title in array element 0
|
||||
printf "### %b\n" "${color_yellow}${path}${color_less}"
|
||||
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
||||
|
||||
else
|
||||
if excludemsg="$(toexclude "${path}")" ; then
|
||||
# Current path is excluded
|
||||
(( tests_skipped++ ))
|
||||
rl::printPath "${i}" "${path}" && printf " %b skip (%s)\n" "🛇" "${excludemsg}"
|
||||
elif ! rl::testcmp "${i}" "${path}" \
|
||||
"$(${tool_readlink} "$path")" "$(${tool_rdlink} -- "$path")"; then
|
||||
# Test case failed
|
||||
(( tests_failed++ ))
|
||||
# Run all tests if option -a is pressend
|
||||
(( ! $flag_runall )) && testend=1 && break
|
||||
else
|
||||
(( tests_success++ ))
|
||||
fi
|
||||
fi
|
||||
|
||||
((i++))
|
||||
done
|
||||
(( testend )) && break
|
||||
done
|
||||
|
||||
rl::printTestSummary $tests_success $tests_failed $tests_skipped
|
||||
return ${tests_failed}
|
||||
}
|
||||
|
||||
rl::printTitle() {
|
||||
(( flag_title )) && return 0
|
||||
flag_title=1
|
||||
|
||||
# See if some tests will be performed
|
||||
printf "# Testing rdlink version %s\n\n" "${tool_rdlink_version}"
|
||||
printf " Testdate: %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")"
|
||||
}
|
||||
|
||||
rl::printPath() {
|
||||
local testnum="${1:-0}"
|
||||
local input="${2:-"-"}"
|
||||
@@ -83,18 +228,20 @@ rl::testcmp() {
|
||||
local actual="${4:-"-"}"
|
||||
local testresult=0 #failed
|
||||
local testresult_graphic="${color_red}✗${color_less}" # alt. symbol ≠
|
||||
local link="$(readlink -- "${input}")"
|
||||
|
||||
if [[ "${expect}" == "${actual}" ]] ; then
|
||||
if [[ "${actual}" =~ ${expect} ]] ; then
|
||||
# Don't print success for this flag
|
||||
(( flag_printerror )) && return 0
|
||||
testresult=1
|
||||
testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓
|
||||
fi
|
||||
|
||||
# Show only reslult as quick overview
|
||||
# Show result as table (without debug)
|
||||
if (( flag_extendedOutput )); then
|
||||
rl::printPath ${testnum} "${input}"
|
||||
printf " %b\n" "${testresult_graphic}"
|
||||
[[ "$link" ]] && printf "%10s %b %s%b\n" "" "${color_blue}⤷" "${link}" "${color_less}"
|
||||
if (( ! testresult )); then
|
||||
# Test failed
|
||||
printf " Result: %s\n Exp: %s\n" "${actual}" "${expect}"
|
||||
@@ -115,6 +262,7 @@ rl::testcmp() {
|
||||
(( ! testresult )) && testresult_graphic="→" # "${color_green}→${color_less}"
|
||||
rl::printPath "" "${input}"
|
||||
printf " %b %s\n" "${testresult_graphic}" "${expect}"
|
||||
[[ "$link" ]] && printf "%b" "${color_blue}" && rl::printPath "" "${link}⤶" && printf " %b\n" "${color_less}" #⤷
|
||||
# Print actual result if test failed
|
||||
if (( ! testresult )); then
|
||||
printf "%$((config_path_width))s %b %s\n" " " "${color_red}✗${color_less}" "${actual}"
|
||||
@@ -123,139 +271,6 @@ rl::testcmp() {
|
||||
return 0
|
||||
}
|
||||
|
||||
rl::test() {
|
||||
local i=0
|
||||
local path=
|
||||
local arraywalker=
|
||||
local firstelement=
|
||||
local testend=0
|
||||
local arg=
|
||||
local excludemsg=
|
||||
local tests_success=0
|
||||
local tests_failed=0
|
||||
local tests_skipped=0
|
||||
|
||||
for arg in "$@"; do
|
||||
case "$1" in
|
||||
--) ## End of options
|
||||
shift && break ;;
|
||||
-a|--run-all) ## Run all even if tests fail
|
||||
flag_runall=1
|
||||
shift ;;
|
||||
-x|--extended-output)
|
||||
flag_extendedOutput=1
|
||||
shift ;;
|
||||
-xd|--verbose)
|
||||
flag_extendedOutput=1
|
||||
flag_verbose=1
|
||||
shift ;;
|
||||
-oa|--only-assert)
|
||||
flag_onlyassert=1
|
||||
shift ;;
|
||||
-oc|--only-compete)
|
||||
flag_onlycompete=1
|
||||
shift ;;
|
||||
-e|--error)
|
||||
flag_printerror=1
|
||||
shift ;;
|
||||
-*|--*)
|
||||
printf "Invalid argument\n"
|
||||
exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ ! -e "${tool_rdlink}" ]] && "${build_cmd}"
|
||||
|
||||
# Cmd line arguments
|
||||
(( ! flag_onlycompete )) && [[ ! "$@" ]] && toassert_init
|
||||
|
||||
# Compare against expected result
|
||||
if (( ${#toassert[@]} )) ; then
|
||||
printf "\n# Assertion tests (\"Expected path\" == rdlink)\n"
|
||||
fi
|
||||
for testarray in "${toassert[@]}"; do
|
||||
i=0
|
||||
arraywalker="$testarray"[@]
|
||||
for path in "${!arraywalker}"; do
|
||||
if (( ! i )); then
|
||||
# Print title in array element 0
|
||||
printf " ### %b ###\n" "${color_yellow}${path}${color_less}"
|
||||
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
||||
|
||||
elif [ -z "$firstelement" ]; then
|
||||
# Save first element for string compare
|
||||
firstelement="${path}"
|
||||
|
||||
elif excludemsg="$(toexclude "${firstelement}")"; then
|
||||
# Current path is excluded
|
||||
(( tests_skipped++ ))
|
||||
rl::printPath "$(( i/2 ))" "${firstelement}" && printf " %b skip (%s)\n" "🛇" "${excludemsg}"
|
||||
else
|
||||
# Execute test case
|
||||
if ! rl::testcmp "$(( i/2 ))" "${firstelement}" \
|
||||
"${path}" "$($tool_rdlink -- "${firstelement}")"; then
|
||||
(( tests_failed++ ))
|
||||
# Run all tests if option -a is pressend
|
||||
(( ! $flag_runall )) && testend=1 && break
|
||||
else
|
||||
(( tests_success++ ))
|
||||
fi
|
||||
|
||||
firstelement=
|
||||
fi
|
||||
((i++))
|
||||
done
|
||||
(( testend )) && break
|
||||
done
|
||||
(( testend )) && rl::printTestSummary $tests_success $tests_failed $tests_skipped && return 1
|
||||
|
||||
# Initialize competition tests
|
||||
(( ! flag_onlyassert )) && tocompete_init
|
||||
|
||||
# Only run `compete_args` if arguments are available
|
||||
if [[ "$@" ]]; then
|
||||
compete_args=( "Tests from command line" "$@" )
|
||||
tocompete=(compete_args)
|
||||
fi
|
||||
|
||||
# Compare output of rdlink and readlink -f
|
||||
if (( ${#tocompete[@]} )) ; then
|
||||
printf "\n# Competition tests (readlink -f == rdlink)\n"
|
||||
fi
|
||||
for testarray in "${tocompete[@]}"; do
|
||||
i=0
|
||||
arraywalker="$testarray"[@]
|
||||
for path in "${!arraywalker}"; do
|
||||
|
||||
if (( ! i )); then
|
||||
# Print title in array element 0
|
||||
printf " ### %b ###\n" "${color_yellow}${path}${color_less}"
|
||||
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
||||
|
||||
else
|
||||
if excludemsg="$(toexclude "${path}")" ; then
|
||||
# Current path is excluded
|
||||
(( tests_skipped++ ))
|
||||
rl::printPath "${i}" "${path}" && printf " %b skip (%s)\n" "🛇" "${excludemsg}"
|
||||
elif ! rl::testcmp "${i}" "${path}" \
|
||||
"$(${tool_readlink} "$path")" "$(${tool_rdlink} -- "$path")"; then
|
||||
# Test case failed
|
||||
(( tests_failed++ ))
|
||||
# Run all tests if option -a is pressend
|
||||
(( ! $flag_runall )) && testend=1 && break
|
||||
else
|
||||
(( tests_success++ ))
|
||||
fi
|
||||
fi
|
||||
|
||||
((i++))
|
||||
done
|
||||
(( testend )) && break
|
||||
done
|
||||
|
||||
rl::printTestSummary $tests_success $tests_failed $tests_skipped
|
||||
(( testend )) && return 1 || return 0
|
||||
}
|
||||
|
||||
#time rl::test "$@"
|
||||
rl::test "$@"
|
||||
|
||||
|
@@ -53,9 +53,9 @@ tocompete_init() {
|
||||
ln -s "../dir_3_l/file" "dir_1/file"
|
||||
|
||||
# compete_no_permission
|
||||
mkdir -p "${_tc_tmp}/noperm"
|
||||
chmod 400 "${_tc_tmp}/noperm"
|
||||
ln -s "noperm" "${_tc_tmp}/noperml"
|
||||
mkdir -p "${_tc_tmp}/nopermdir"
|
||||
chmod 400 "${_tc_tmp}/nopermdir"
|
||||
ln -s "nopermdir" "${_tc_tmp}/nopermdirl"
|
||||
ln -s "/" "${_tc_tmp}/lroot"
|
||||
ln -s "/root" "${_tc_tmp}/lroothome"
|
||||
|
||||
@@ -95,14 +95,14 @@ tocompete_init() {
|
||||
)
|
||||
|
||||
compete_no_permission=( "No permission to enter directory (direct and link)"
|
||||
"noperm"*
|
||||
"nopermdir"*
|
||||
"lroot"*
|
||||
"/"
|
||||
"/root"
|
||||
"/root/"
|
||||
"/root/."
|
||||
"/root/.."
|
||||
"/proc/"**/cwd # special - no permission for links
|
||||
#"/proc/"**/cwd # special - no permission for links
|
||||
)
|
||||
|
||||
# Add tests to global test array from test_rdlink
|
||||
@@ -112,7 +112,7 @@ tocompete_init() {
|
||||
compete_relative
|
||||
compete_no_permission
|
||||
compete_links
|
||||
compete_all
|
||||
#compete_all
|
||||
)
|
||||
}
|
||||
|
||||
@@ -132,12 +132,44 @@ _ta_tmp="${test_dir:-"/tmp"}/tmp_assert"
|
||||
# [1] input == [2] expected
|
||||
# [3] input == [4] expected
|
||||
# ...
|
||||
assert_canonicalize=( "Canonicalize invalid path"
|
||||
"///tmp//./b" "/tmp/b"
|
||||
"//tmp//./b/.." ""
|
||||
"//tmp//./b/." ""
|
||||
"//tmp//./b/" "/tmp/b"
|
||||
"///notthere//...../help/." ""
|
||||
)
|
||||
|
||||
|
||||
|
||||
toassert_init() {
|
||||
{
|
||||
# Base directory for the test initialization
|
||||
mkdir -p "${_ta_tmp}"
|
||||
}
|
||||
cd "${_ta_tmp}"
|
||||
|
||||
# assert_no_permission
|
||||
mkdir -p "nopermdir"
|
||||
chmod 400 "nopermdir"
|
||||
ln -s "nopermdir" "nopermdirl"
|
||||
ln -s "/" "lroot"
|
||||
ln -s "/root" "lroothome"
|
||||
|
||||
# assert_links
|
||||
touch "a"
|
||||
ln -s "a" "a 2"
|
||||
ln -s "a 2" "a 3"
|
||||
mkdir -p "dir_1"
|
||||
mkdir -p "dir_2/dir_22"
|
||||
touch "dir_2/dir_22/file"
|
||||
ln -s "dir_2/dir_22" "dir_3_l"
|
||||
ln -s "../dir_3_l/file" "dir_1/file"
|
||||
|
||||
# assert_relative
|
||||
ln -s "../nofile" "nofile_l"
|
||||
ln -s "../nodir/nofile" "nopath_l"
|
||||
}
|
||||
|
||||
# Base directory for the test
|
||||
cd "${_ta_tmp}"
|
||||
|
||||
@@ -150,11 +182,55 @@ toassert_init() {
|
||||
"${_ta_tmp}/miss c" "${_ta_tmp}/miss c"
|
||||
"rel_a" "${_ta_tmp}/rel_a"
|
||||
"../rel_b" "$(cd ".." && pwd)/rel_b"
|
||||
"-v" "$PWD/-v" # Not recommended file naming
|
||||
"../-v" "$(cd "$PWD/.." && pwd)/-v" # Not recommended file naming
|
||||
"-v/.." "" # Not recommended file naming
|
||||
)
|
||||
|
||||
assert_relative=( "Resolving of relative links"
|
||||
# Invalid
|
||||
"nofile_l" "$(cd "$PWD/.." && pwd)/nofile"
|
||||
"nopath_l" ""
|
||||
# Valid
|
||||
"dir_1/file" "$PWD/dir_2/dir_22/file" # Valid relative link to path with relative element
|
||||
)
|
||||
|
||||
assert_no_permission=( "No permission to enter directory (direct and link)"
|
||||
"nopermdir" "$PWD/nopermdir"
|
||||
"nopermdirl" "$PWD/nopermdir"
|
||||
"lroot" "/"
|
||||
"lroothome" "/root"
|
||||
"/" "/"
|
||||
"/root" "/root"
|
||||
"/root/" "/root"
|
||||
"/root/." "/root"
|
||||
"/root/.." "/"
|
||||
)
|
||||
|
||||
assert_links=( "Test - Valid links"
|
||||
"${_ta_tmp}/a 3" "$PWD/a" # slink chain a3 -> a2 -> a
|
||||
"${_ta_tmp}/a" "$PWD/a"
|
||||
"${_ta_tmp}/a 2" "$PWD/a"
|
||||
"/dev/stdin" "/dev/pts/[0-9]+"
|
||||
#"/dev/stdout" "" # skip - Always different ( add for assert )
|
||||
#"/dev/fd" # Test skip - /dev/fd is different on every call
|
||||
#"/etc/mtab" # skip - Always different
|
||||
#"/proc/mounts" # skip - Always different
|
||||
#"/proc/net/"* # skip - Always different
|
||||
)
|
||||
|
||||
assert_force_fail=( "Force fail"
|
||||
"nix" ""
|
||||
)
|
||||
|
||||
# Add test arrays to global test array from test_rdlink
|
||||
toassert+=(
|
||||
assert_canonicalize
|
||||
assert_relative
|
||||
assert_no_permission
|
||||
assert_invalid_files
|
||||
assert_links
|
||||
#assert_force_fail
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user