Skip to content

Commit

Permalink
is-file, is-missing, is-nonempty-file: add 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 d259793 commit a20ad1f
Show file tree
Hide file tree
Showing 3 changed files with 573 additions and 168 deletions.
242 changes: 198 additions & 44 deletions commands/is-file
Original file line number Diff line number Diff line change
@@ -1,63 +1,217 @@
#!/usr/bin/env bash

function is_file_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-file --

eval-tester --name='empty args' --status=22 \
-- is-file -- '' ''
local root command='is-file'
root="$(fs_tests__prep "$command")"

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

eval-tester --name='directory' --status=79 \
-- is-file -- "$DOROTHY" "$DOROTHY"

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

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

eval-tester --name='file then invalid then dir' --status=22 \
-- is-file -- "$DOROTHY/README.md" '' "$DOROTHY"
2 "$root/missing-dir/missing-file"
2 "$root/missing-file"

eval-tester --name='file then missing then invalid then dir' --status=2 \
-- is-file -- "$DOROTHY/README.md" "$DOROTHY/this-does-not-exist" '' "$DOROTHY"
79 "$root/targets/empty-dir"
0 "$root/targets/empty-file"
0 "$root/targets/filled-dir/empty-subfile"
79 "$root/targets/filled-dir/filled-subdir"
79 "$root/targets/filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/filled-dir/filled-subfile"
0 "$root/targets/filled-file"
79 "$root/targets/unaccessible-empty-dir"
# able to determine if this is a file or directory because that information is stored in the parent that is accessible
0 "$root/targets/unaccessible-empty-file"
79 "$root/targets/unaccessible-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unaccessible-filled-dir/empty-subfile"
"$(__status__root_or_nonroot 79 13)" "$root/targets/unaccessible-filled-dir/filled-subdir"
"$(__status__root_or_nonroot 79 13)" "$root/targets/unaccessible-filled-dir/filled-subdir/empty-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unaccessible-filled-dir/filled-subfile"
# able to determine if this is a file or directory because that information is stored in the parent that is accessible
0 "$root/targets/unaccessible-filled-file"
79 "$root/targets/unexecutable-empty-dir"
0 "$root/targets/unexecutable-empty-file"
79 "$root/targets/unexecutable-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/targets/unexecutable-filled-dir/empty-subfile"
"$(__status__root_or_nonroot 79 13)" "$root/targets/unexecutable-filled-dir/filled-subdir"
"$(__status__root_or_nonroot 79 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"
79 "$root/targets/unreadable-empty-dir"
# able to determine if this is a file or directory because that is unrelated to its read/write/executable permissions
0 "$root/targets/unreadable-empty-file"
79 "$root/targets/unreadable-filled-dir"
0 "$root/targets/unreadable-filled-dir/empty-subfile"
79 "$root/targets/unreadable-filled-dir/filled-subdir"
79 "$root/targets/unreadable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unreadable-filled-dir/filled-subfile"
# able to determine if this is a file or directory because that is unrelated to its read/write/executable permissions
0 "$root/targets/unreadable-filled-file"
79 "$root/targets/unwritable-empty-dir"
0 "$root/targets/unwritable-empty-file"
79 "$root/targets/unwritable-filled-dir"
0 "$root/targets/unwritable-filled-dir/empty-subfile"
79 "$root/targets/unwritable-filled-dir/filled-subdir"
79 "$root/targets/unwritable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unwritable-filled-dir/filled-subfile"
0 "$root/targets/unwritable-filled-file"

