diff --git a/test/run_tests.sh b/test/run_tests.sh index 3f8e5149..71ebd38a 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -145,6 +145,47 @@ test_untar_D_flag_commit() fi } +test_untar_D_flag_commit_without_cleanup() +{ + local shell=$1 + cp $RESOURCE_DIR/* "$2/" + # Will always commit the result in case of try + if [ "$shell" == "bash" ]; then + $shell gunzip $2/file.txt.gz + else + try_example_dir=$(mktemp -d) + $shell -D $try_example_dir gunzip $2/file.txt.gz + if ! [ -d "$try_example_dir" ]; then + echo "try_example_dir does not exist" + return 1 + fi + $shell commit $try_example_dir + if ! [ -d "$try_example_dir" ]; then + echo "try_example_dir does not exist" + return 1 + fi + fi +} + +test_touch_and_rm_with_cleanup() +{ + TMPDIR="/tmp" + local shell=$1 + cp $RESOURCE_DIR/* "$2/" + # Will always commit the result in case of try + if [ "$shell" == "bash" ]; then + $shell $MISC_SCRIPT_DIR/touch_echo_and_rm.sh $2/file_1.txt $2/file_2.txt $2/file.txt.gz + else + tmp_file_count1=$(ls $TMPDIR | wc -l) + $shell -y $MISC_SCRIPT_DIR/touch_echo_and_rm.sh $2/file_1.txt $2/file_2.txt $2/file.txt.gz + tmp_file_count2=$(ls $TMPDIR | wc -l) + # We save try_mount_log in /tmp/ dir + if [ $tmp_file_count1 -ne $(($tmp_file_count2 - 1)) ]; then + return 1 + fi + fi +} + test_touch_and_rm_no_flag() { local shell=$1 @@ -219,6 +260,8 @@ if [ "$#" -eq 0 ]; then run_test test_touch_and_rm_no_flag # run_test test_touch_and_rm_n_flag_commit run_test test_touch_and_rm_D_flag_commit + run_test test_touch_and_rm_with_cleanup + run_test test_untar_D_flag_commit_without_cleanup else for testname in $@ diff --git a/try b/try index aaa91706..769dcbc4 100755 --- a/try +++ b/try @@ -45,7 +45,7 @@ try() { export try_mount_log=$(mktemp) # Cleanup the temporary files - CLEANUP="rm -rf $mount_and_execute $chroot_executable $try_mount_log" + CLEANUP="$mount_and_execute $chroot_executable" if [ "$SANDBOX_DIR_EXISTS" != "1" ]; then CLEANUP="$CLEANUP $SANDBOX_DIR" fi @@ -101,8 +101,6 @@ EOF chmod +x "$mount_and_execute" "$chroot_executable" - trap "$CLEANUP" EXIT - # --mount: mounting and unmounting filesystems will not affect the rest of the system outside the unshare # --map-root-user: map to the superuser UID and GID in the newly created user namespace. # --user: the process will have a distinct set of UIDs, GIDs and capabilities. @@ -118,24 +116,37 @@ EOF case "$NO_COMMIT" in (quiet) ;; (show) printf "%s\n" "$SANDBOX_DIR";; - (commit) commit "$SANDBOX_DIR";; + (commit) commit "$SANDBOX_DIR" + cleanup $CLEANUP;; (interactive) summary "$SANDBOX_DIR" >&2 - if [ "$exitcode" -eq 0 ] && [ "$?" -eq 0 ] + if [ "$?" -eq 0 ] then echo read -p "Commit these changes? [y/N] " DO_COMMIT >&2 case "$DO_COMMIT" in - (y|Y|yes|YES) commit "$SANDBOX_DIR";; + (y|Y|yes|YES) commit "$SANDBOX_DIR" + cleanup $CLEANUP;; (*) printf "Not committing.\n" >&2 echo "$SANDBOX_DIR";; esac fi ;; esac - $CLEANUP } +################################################################################ +# Cleanup temporary files +################################################################################ + +cleanup() { + if [ -n "$CLEANUP" ]; then + echo "Cleaning up temporary files: $CLEANUP" + rm -rf $CLEANUP + fi +} + + ################################################################################ # Summarize an overlay ################################################################################