Skip to content

Commit

Permalink
is-present, is-symlink: new fs test conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Jan 17, 2025
1 parent 93adb5a commit 73b0643
Show file tree
Hide file tree
Showing 3 changed files with 352 additions and 96 deletions.
221 changes: 174 additions & 47 deletions commands/is-present
Original file line number Diff line number Diff line change
@@ -1,63 +1,190 @@
#!/usr/bin/env bash

function is_present_test() (
source "$DOROTHY/sources/bash.bash"
source "$DOROTHY/sources/tests.bash"
echo-style --h1="TEST: $0"

# test standard paths
eval-tester --name='no args' --status=22 --ignore-stderr \
-- is-present --

eval-tester --name='empty args' --status=22 \
-- is-present -- '' ''

eval-tester --name='missing' --status=2 \
-- is-present -- "$DOROTHY/this-does-not-exist"

eval-tester --name='directory' \
-- is-present -- "$DOROTHY"
local root command='is-present'
root="$(fs_tests__prep "$command")"

eval-tester --name='file' \
-- is-present -- "$DOROTHY/README.md"

eval-tester --name='file then dir' \
-- is-present -- "$DOROTHY/README.md" "$DOROTHY"

eval-tester --name='dir then file' \
-- is-present -- "$DOROTHY" "$DOROTHY/README.md"

eval-tester --name='dir then file then invalid' --status=22 \
-- is-present -- "$DOROTHY" "$DOROTHY/README.md" ''
eval-tester --name='no args' --status=22 --ignore-stderr \
-- "$command" --

eval-tester --name='dir then invalid then file' --status=22 \
-- is-present -- "$DOROTHY" '' "$DOROTHY/README.md"
# test no escalation
local tuples=(
22 ''

# prep
local root dir_target dir_symlink file_target file_symlink
root="$(fs-temp --directory='is-present-test')"
fs-rm --quiet --no-confirm -- "$root"
dir_target="$(fs-temp --root="$root" --directory='dir_target' --touch)"
file_target="$(fs-temp --root="$root" --file='file_target' --touch)"
dir_symlink="$(fs-temp --root="$root" --directory='dir_symlink' --no-touch)"
file_symlink="$(fs-temp --root="$root" --file='file_symlink' --no-touch)"
symlink-helper --existing="$dir_target" --symlink="$dir_symlink" --quiet
symlink-helper --existing="$file_target" --symlink="$file_symlink" --quiet
2 "$root/missing-dir/missing-file"
2 "$root/missing-file"

# test working symlinks
eval-tester --name='symlink dir' \
-- is-present -- "$dir_symlink"
0 "$root/targets/empty-dir"
0 "$root/targets/empty-file"
0 "$root/targets/filled-dir/empty-subfile"
0 "$root/targets/filled-dir/filled-subdir"
0 "$root/targets/filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/filled-dir/filled-subfile"
0 "$root/targets/filled-file"
0 "$root/targets/unaccessible-empty-dir"
0 "$root/targets/unaccessible-empty-file"
0 "$root/targets/unaccessible-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unaccessible-filled-dir/empty-subfile"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unaccessible-filled-dir/filled-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unaccessible-filled-dir/filled-subdir/empty-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unaccessible-filled-dir/filled-subfile"
0 "$root/targets/unaccessible-filled-file"
0 "$root/targets/unexecutable-empty-dir"
0 "$root/targets/unexecutable-empty-file"
0 "$root/targets/unexecutable-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unexecutable-filled-dir/empty-subfile"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unexecutable-filled-dir/filled-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unexecutable-filled-dir/filled-subdir/empty-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unexecutable-filled-dir/filled-subfile"
0 "$root/targets/unexecutable-filled-file"
0 "$root/targets/unreadable-empty-dir"
0 "$root/targets/unreadable-empty-file"
0 "$root/targets/unreadable-filled-dir"
0 "$root/targets/unreadable-filled-dir/empty-subfile"
0 "$root/targets/unreadable-filled-dir/filled-subdir"
0 "$root/targets/unreadable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unreadable-filled-dir/filled-subfile"
0 "$root/targets/unreadable-filled-file"
0 "$root/targets/unwritable-empty-dir"
0 "$root/targets/unwritable-empty-file"
0 "$root/targets/unwritable-filled-dir"
0 "$root/targets/unwritable-filled-dir/empty-subfile"
0 "$root/targets/unwritable-filled-dir/filled-subdir"
0 "$root/targets/unwritable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unwritable-filled-dir/filled-subfile"
0 "$root/targets/unwritable-filled-file"

eval-tester --name='symlink file' \
-- is-present -- "$file_symlink"
0 "$root/symlinks/empty-dir"
0 "$root/symlinks/empty-file"
0 "$root/symlinks/filled-dir--empty-subfile"
0 "$root/symlinks/filled-dir--filled-subdir"
0 "$root/symlinks/filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/filled-dir--filled-subfile"
0 "$root/symlinks/filled-file"
0 "$root/symlinks/unaccessible-empty-dir"
0 "$root/symlinks/unaccessible-empty-file"
0 "$root/symlinks/unaccessible-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unaccessible-filled-dir--empty-subfile"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unaccessible-filled-dir--filled-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unaccessible-filled-dir--filled-subdir--empty-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unaccessible-filled-dir--filled-subfile"
0 "$root/symlinks/unaccessible-filled-file"
0 "$root/symlinks/unexecutable-empty-dir"
0 "$root/symlinks/unexecutable-empty-file"
0 "$root/symlinks/unexecutable-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unexecutable-filled-dir--empty-subfile"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unexecutable-filled-dir--filled-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unexecutable-filled-dir--filled-subdir--empty-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unexecutable-filled-dir--filled-subfile"
0 "$root/symlinks/unexecutable-filled-file"
0 "$root/symlinks/unreadable-empty-dir"
0 "$root/symlinks/unreadable-empty-file"
0 "$root/symlinks/unreadable-filled-dir"
0 "$root/symlinks/unreadable-filled-dir--empty-subfile"
0 "$root/symlinks/unreadable-filled-dir--filled-subdir"
0 "$root/symlinks/unreadable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unreadable-filled-dir--filled-subfile"
0 "$root/symlinks/unreadable-filled-file"
0 "$root/symlinks/unwritable-empty-dir"
0 "$root/symlinks/unwritable-empty-file"
0 "$root/symlinks/unwritable-filled-dir"
0 "$root/symlinks/unwritable-filled-dir--empty-subfile"
0 "$root/symlinks/unwritable-filled-dir--filled-subdir"
0 "$root/symlinks/unwritable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unwritable-filled-dir--filled-subfile"
0 "$root/symlinks/unwritable-filled-file"
)
fs_tests__tuples --group='test no escalation' "$command" --no-sudo -- "${tuples[@]}"

# test broken symlinks
fs-rm --quiet --no-confirm -- "$dir_target" "$file_target"
# test default escalation
tuples=(
0 "$root/targets/unaccessible-filled-dir/empty-subfile"
0 "$root/targets/unaccessible-filled-dir/filled-subdir"
0 "$root/targets/unaccessible-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unaccessible-filled-dir/filled-subfile"
0 "$root/targets/unexecutable-filled-dir/empty-subfile"
0 "$root/targets/unexecutable-filled-dir/filled-subdir"
0 "$root/targets/unexecutable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unexecutable-filled-dir/filled-subfile"
0 "$root/symlinks/unaccessible-filled-dir--empty-subfile"
0 "$root/symlinks/unaccessible-filled-dir--filled-subdir"
0 "$root/symlinks/unaccessible-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unaccessible-filled-dir--filled-subfile"
0 "$root/symlinks/unexecutable-filled-dir--empty-subfile"
0 "$root/symlinks/unexecutable-filled-dir--filled-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subfile"
)
fs_tests__tuples --group='test default escalation' "$command" -- "${tuples[@]}"

eval-tester --name='broken symlink dir is present' \
-- is-present -- "$dir_symlink"
# test with escalation
tuples=(
0 "$root/targets/unaccessible-filled-dir/empty-subfile"
0 "$root/targets/unaccessible-filled-dir/filled-subdir"
0 "$root/targets/unaccessible-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unaccessible-filled-dir/filled-subfile"
0 "$root/targets/unexecutable-filled-dir/empty-subfile"
0 "$root/targets/unexecutable-filled-dir/filled-subdir"
0 "$root/targets/unexecutable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unexecutable-filled-dir/filled-subfile"
0 "$root/symlinks/unaccessible-filled-dir--empty-subfile"
0 "$root/symlinks/unaccessible-filled-dir--filled-subdir"
0 "$root/symlinks/unaccessible-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unaccessible-filled-dir--filled-subfile"
0 "$root/symlinks/unexecutable-filled-dir--empty-subfile"
0 "$root/symlinks/unexecutable-filled-dir--filled-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subfile"
)
fs_tests__tuples --group='test with escalation' "$command" --sudo -- "${tuples[@]}"

eval-tester --name='broken symlink file is present' \
-- is-present -- "$file_symlink"
# break the symlinks
sudo-helper -- rm -rf "$root/targets"
tuples=(
0 "$root/symlinks/empty-dir"
0 "$root/symlinks/empty-file"
0 "$root/symlinks/filled-dir--empty-subfile"
0 "$root/symlinks/filled-dir--filled-subdir"
0 "$root/symlinks/filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/filled-dir--filled-subfile"
0 "$root/symlinks/filled-file"
0 "$root/symlinks/unaccessible-empty-dir"
0 "$root/symlinks/unaccessible-empty-file"
0 "$root/symlinks/unaccessible-filled-dir"
0 "$root/symlinks/unaccessible-filled-dir--empty-subfile"
0 "$root/symlinks/unaccessible-filled-dir--filled-subdir"
0 "$root/symlinks/unaccessible-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unaccessible-filled-dir--filled-subfile"
0 "$root/symlinks/unaccessible-filled-file"
0 "$root/symlinks/unexecutable-empty-dir"
0 "$root/symlinks/unexecutable-empty-file"
0 "$root/symlinks/unexecutable-filled-dir"
0 "$root/symlinks/unexecutable-filled-dir--empty-subfile"
0 "$root/symlinks/unexecutable-filled-dir--filled-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subfile"
0 "$root/symlinks/unexecutable-filled-file"
0 "$root/symlinks/unreadable-empty-dir"
0 "$root/symlinks/unreadable-empty-file"
0 "$root/symlinks/unreadable-filled-dir"
0 "$root/symlinks/unreadable-filled-dir--empty-subfile"
0 "$root/symlinks/unreadable-filled-dir--filled-subdir"
0 "$root/symlinks/unreadable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unreadable-filled-dir--filled-subfile"
0 "$root/symlinks/unreadable-filled-file"
0 "$root/symlinks/unwritable-empty-dir"
0 "$root/symlinks/unwritable-empty-file"
0 "$root/symlinks/unwritable-filled-dir"
0 "$root/symlinks/unwritable-filled-dir--empty-subfile"
0 "$root/symlinks/unwritable-filled-dir--filled-subdir"
0 "$root/symlinks/unwritable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unwritable-filled-dir--filled-subfile"
0 "$root/symlinks/unwritable-filled-file"
)
fs_tests__tuples --group='test broken symlinks' "$command" -- "${tuples[@]}"

echo-style --g1="TEST: $0"
return 0
Expand Down
Loading

0 comments on commit 73b0643

Please sign in to comment.