Skip to content
Open
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
1 change: 1 addition & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pane_content_files_restore_from_archive() {
local archive_file="$(pane_contents_archive_file)"
if [ -f "$archive_file" ]; then
mkdir -p "$(pane_contents_dir "restore")"
rm -f "$(pane_contents_dir "restore")"/*
gzip -d < "$archive_file" |
tar xf - -C "$(resurrect_dir)/restore/"
fi
Expand Down
16 changes: 5 additions & 11 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ tmux_default_command() {
}

pane_creation_command() {
echo "cat '$(pane_contents_file "restore" "${1}:${2}.${3}")'; exec $(tmux_default_command)"
local pane_content_file="$(pane_contents_file "restore" "${1}:${2}.${3}")"
echo "cat '$pane_content_file'; rm -f '$pane_content_file'; exec $(tmux_default_command)"
}

new_window() {
Expand Down Expand Up @@ -355,14 +356,6 @@ restore_active_and_alternate_sessions() {
done < $(last_resurrect_file)
}

# A cleanup that happens after 'restore_all_panes' seems to fix fish shell
# users' restore problems.
cleanup_restored_pane_contents() {
if is_restoring_pane_contents; then
rm "$(pane_contents_dir "restore")"/*
fi
}

main() {
if supported_tmux_version_ok && check_saved_session_exists; then
start_spinner "Restoring..." "Tmux restore complete!"
Expand All @@ -378,10 +371,11 @@ main() {
restore_grouped_sessions # also restores active and alt windows for grouped sessions
restore_active_and_alternate_windows
restore_active_and_alternate_sessions
cleanup_restored_pane_contents
execute_hook "post-restore-all"
stop_spinner
display_message "Tmux restore complete!"
fi
}
main
if [ "${BASH_SOURCE[0]}" == "$0" ]; then
main
fi
92 changes: 92 additions & 0 deletions tests/test_resurrect_restore.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,97 @@
#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIR="$( cd "$CURRENT_DIR/.." && pwd )"

fail_unit_test() {
local message="$1"
if command -v fail_helper >/dev/null 2>&1; then
fail_helper "$message"
else
printf '%s\n' "$message" >&2
exit 1
fi
}

make_temp_dir() {
mktemp -d "${TMPDIR:-/tmp}/tmux-resurrect-test.XXXXXX"
}

pane_creation_command_removes_restored_file_after_cat() {
local tmp_dir actual expected pane_file
tmp_dir="$(make_temp_dir)" || fail_unit_test "Could not create temp dir"
actual="$(
tmux() { return 1; }
source "$PROJECT_DIR/scripts/restore.sh"
_RESURRECT_DIR="$tmp_dir"
TMUX_DEFAULT_COMMAND="/bin/sh"
pane_creation_command "session" "1" "2"
)"
pane_file="$tmp_dir/restore/pane_contents//pane-session:1.2"
expected="cat '$pane_file'; rm -f '$pane_file'; exec /bin/sh"
rm -rf "$tmp_dir"
if [ "$actual" != "$expected" ]; then
fail_unit_test "Pane creation command does not remove its content file"
fi
}

pane_content_files_restore_from_archive_clears_stale_files_before_extract() {
local tmp_dir status
tmp_dir="$(make_temp_dir)" || fail_unit_test "Could not create temp dir"
(
tmux() { return 1; }
source "$PROJECT_DIR/scripts/helpers.sh"
_RESURRECT_DIR="$tmp_dir"
mkdir -p "$tmp_dir/save/pane_contents" "$tmp_dir/restore/pane_contents"
printf 'saved\n' > "$tmp_dir/save/pane_contents/pane-current"
printf 'stale\n' > "$tmp_dir/restore/pane_contents/pane-stale"
pane_contents_create_archive
pane_content_files_restore_from_archive
[ -f "$tmp_dir/restore/pane_contents/pane-current" ] || exit 10
[ ! -e "$tmp_dir/restore/pane_contents/pane-stale" ] || exit 11
)
status="$?"
rm -rf "$tmp_dir"
if [ "$status" -eq 10 ]; then
fail_unit_test "Pane contents archive was not extracted"
elif [ "$status" -eq 11 ]; then
fail_unit_test "Stale pane contents were not cleared before extract"
elif [ "$status" -ne 0 ]; then
fail_unit_test "Pane contents archive restore test failed"
fi
}

pane_content_files_restore_from_archive_skips_cleanup_without_archive() {
local tmp_dir status
tmp_dir="$(make_temp_dir)" || fail_unit_test "Could not create temp dir"
(
tmux() { return 1; }
source "$PROJECT_DIR/scripts/helpers.sh"
_RESURRECT_DIR="$tmp_dir"
mkdir -p "$tmp_dir/restore/pane_contents"
printf 'stale\n' > "$tmp_dir/restore/pane_contents/pane-stale"
pane_content_files_restore_from_archive
[ -f "$tmp_dir/restore/pane_contents/pane-stale" ] || exit 10
)
status="$?"
rm -rf "$tmp_dir"
if [ "$status" -eq 10 ]; then
fail_unit_test "Pane contents dir was touched without an archive"
elif [ "$status" -ne 0 ]; then
fail_unit_test "Pane contents no-archive restore test failed"
fi
}

run_pane_contents_unit_tests() {
pane_creation_command_removes_restored_file_after_cat
pane_content_files_restore_from_archive_clears_stale_files_before_extract
pane_content_files_restore_from_archive_skips_cleanup_without_archive
}

if [ "$1" == "--pane-contents-unit-tests" ]; then
run_pane_contents_unit_tests
exit 0
fi

source $CURRENT_DIR/helpers/helpers.sh
source $CURRENT_DIR/helpers/resurrect_helpers.sh
Expand All @@ -21,6 +112,7 @@ restore_tmux_environment_and_save_again() {
}

main() {
run_pane_contents_unit_tests
install_tmux_plugin_under_test_helper
setup_before_restore
restore_tmux_environment_and_save_again
Expand Down