Skip to content

Commit

Permalink
XSH library - Fix ns_relativepath edge case
Browse files Browse the repository at this point in the history
* Fix invalid path produced by ns_relativepath when the common parent of paths is "/"
* Add ns_relativepath unit test
* Update tools
  • Loading branch information
noresources committed Mar 13, 2024
1 parent 8a02022 commit 47d99c2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 8 deletions.
4 changes: 3 additions & 1 deletion ns/sh/new-xsh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
4 changes: 3 additions & 1 deletion ns/sh/xsltdeps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
4 changes: 3 additions & 1 deletion ns/xsh/lib/filesystem/filesystem.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ for ((i=0;${i}<${c};i++))
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
78 changes: 78 additions & 0 deletions tests/xsh/relativepath.xsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Function declarations -->
<sh:functions xmlns:sh="http://xsd.nore.fr/xsh" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="../../ns/xsh/lib/base/base.xsh" xpointer="xmlns(xsh=http://xsd.nore.fr/xsh)xpointer(//xsh:function)" />
<xi:include href="../../ns/xsh/lib/filesystem/filesystem.xsh"
xpointer="xmlns(xsh=http://xsd.nore.fr/xsh)xpointer(//xsh:function)" />

<sh:function name="ns_testsuite_ns_relativepath">
<sh:body>
<sh:local name="ns_testsuite_relativepath_root">$(ns_mktempdir "ns_testsuite_relativepath_root")</sh:local>
<sh:local name="cwd">$(pwd)</sh:local>
<sh:local name="t" />
<sh:local name="target"/>
<sh:local name="source" />
<sh:local name="actual" />
<sh:local name="expected" />
<![CDATA[
###################################################

if [ -z "${ns_testsuite_relativepath_root}" ] \
|| [ ! -d "${ns_testsuite_relativepath_root}" ]
then
echo "Unable to create ns_testsuite_relativepath_root" 1>&2
return 1
fi

for t in \
'foo:bar:../foo' \
'foo:foo:.' \
'foo:foo/bar:..' \
'foo/bar:huey/dewey/louie:../../../foo/bar' \
'/bin/echo:/etc/hosts:../bin/echo'
do
target="$(cut -f 1 -d':' <<< "${t}")"
source="$(cut -f 2 -d':' <<< "${t}")"
expected="$(cut -f 3 -d':' <<< "${t}")"

if [ "${target:0:1}" != '/' ]
then
mkdir -p "${ns_testsuite_relativepath_root}/${target}" \
|| return 1
target="$(ns_realpath "${ns_testsuite_relativepath_root}/${target}")"
[ -d "${target}" ] || return 1
elif [ ! -r "${target}" ]
then
echo 'warning: ' "${target} does not exists" 1>&2
continue
fi

if [ "${source:0:1}" != '/' ]
then
mkdir -p "${ns_testsuite_relativepath_root}/${source}" \
|| return 1
source="$(ns_realpath "${ns_testsuite_relativepath_root}/${source}")"
[ -d "${source}" ] || return 1
elif [ ! -r ${source} ]
then
echo 'warning: ' "${source} does not exists" 1>&2
continue
fi

actual="$(ns_relativepath "${target}" "${source}")"
if [ "${actual}" != "${expected}" ]
then
printf "%-10.10s: %s\n" Source "${source#${ns_testsuite_relativepath_root}/}"
printf "%-10.10s: %s\n" Target "${target#${ns_testsuite_relativepath_root}/}"
printf "%-10.10s: %s\n" "Expected" "${expected}"
printf "%-10.10s: %s\n" Actual "${actual}"
return 1
fi

done

###################################################
return 0
]]></sh:body>
</sh:function>
</sh:functions>
4 changes: 3 additions & 1 deletion tools/sh/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
4 changes: 3 additions & 1 deletion tools/sh/update-c-parser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
4 changes: 3 additions & 1 deletion tools/sh/update-doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
4 changes: 3 additions & 1 deletion tools/sh/update-php-parser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down
4 changes: 3 additions & 1 deletion tools/sh/update-python-parser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,9 @@ ns_relativepath()
do
res="${res}/.."
done
res="${res}${from#${sub}}"
[ "${sub}" != '/' ] \
&& res="${res}${from#${sub}}" \
|| res="${res}${from}"
res="${res#./}"
[ -z "${res}" ] && res='.'
echo "${res}"
Expand Down

0 comments on commit 47d99c2

Please sign in to comment.