# prep
local root dir_target dir_symlink file_target file_symlink
root="$(fs-temp --directory='is-file-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
79 "$root/symlinks/empty-dir"
0 "$root/symlinks/empty-file"
0 "$root/symlinks/filled-dir--empty-subfile"
79 "$root/symlinks/filled-dir--filled-subdir"
79 "$root/symlinks/filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/filled-dir--filled-subfile"
0 "$root/symlinks/filled-file"
79 "$root/symlinks/unaccessible-empty-dir"
# able to determine if this is a file or directory because that information is stored in the parent that is accessible
0 "$root/symlinks/unaccessible-empty-file"
79 "$root/symlinks/unaccessible-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unaccessible-filled-dir--empty-subfile"
"$(__status__root_or_nonroot 79 13)" "$root/symlinks/unaccessible-filled-dir--filled-subdir"
"$(__status__root_or_nonroot 79 13)" "$root/symlinks/unaccessible-filled-dir--filled-subdir--empty-subdir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unaccessible-filled-dir--filled-subfile"
# able to determine if this is a file or directory because that information is stored in the parent that is accessible
0 "$root/symlinks/unaccessible-filled-file"
79 "$root/symlinks/unexecutable-empty-dir"
0 "$root/symlinks/unexecutable-empty-file"
79 "$root/symlinks/unexecutable-filled-dir"
"$(__status__root_or_nonroot 0 13)" "$root/symlinks/unexecutable-filled-dir--empty-subfile"
"$(__status__root_or_nonroot 79 13)" "$root/symlinks/unexecutable-filled-dir--filled-subdir"
"$(__status__root_or_nonroot 79 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"
79 "$root/symlinks/unreadable-empty-dir"
# able to determine if this is a file or directory because that is unrelated to its read/write/executable permissions
0 "$root/symlinks/unreadable-empty-file"
79 "$root/symlinks/unreadable-filled-dir"
0 "$root/symlinks/unreadable-filled-dir--empty-subfile"
79 "$root/symlinks/unreadable-filled-dir--filled-subdir"
79 "$root/symlinks/unreadable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unreadable-filled-dir--filled-subfile"
# able to determine if this is a file or directory because that is unrelated to its read/write/executable permissions
0 "$root/symlinks/unreadable-filled-file"
79 "$root/symlinks/unwritable-empty-dir"
0 "$root/symlinks/unwritable-empty-file"
79 "$root/symlinks/unwritable-filled-dir"
0 "$root/symlinks/unwritable-filled-dir--empty-subfile"
79 "$root/symlinks/unwritable-filled-dir--filled-subdir"
79 "$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 working symlinks
eval-tester --name='symlink dir' --status=79 \
-- is-file -- "$dir_symlink"
# test default escalation
tuples=(
0 "$root/targets/unaccessible-empty-file"
0 "$root/targets/unaccessible-filled-dir/empty-subfile"
79 "$root/targets/unaccessible-filled-dir/filled-subdir"
79 "$root/targets/unaccessible-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unaccessible-filled-dir/filled-subfile"
0 "$root/targets/unaccessible-filled-file"
0 "$root/targets/unexecutable-filled-dir/empty-subfile"
79 "$root/targets/unexecutable-filled-dir/filled-subdir"
79 "$root/targets/unexecutable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unexecutable-filled-dir/filled-subfile"
0 "$root/targets/unreadable-empty-file"
0 "$root/targets/unreadable-filled-file"

eval-tester --name='symlink file' \
-- is-file -- "$file_symlink"
0 "$root/symlinks/unaccessible-empty-file"
0 "$root/symlinks/unaccessible-filled-dir--empty-subfile"
79 "$root/symlinks/unaccessible-filled-dir--filled-subdir"
79 "$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-filled-dir--empty-subfile"
79 "$root/symlinks/unexecutable-filled-dir--filled-subdir"
79 "$root/symlinks/unexecutable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subfile"
0 "$root/symlinks/unreadable-empty-file"
0 "$root/symlinks/unreadable-filled-file"
)
fs_tests__tuples --group='test default escalation' "$command" -- "${tuples[@]}"

# test broken symlinks
fs-rm --quiet --no-confirm -- "$dir_target" "$file_target"
# test with escalation
tuples=(
0 "$root/targets/unaccessible-empty-file"
0 "$root/targets/unaccessible-filled-dir/empty-subfile"
79 "$root/targets/unaccessible-filled-dir/filled-subdir"
79 "$root/targets/unaccessible-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unaccessible-filled-dir/filled-subfile"
0 "$root/targets/unaccessible-filled-file"
0 "$root/targets/unexecutable-filled-dir/empty-subfile"
79 "$root/targets/unexecutable-filled-dir/filled-subdir"
79 "$root/targets/unexecutable-filled-dir/filled-subdir/empty-subdir"
0 "$root/targets/unexecutable-filled-dir/filled-subfile"
0 "$root/targets/unreadable-empty-file"
0 "$root/targets/unreadable-filled-file"

eval-tester --name='broken symlink dir' --status=9 \
-- is-file -- "$dir_symlink"
0 "$root/symlinks/unaccessible-empty-file"
0 "$root/symlinks/unaccessible-filled-dir--empty-subfile"
79 "$root/symlinks/unaccessible-filled-dir--filled-subdir"
79 "$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-filled-dir--empty-subfile"
79 "$root/symlinks/unexecutable-filled-dir--filled-subdir"
79 "$root/symlinks/unexecutable-filled-dir--filled-subdir--empty-subdir"
0 "$root/symlinks/unexecutable-filled-dir--filled-subfile"
0 "$root/symlinks/unreadable-empty-file"
0 "$root/symlinks/unreadable-filled-file"
)
fs_tests__tuples --group='test with escalation' "$command" --sudo -- "${tuples[@]}"

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

echo-style --g1="TEST: $0"
return 0
)
function is_file() (
source "$DOROTHY/sources/bash.bash"
Expand Down
Loading

0 comments on commit a20ad1f

Please sign in to comment.