Skip to content

Commit 7acaa70

Browse files
onnozweershailihu
andauthored
102 do not create channel if folder does not exist (#108)
* To check if parent dir exists, use /namespace call instead of /tape/locality Fixes #105 * Fix unit test * New function is_dir() interfered with the output of create_path() * Typo was fixed in ada but should be fixed in unit test as well, to match * Improve validation for --events and --report-staged * Ada now checks whether a valid, existing directory is provided * The order of validation has changed to provide more accurate error messages. When the channel name is missing, Ada will not say the path is missing, but say that the channel name and directory are missing (because if there's no channel name, the path must be missing as well) * Do path validation after construct_auth() Because the path validation calls the API and this will fail when the authentication has not been set up. * Exit when is_dir is used before construct_auth This should prevent us developers from making this mistake again. * Fix unit test to match changed output in previous commit * Fix comment * Adapt unit test for create_path --------- Co-authored-by: Haili Hu <hailihu@gmail.com>
1 parent 555461b commit 7acaa70

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

ada/ada

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,12 @@ is_dir () {
11301130
# Check whether a path is a directory. If the path doesn't exist, just continue.
11311131
# Similar to pathtype(), but that one quits with error if the path doesn't exist.
11321132
encoded_path=$(urlencode "$1")
1133+
# Warn the developers not to call is_dir before the auth has been set up.
1134+
if [ -z "$curl_authorization" ] ; then
1135+
echo 1>&2 "ERROR: calling function is_dir() before authentication has been set up." \
1136+
"Please submit an issue for this at https://github.com/sara-nl/SpiderScripts/ ."
1137+
exit 1
1138+
fi
11331139
result=$(
11341140
$debug && set -x
11351141
curl "${curl_authorization[@]}" \
@@ -2160,8 +2166,7 @@ validate_input() {
21602166
case $command in
21612167
list | stat | mkdir | mv | delete | \
21622168
setlabel | lslabel | findlabel | rmlabel | \
2163-
setxattr | lsxattr | findxattr | rmxattr | \
2164-
events | report-staged )
2169+
setxattr | lsxattr | findxattr | rmxattr )
21652170
if [[ -z $path || $path =~ ^-- ]] ; then
21662171
echo 1>&2 "ERROR: command $command requires a path."
21672172
exit 1
@@ -2215,14 +2220,18 @@ validate_input() {
22152220
exit 1
22162221
fi
22172222
;;
2218-
events | report-staged )
2219-
if [[ -z $channel_name || $channel_name =~ ^-- ]] ; then
2220-
echo 1>&2 "ERROR: command $command requires a channel name."
2221-
exit 1
2222-
fi
2223-
;;
22242223
esac
22252224
;;
2225+
events | report-staged )
2226+
if [[ -z $channel_name || $channel_name =~ ^-- ]] ; then
2227+
echo 1>&2 "ERROR: command $command requires a channel name and a directory."
2228+
exit 1
2229+
fi
2230+
if [[ -z $path || $path =~ ^-- ]] ; then
2231+
echo 1>&2 "ERROR: command $command requires a directory."
2232+
exit 1
2233+
fi
2234+
;;
22262235
longlist | checksum | stage | unstage )
22272236
if [[ -z $pathlist || $pathlist =~ ^-- ]] ; then
22282237
echo 1>&2 "ERROR: command $command requires a path or a path list."
@@ -2743,6 +2752,13 @@ api_call () {
27432752
esac
27442753
exit 1
27452754
fi
2755+
# Some additional input validation, after construct_auth().
2756+
# We need an existing directory.
2757+
if ! is_dir "$path" ; then
2758+
echo 1>&2 "ERROR: '$path' is not a directory."
2759+
exit 1
2760+
fi
2761+
# Setting up channel
27462762
channel_url=$(get_channel_by_name "$channel_name")
27472763
channel_id=$(basename "$channel_url")
27482764
if [ "$channel_url" = "" ] ; then

tests/unit_test.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ test_pathtype() {
2525

2626

2727
test_create_path() {
28-
# Check error handling when incorrect path is given
28+
29+
# Initialize counter
2930
counter=0
31+
32+
# Check error handling when authentication is not set up yet
33+
( create_path "/test/a" true >${stdoutF} 2>${stderrF} )
34+
result=$?
35+
assertFalse "expecting return code of 1 (false)" ${result}
36+
grep "ERROR: calling function is_dir() before authentication has been set up." "${stderrF}" >/dev/null
37+
assertTrue "STDERR message incorrect" $?
38+
39+
curl_authorization="test"
40+
# Check error handling when incorrect path is given
3041
( create_path "/test/a" true >${stdoutF} 2>${stderrF} )
3142
result=$?
3243
assertFalse "expecting return code of 1 (false)" ${result}

0 commit comments

Comments
 (0)