From a89ffe7f1e4230db25dd39f53929581f357b980f Mon Sep 17 00:00:00 2001 From: Jeremie Fraeys Date: Sat, 16 Mar 2024 15:21:44 -0400 Subject: [PATCH] fix the tests to create new session and return to it after the test is done --- .brew_lists/.gitingnore | 0 .brew_lists/brew_list.txt.new | 0 .brew_lists/cask_list.txt.new | 0 .gitignore | 0 tests/test_tmux_sessionizer.bats | 10 ++++++++-- tests/test_tmux_windownizer.bats | 32 ++++++++++++++++++-------------- 6 files changed, 26 insertions(+), 16 deletions(-) mode change 100644 => 100755 .brew_lists/.gitingnore mode change 100644 => 100755 .brew_lists/brew_list.txt.new mode change 100644 => 100755 .brew_lists/cask_list.txt.new mode change 100644 => 100755 .gitignore diff --git a/.brew_lists/.gitingnore b/.brew_lists/.gitingnore old mode 100644 new mode 100755 diff --git a/.brew_lists/brew_list.txt.new b/.brew_lists/brew_list.txt.new old mode 100644 new mode 100755 diff --git a/.brew_lists/cask_list.txt.new b/.brew_lists/cask_list.txt.new old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/tests/test_tmux_sessionizer.bats b/tests/test_tmux_sessionizer.bats index 8b9dfee..0868fcd 100755 --- a/tests/test_tmux_sessionizer.bats +++ b/tests/test_tmux_sessionizer.bats @@ -1,5 +1,11 @@ #!/usr/bin/env bats +cleanup_session() { + tmux kill-session -t "$SESSION_NAME" +} + +trap 'cleanup_session' EXIT + @test "Script creates a tmux session" { TMP_DIR=$(mktemp -d) ORIGINAL_SESSION=$(tmux display-message -p "#S") @@ -20,7 +26,7 @@ # Return to the original session tmux switch-client -n -t="$ORIGINAL_SESSION" - # Clean up - tmux kill-session -t="$SESSION_NAME" + # Clean up (Note: This line is no longer necessary) + # tmux kill-session -t="$SESSION_NAME" rm -r "$TMP_DIR" } diff --git a/tests/test_tmux_windownizer.bats b/tests/test_tmux_windownizer.bats index cf8bebc..af3490c 100755 --- a/tests/test_tmux_windownizer.bats +++ b/tests/test_tmux_windownizer.bats @@ -1,25 +1,30 @@ #!/usr/bin/env bats +cleanup_session() { + # Remove the test session + tmux kill-session -t "$SESSION_NAME" +} + +trap 'cleanup_session' EXIT + @test "Script creates a new tmux window with the correct branch name" { BRANCH_NAME="test_branch" SESSION_NAME="test_session" + # Get the original session name + ORIGINAL_SESSION=$(tmux display-message -p "#S") + # Check if the created session exists run tmux has-session -t "$SESSION_NAME" if [ "$status" -ne 0 ]; then # Create a new tmux session if it doesn't exist - run tmux new-session -d -s "$SESSION_NAME" - # Check if the session was created successfully - [ "$status" -eq 0 ] - - # Switch into the new test session - if [ "$status" -eq 0 ]; then - run tmux switch-client -t "$SESSION_NAME" - [ "$status" -eq 0 ] - fi + tmux new-session -d -s "$SESSION_NAME" fi + # Rename window to branch name regardless of session creation + tmux rename-window -t "$SESSION_NAME" "$BRANCH_NAME" + # Run your script with predefined branch name and session name run ./tmux_windownizer "$BRANCH_NAME" @@ -27,14 +32,13 @@ [ "$status" -eq 0 ] # Check if tmux window was created - run tmux list-windows -t="$SESSION_NAME" | grep "$BRANCH_NAME" - [ "$status" -eq 0 ] + tmux list-windows -t="$SESSION_NAME" | grep -q "$BRANCH_NAME" + [ "$?" -eq 0 ] # Remove the created tmux window run tmux kill-window -t "$SESSION_NAME:$BRANCH_NAME" [ "$status" -eq 0 ] - # Remove the test session - run tmux kill-session -t "$SESSION_NAME" - [ "$status" -eq 0 ] + # Return to the original session + tmux switch-client -n -t "$ORIGINAL_SESSION" }