Compare commits
11 Commits
v0.1
...
dev-minima
Author | SHA1 | Date | |
---|---|---|---|
d0664fea6c | |||
124ae12aaf | |||
eb626d0f90 | |||
bf303a608b | |||
470a8daba6 | |||
2787968487 | |||
ac939e3bb9 | |||
21024f24af | |||
a9d0eeef5a | |||
5c367b357a | |||
d20ef76316 |
24
CHANGELOG.md
Normal file
24
CHANGELOG.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Changelog
|
||||
|
||||
## [0.3](https://winklerfamilie.eu/git/efelon/rdlink/releases/tag/v0.3) - 2022-03-23
|
||||
|
||||
* 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
|
||||
|
||||
* Fix pattern substitution bugs (found with bash 3.2.57)
|
||||
* Test successful on
|
||||
* Ubuntu x86_64 (Ubuntu 20.04.4 LTS)
|
||||
**GNU bash, version 3.2.57**
|
||||
|
||||
## [0.1](https://winklerfamilie.eu/git/efelon/rdlink/releases/tag/v0.1) - 2022-03-22
|
||||
|
||||
* First relase
|
||||
* behavior for normal cases same as readlink -f
|
||||
* Tests successful on
|
||||
* Raspberry Pi OS aarch64/arm64 (Debian GNU/Linux 11 (bullseye))
|
||||
**GNU bash, version 5.1.4**
|
||||
* Ubuntu x86_64 (Ubuntu 20.04.4 LTS)
|
||||
**GNU bash, Version 5.0.17**
|
||||
|
@@ -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
|
||||
|
3
build.sh
3
build.sh
@@ -14,8 +14,9 @@ build::rdlink() {
|
||||
# -- : End of options marker
|
||||
# -* : Other options are ignored
|
||||
#
|
||||
# License: GNU GPL V3 or later
|
||||
# Version: $(git describe --long --tags --always --dirty)
|
||||
# Author: Martin Winkler
|
||||
# Origin: https://winklerfamilie.eu/git/efelon/rdlink.git
|
||||
#
|
||||
# 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
|
||||
|
170
rdlink.sh
170
rdlink.sh
@@ -40,65 +40,25 @@ msg() { echo "$@"; true; }
|
||||
### Script
|
||||
rl::rdlink() {
|
||||
local subject=
|
||||
local work=
|
||||
|
||||
info "Processing: $*"
|
||||
info " with pwd: $(pwd)"
|
||||
|
||||
subject="$(rl::cleanpath "${1:-}")" || true
|
||||
|
||||
# Follow multiple symlinks
|
||||
while subject="$(rl::quicklink "${subject}")" ; do
|
||||
: # A link was resolved at least once
|
||||
info " rl::rdlink - Link found: $subject"
|
||||
done
|
||||
|
||||
# Special cases handling
|
||||
{
|
||||
# If subject is still a link, after rl::quicklink call(s)
|
||||
# current user has no permission to access the link itself
|
||||
# (e.g. /proc/**/cwd)
|
||||
if [ -L "${subject}" ] ; then
|
||||
info " rl::rdlink exit - can't access link ${subject}"
|
||||
printf "\n"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 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"
|
||||
return 1
|
||||
else
|
||||
subject="${work}"
|
||||
fi
|
||||
|
||||
subject="${1:-}"
|
||||
while [[ "${subject:-}" = *"//"* ]]; do subject="${subject/\/\///}"; done
|
||||
while subject="$(rl::quicklink "${subject}")" ; do : ; done
|
||||
[ -L "${subject}" ] && printf "\n" && return 1
|
||||
subject="$(rl::canon "${subject}")" || { printf "\n"; return 1; }
|
||||
printf "%s\n" "${subject}"
|
||||
}
|
||||
|
||||
rl::quicklink() {
|
||||
subject=
|
||||
work=
|
||||
local subject=
|
||||
local work=
|
||||
|
||||
info "Quicklink... ${1:-}"
|
||||
|
||||
# Check if current candidate is a symlink
|
||||
if ! subject=$(readlink -- "${1:-}"); then
|
||||
printf -- "%s\n" "${1:-}"
|
||||
return 1
|
||||
fi
|
||||
info " rl::quicklink symlink ${1} -> ${subject}"
|
||||
|
||||
# relative symlink target; prepend its parent direcotry
|
||||
subject=$(readlink -- "${1:-}") || { printf -- "%s\n" "${1:-}"; return 1; }
|
||||
if [[ "${subject}" != "/"* ]]; then
|
||||
work="$(rl::canon "$(dirname -- "${1:-}")")"
|
||||
subject="${work}/${subject}"
|
||||
info " rl::quicklink relative link resolved: ${subject}"
|
||||
fi
|
||||
|
||||
printf "%s\n" "${subject}"
|
||||
return 0
|
||||
}
|
||||
|
||||
rl::canon() {
|
||||
@@ -110,100 +70,31 @@ rl::canon() {
|
||||
local retval=0
|
||||
|
||||
start="${1:-}"
|
||||
info "Canonicalize path... ${start}"
|
||||
|
||||
while (( run )) ; do
|
||||
if work="$(cd "${start}" >/dev/null 2>&1 && pwd -P)" ; then
|
||||
# Special: `pwd -P` returns with // as root for links starting at /
|
||||
# e.g. $(readlink /proc/self/root) == "/"
|
||||
# $(cd /proc/self/root/mnt && pwd -P) == "//mnt"
|
||||
subject="$(rl::normalize "${work}")"
|
||||
info " rl::canon valid directory: ${subject}"
|
||||
run=0
|
||||
subject="$(rl::normalize "${work}")"; run=0
|
||||
elif work="$(cd "$(dirname -- "${start}")" >/dev/null 2>&1 && pwd -P)" ; then
|
||||
bname="$(basename -- "${start}")"
|
||||
|
||||
# Special: / produces //
|
||||
[[ "${work}" == "/" ]] && work=
|
||||
|
||||
subject="${work}${bname:+"/${bname}"}"
|
||||
info " rl::canon valid parent: ${subject}"
|
||||
|
||||
# Special: Succeed with valid element after second run; see special below
|
||||
# e.g. /root/.
|
||||
# * /root is valid but not accessible
|
||||
if (( retval )) && [ -e "${subject}" ] ;then
|
||||
info " rl::canon valid element"
|
||||
retval=0
|
||||
fi
|
||||
run=0
|
||||
(( retval )) && [ -e "${subject}" ] && retval=0; run=0
|
||||
else
|
||||
# Special: Some paths may be resolvable after normalization
|
||||
# e.g. somedir/..
|
||||
# * base "somedir" does not exist
|
||||
# * but irrelevant because of /..
|
||||
# * resolves to pwd but fails by readlink -f
|
||||
work="$(rl::normalize "${start}")"
|
||||
if [[ "${work}" != "${start}" ]] ; then
|
||||
info " rl::canon retry with: ${work}"
|
||||
start="${work}"
|
||||
retval=1
|
||||
continue
|
||||
fi
|
||||
info " rl::canon invalid path: ${work}"
|
||||
subject="${work}"
|
||||
run=0 && retval=1
|
||||
[[ "${work}" != "${start}" ]] && start="${work}" && retval=1 && continue
|
||||
subject="${work}"; run=0 && retval=1
|
||||
fi
|
||||
done
|
||||
|
||||
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}"
|
||||
printf -- "%s\n" "${subject}"; return ${retval}
|
||||
}
|
||||
|
||||
rl::normalize() {
|
||||
local work=
|
||||
|
||||
info "Normalizing path... ${1:-}"
|
||||
work="${1:-}"
|
||||
|
||||
# Remove dir/.. sequences.
|
||||
local rex_tmp='[^/][^/]*/\.\./*'
|
||||
while [[ "${work}" =~ $rex_tmp ]] ; do
|
||||
work="${work/"${BASH_REMATCH[0]}"/}"
|
||||
done
|
||||
|
||||
# Remove /./ and /.$ sequences.
|
||||
rex_tmp='/\.(/|$)'
|
||||
while [[ "$work" =~ $rex_tmp ]]; do
|
||||
work="${work/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]/$//}"}"
|
||||
done
|
||||
|
||||
# Remove leading ./
|
||||
while [[ "${work}" =~ [^/][^/]*/\.\./* ]] ; do work="${work/"${BASH_REMATCH[0]}"/}"; done
|
||||
while [[ "$work" =~ /\.(/|$) ]]; do work="${work/${BASH_REMATCH[0]}/${BASH_REMATCH[1]/$//}}"; done
|
||||
work="${work#./*}"
|
||||
|
||||
# Remove trailing /
|
||||
rex_tmp='(.*[^/])/$'
|
||||
if [[ "${work}" =~ $rex_tmp ]] ; then
|
||||
work="${BASH_REMATCH[1]}"
|
||||
fi
|
||||
|
||||
info " rl::normalize result: ${work}"
|
||||
[[ "${work}" =~ (.*[^/])/$ ]] && work="${BASH_REMATCH[1]}"
|
||||
printf -- "%s\n" "${work}"
|
||||
}
|
||||
|
||||
@@ -213,31 +104,18 @@ rl::main() {
|
||||
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--)
|
||||
shift
|
||||
break ;;
|
||||
-d|--debug)
|
||||
shift
|
||||
LOG_LEVEL=7 ;;
|
||||
-dd)
|
||||
shift
|
||||
set -o xtrace ;;
|
||||
-*)
|
||||
shift ;;
|
||||
--) shift; break ;;
|
||||
-d|--debug) shift; LOG_LEVEL=7 ;;
|
||||
-dd) shift; set -o xtrace ;;
|
||||
-*) shift ;;
|
||||
esac
|
||||
done
|
||||
|
||||
info "BASH rdlink"
|
||||
|
||||
for file in "$@"; do
|
||||
rl::rdlink "$file"
|
||||
done
|
||||
for file in "$@"; do rl::rdlink "$file"; done
|
||||
}
|
||||
|
||||
# Provide as function to be called when sourced
|
||||
rdlink() {
|
||||
rl::main "$@"
|
||||
}
|
||||
rdlink() { rl::main "$@"; }
|
||||
|
||||
### Check if script is _sourced
|
||||
### https://stackoverflow.com/a/28776166
|
||||
@@ -255,9 +133,7 @@ else # All other shells: examine $0 for known shell binary filenames
|
||||
fi
|
||||
###
|
||||
|
||||
if (( ! _sourced )); then
|
||||
rl::main "$@"
|
||||
fi
|
||||
(( ! _sourced )) && rl::main "$@"
|
||||
|
||||
### Script EOF
|
||||
|
||||
|
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
|
||||
|
||||
readonly getenv_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||
get_os_version() {
|
||||
local os_version=
|
||||
local utils_version=
|
||||
|
||||
os_version="$(bash "${getenv_dir}/distro.sh" | sed '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 -E "BusyBox")" ; then
|
||||
utils_version="$(sed 's/\(BusyBox v[0-9\.]\+\).*/\1/g' <<<${utils_version})"
|
||||
elif utils_version="$(readlink --version 2>&1 | grep -E "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}_${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,89 +42,6 @@ tocompete=()
|
||||
|
||||
. "${test_dir}/totest.sh"
|
||||
|
||||
rl::printPath() {
|
||||
local testnum="${1:-0}"
|
||||
local input="${2:-"-"}"
|
||||
local truncate_graphic=
|
||||
local inputwidth=${config_path_width}
|
||||
|
||||
if (( flag_extendedOutput )); then
|
||||
printf -- "%-3d Inp: %-${config_path_width}s ---" ${testnum} "${input}"
|
||||
else
|
||||
# Truncate input string on the left if longer than $config_path_width
|
||||
if (( ${#input} > ${config_path_width} )) ; then
|
||||
# +1 : prepending truncate_graphic
|
||||
input="${input:$(( ${#input} - ${config_path_width} + 1 ))}"
|
||||
# -1 : prepending truncate_graphic
|
||||
inputwidth=$((config_path_width - 1))
|
||||
truncate_graphic="✀"
|
||||
fi
|
||||
# Print input and expected
|
||||
printf "%b%${inputwidth}s" "${truncate_graphic}" "${input}"
|
||||
fi
|
||||
}
|
||||
|
||||
rl::printTestSummary() {
|
||||
local success=${1:-0}
|
||||
local failed=${2:-0}
|
||||
local skipped=${3:-0}
|
||||
local total=$(( success + failed + skipped ))
|
||||
readonly columnwidth=7
|
||||
readonly tableformat="%${columnwidth}s | %${columnwidth}s | %${columnwidth}s | %${columnwidth}s\n"
|
||||
|
||||
printf "\n# Result : ${tableformat}" "success" "failed" "skipped" "total"
|
||||
printf " ${tableformat}" "${success}" "${failed}" "${skipped}" "${total}"
|
||||
}
|
||||
|
||||
# rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT>
|
||||
# Compare results and print summary
|
||||
rl::testcmp() {
|
||||
local testnum="${1:-0}"
|
||||
local input="${2:-"-"}"
|
||||
local expect="${3:-"-"}"
|
||||
local actual="${4:-"-"}"
|
||||
local testresult=0 #failed
|
||||
local testresult_graphic="${color_red}✗${color_less}" # alt. symbol ≠
|
||||
|
||||
if [[ "${expect}" == "${actual}" ]] ; 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
|
||||
if (( flag_extendedOutput )); then
|
||||
rl::printPath ${testnum} "${input}"
|
||||
printf " %b\n" "${testresult_graphic}"
|
||||
if (( ! testresult )); then
|
||||
# Test failed
|
||||
printf " Result: %s\n Exp: %s\n" "${actual}" "${expect}"
|
||||
|
||||
if (( flag_verbose )); then
|
||||
[ -e "${input}" ] && printf "\n Subject:\n" && ls -al "${input}"
|
||||
|
||||
# Print debug output of tool_a
|
||||
printf "\n Debug:\n"
|
||||
( ${tool_rdlink} -d -- "${input}" )
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Change result graphic if test failed
|
||||
(( ! testresult )) && testresult_graphic="→" # "${color_green}→${color_less}"
|
||||
rl::printPath "" "${input}"
|
||||
printf " %b %s\n" "${testresult_graphic}" "${expect}"
|
||||
# Print actual result if test failed
|
||||
if (( ! testresult )); then
|
||||
printf "%$((config_path_width))s %b %s\n" " " "${color_red}✗${color_less}" "${actual}"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
rl::test() {
|
||||
local i=0
|
||||
local path=
|
||||
@@ -164,14 +83,13 @@ rl::test() {
|
||||
esac
|
||||
done
|
||||
|
||||
[[ ! -e "${tool_rdlink}" ]] && "${build_cmd}"
|
||||
|
||||
# Cmd line arguments
|
||||
# Check if assertion tests need to be performed
|
||||
(( ! flag_onlycompete )) && [[ ! "$@" ]] && toassert_init
|
||||
|
||||
# Compare against expected result
|
||||
if (( ${#toassert[@]} )) ; then
|
||||
printf "\n# Assertion tests (\"Expected path\" == rdlink)\n"
|
||||
rl::printTitle
|
||||
printf "\n## Assertion tests (\"Expected path\" == rdlink)\n"
|
||||
fi
|
||||
for testarray in "${toassert[@]}"; do
|
||||
i=0
|
||||
@@ -179,7 +97,7 @@ rl::test() {
|
||||
for path in "${!arraywalker}"; do
|
||||
if (( ! i )); then
|
||||
# Print title in array element 0
|
||||
printf " ### %b ###\n" "${color_yellow}${path}${color_less}"
|
||||
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
|
||||
@@ -220,7 +138,8 @@ rl::test() {
|
||||
|
||||
# Compare output of rdlink and readlink -f
|
||||
if (( ${#tocompete[@]} )) ; then
|
||||
printf "\n# Competition tests (readlink -f == rdlink)\n"
|
||||
rl::printTitle
|
||||
printf "\n## Competition tests (readlink -f == rdlink)\n"
|
||||
fi
|
||||
for testarray in "${tocompete[@]}"; do
|
||||
i=0
|
||||
@@ -229,7 +148,7 @@ rl::test() {
|
||||
|
||||
if (( ! i )); then
|
||||
# Print title in array element 0
|
||||
printf " ### %b ###\n" "${color_yellow}${path}${color_less}"
|
||||
printf "### %b\n" "${color_yellow}${path}${color_less}"
|
||||
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
||||
|
||||
else
|
||||
@@ -254,8 +173,104 @@ rl::test() {
|
||||
done
|
||||
|
||||
rl::printTestSummary $tests_success $tests_failed $tests_skipped
|
||||
(( testend )) && return 1 || return 0
|
||||
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:-"-"}"
|
||||
local truncate_graphic=
|
||||
local inputwidth=${config_path_width}
|
||||
|
||||
if (( flag_extendedOutput )); then
|
||||
printf -- "%-5d Inp: %-${config_path_width}s ---" ${testnum} "${input}"
|
||||
else
|
||||
# Truncate input string on the left if longer than $config_path_width
|
||||
if (( ${#input} > ${config_path_width} )) ; then
|
||||
# +1 : prepending truncate_graphic
|
||||
input="${input:$(( ${#input} - ${config_path_width} + 1 ))}"
|
||||
# -1 : prepending truncate_graphic
|
||||
inputwidth=$((config_path_width - 1))
|
||||
truncate_graphic="✀"
|
||||
fi
|
||||
# Print input and expected
|
||||
printf "%b%${inputwidth}s" "${truncate_graphic}" "${input}"
|
||||
fi
|
||||
}
|
||||
|
||||
rl::printTestSummary() {
|
||||
local success=${1:-0}
|
||||
local failed=${2:-0}
|
||||
local skipped=${3:-0}
|
||||
local total=$(( success + failed + skipped ))
|
||||
readonly columnwidth=7
|
||||
readonly tableformat="%${columnwidth}s | %${columnwidth}s | %${columnwidth}s | %${columnwidth}s\n"
|
||||
|
||||
printf "\n# Result : ${tableformat}" "success" "failed" "skipped" "total"
|
||||
printf " ${tableformat}" "${success}" "${failed}" "${skipped}" "${total}"
|
||||
}
|
||||
|
||||
# rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT>
|
||||
# Compare results and print summary
|
||||
rl::testcmp() {
|
||||
local testnum="${1:-0}"
|
||||
local input="${2:-"-"}"
|
||||
local expect="${3:-"-"}"
|
||||
local actual="${4:-"-"}"
|
||||
local testresult=0 #failed
|
||||
local testresult_graphic="${color_red}✗${color_less}" # alt. symbol ≠
|
||||
local link="$(readlink -- "${input}")"
|
||||
|
||||
if [[ "${expect}" == "${actual}" ]] ; then
|
||||
# Don't print success for this flag
|
||||
(( flag_printerror )) && return 0
|
||||
testresult=1
|
||||
testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓
|
||||
fi
|
||||
|
||||
# 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}"
|
||||
|
||||
if (( flag_verbose )); then
|
||||
[ -e "${input}" ] && printf "\n Subject:\n" && ls -al "${input}"
|
||||
|
||||
# Print debug output of tool_a
|
||||
printf "\n Debug:\n"
|
||||
( ${tool_rdlink} -d -- "${input}" )
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Change result graphic if test failed
|
||||
(( ! 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}"
|
||||
return 1
|
||||
fi
|
||||
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
|
||||
)
|
||||
}
|
||||
|
||||
@@ -152,9 +152,14 @@ toassert_init() {
|
||||
"../rel_b" "$(cd ".." && pwd)/rel_b"
|
||||
)
|
||||
|
||||
assert_force_fail=( "Force fail"
|
||||
"nix" ""
|
||||
)
|
||||
|
||||
# Add test arrays to global test array from test_rdlink
|
||||
toassert+=(
|
||||
assert_invalid_files
|
||||
#assert_force_fail
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user