Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ foreach case : test_cases
test('test_' + case, exe)
endforeach

# Set HOME to the fake home so rmw falls back safely if RMW_FAKE_HOME is ever
# unset mid-script, preventing accidental writes to the real home directory.
test_env = [
'MESON_SOURCE_ROOT=' + meson.project_source_root(),
'RMW_FAKE_HOME=' + RMW_FAKE_HOME,
'HOME=' + RMW_FAKE_HOME,
'XDG_DATA_HOME=' + RMW_FAKE_HOME + '/.local/share',
'XDG_CONFIG_HOME=' + RMW_FAKE_HOME + '/.config',
'MESON_BUILD_ROOT=' + meson.project_build_root(),
]

foreach s : scripts
test(
s,
files(s),
env: [
'MESON_SOURCE_ROOT=' + meson.project_source_root(),
'RMW_FAKE_HOME=' + RMW_FAKE_HOME,
'MESON_BUILD_ROOT=' + meson.project_build_root(),
],
env: test_env,
depends: main_bin,
)
endforeach

99 changes: 61 additions & 38 deletions test/test_media_root.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# Path should be relative
#
#https://specifications.freedesktop.org/trash-spec/1.0/#id-1.6.10.1 The key
#Path contains the original location of the file/directory, as either an
#absolute pathname (starting with the slash character “/”) or a relative
#"Path" contains the original location of the file/directory, as either an
#absolute pathname (starting with the slash character "/") or a relative
#pathname (starting with any other character). A relative pathname is to be
#from the directory in which the trash directory resides (for example, from
#$XDG_DATA_HOME for the home trash directory); it MUST not include a “..”
#directory, and for files not under that directory, absolute pathnames must
#be used. The system SHOULD support absolute pathnames only in the home trash
#$XDG_DATA_HOME for the "home trash" directory); it MUST not include a ".."
#directory, and for files not "under" that directory, absolute pathnames must
#be used. The system SHOULD support absolute pathnames only in the "home trash"
#directory, not in the directories under $topdir.

set -ve
Expand All @@ -20,45 +20,68 @@ else
. "${MESON_SOURCE_ROOT}/test/COMMON"
fi

# This test will only work on Andy's workstation.
# The media root, /home/andy/src is on a different partition than /home/andy
# It's mounted with 'bind' and therefore has a different device id
test ! -d /mnt/918375c2 && exit 0;

# This test use Andy's regular config, outside of the test sandbox, so don't
# run it if the epoch test setup is being used. Otherwise all the waste dirs
# will be purged.
test -n "$RMW_TEST_EPOCHALYPSE" && exit 0;
echo "hello"
# This test requires /tmp to be a top-level mount point on its own device so
# that rmw will write a relative Path in the trashinfo. Check /proc/mounts
# (Linux); on macOS/BSD where it doesn't exist the grep fails and we skip.
if ! grep -q '^[^ ]* /tmp ' /proc/mounts 2>/dev/null; then
echo "/tmp is not a top-level mount point; skipping"
exit 0
fi

# /tmp is on a different device — use it as the simulated media root.
TRASH_DIR="/tmp/.Trash-$(id -u)"
TEST_DIR="/tmp/rmw-media-root-test"
test_file="media_root_test"
PREV_RMW_FAKE_HOME=${RMW_FAKE_HOME}
# needs to be unset so rmw will use $HOME instead
unset RMW_FAKE_HOME
mkdir -p "$PREV_RMW_FAKE_HOME"
test_file_path=${PREV_RMW_FAKE_HOME}/$test_file
if test -f "$test_file_path"; then
rm "$test_file_path"
fi
if test -f /home/andy/src/rmw-project/.Trash-1000/files/$test_file; then
rm /home/andy/src/rmw-project/.Trash-1000/files/$test_file
fi
if test -f /home/andy/src/rmw-project/.Trash-1000/info/$test_file.trashinfo; then
rm /home/andy/src/rmw-project/.Trash-1000/info/$test_file.trashinfo
fi
test_file_path="$TEST_DIR/$test_file"

# shellcheck disable=SC2329
cleanup() {
rm -rf "$TRASH_DIR" "$TEST_DIR"
}
trap cleanup EXIT

# Clean up any leftovers from a previous run
rm -rf "$TRASH_DIR" "$TEST_DIR"

# Create test config pointing the waste folder to /tmp
mkdir -p "$RMW_FAKE_HOME"
TEST_CONFIG="$RMW_FAKE_HOME/media-root.testrc"
printf 'WASTE = /tmp/.Trash-%s, removable\nexpire_age = 90\n' "$(id -u)" > "$TEST_CONFIG"

# Create waste dir manually (required because it is marked removable)
mkdir -p "$TRASH_DIR/files" "$TRASH_DIR/info"
mkdir -p "$TEST_DIR"

touch "$test_file_path"
"$BIN_DIR"/rmw -c /home/andy/.config/rmwrc "$test_file_path"
"$BIN_DIR"/rmw -c "$TEST_CONFIG" "$test_file_path"

output=$(grep Path /home/andy/src/rmw-project/.Trash-1000/info/$test_file.trashinfo)
test -f "$TRASH_DIR/info/$test_file.trashinfo"
test -f "$TRASH_DIR/files/$test_file"
test ! -f "$test_file_path"

# There should be no leading '/' in the filename.
path_expected=$(echo "${MESON_BUILD_ROOT}" | sed -e "s/\/home\/andy\/src\/rmw-project\///g")
echo "$path_expected"
test "$output" = "Path=${path_expected}/test/rmw-tests-home/test_media_root.sh_dir/media_root_test"
# The Path must be relative (no leading '/') since the waste folder is at
# the topdir of /tmp's partition.
output=$(grep '^Path=' "$TRASH_DIR/info/$test_file.trashinfo")
echo "trashinfo: $output"
case "$output" in
Path=/*)
echo "FAIL: Path is absolute, expected relative"
exit 1
;;
Path=*)
echo "PASS: Path is relative"
;;
*)
echo "FAIL: Path line not found"
exit 1
;;
esac

output=$("$BIN_DIR"/rmw -uvv -c /home/andy/.config/rmwrc | grep media_root_test)
# Restore and verify
"$BIN_DIR"/rmw -uvv -c "$TEST_CONFIG"
test -f "$test_file_path"
test ! -f "$TRASH_DIR/info/$test_file.trashinfo"

test "$output" = "+'/home/andy/src/rmw-project/.Trash-1000/files/media_root_test' -> '${MESON_BUILD_ROOT}/test/rmw-tests-home/test_media_root.sh_dir/media_root_test'
-/home/andy/src/rmw-project/.Trash-1000/info/media_root_test.trashinfo"
rm -f "$TEST_CONFIG"

exit 0
Loading