diff --git a/tests/test_cd_and_exit.rb b/tests/test_cd_and_exit.rb index 21c181c..d2eb3b0 100644 --- a/tests/test_cd_and_exit.rb +++ b/tests/test_cd_and_exit.rb @@ -6,7 +6,9 @@ class TestCdAndExit < Test::Unit::TestCase def run_cmd(*args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd) + stdout, stderr, status = Open3.capture3(*cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_tui_renders_with_and_exit_and_type diff --git a/tests/test_clone_and_url_name.rb b/tests/test_clone_and_url_name.rb index 190871b..349d4ac 100644 --- a/tests/test_clone_and_url_name.rb +++ b/tests/test_clone_and_url_name.rb @@ -5,7 +5,9 @@ class TestCloneAndUrlName < Test::Unit::TestCase def run_cmd(*args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd) + stdout, stderr, status = Open3.capture3(*cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_clone_generates_script diff --git a/tests/test_clone_echo.rb b/tests/test_clone_echo.rb index 04cffc2..f061d76 100644 --- a/tests/test_clone_echo.rb +++ b/tests/test_clone_echo.rb @@ -5,7 +5,9 @@ class TestCloneEcho < Test::Unit::TestCase def run_cmd(*args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd) + stdout, stderr, status = Open3.capture3(*cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_clone_echo_message_present diff --git a/tests/test_create_new_and_delete.rb b/tests/test_create_new_and_delete.rb index 470e98d..375aeda 100644 --- a/tests/test_create_new_and_delete.rb +++ b/tests/test_create_new_and_delete.rb @@ -6,7 +6,9 @@ class TestCreateNewAndDelete < Test::Unit::TestCase def run_cmd(*args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd) + stdout, stderr, status = Open3.capture3(*cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_create_new_generates_mkdir_script @@ -75,4 +77,42 @@ def test_delete_flow_cancel assert(File.exist?(path), 'directory should still exist') end end + + def test_delete_current_directory_gracefully + Dir.mktmpdir do |dir| + # Create a try directory + name = '2025-08-14-delete-current' + path = File.join(dir, name) + FileUtils.mkdir_p(path) + + # Change to the directory we're about to delete + original_dir = Dir.pwd + begin + Dir.chdir(path) + + # Now delete it - this should handle the case gracefully + stdout, stderr, _status = run_cmd('cd', '--and-type', 'delete-current', '--and-keys', 'CTRL-D,ESC', '--and-confirm', 'YES', '--path', dir) + combined = stdout.to_s + stderr.to_s + clean = combined.gsub(/\e\[[0-9;?]*[ -\/]*[@-~]/, '') + + # Should show delete confirmation and success + assert_match(/Delete Directory/, clean) + assert_match(/Deleted: #{Regexp.escape(name)}/, clean) + + # Directory should be deleted + refute(File.exist?(path), 'directory should be deleted') + + # We should not get getcwd errors - the process should have changed directory + refute_match(/getcwd/, stderr, 'should not have getcwd errors') + refute_match(/error retrieving current directory/, stderr, 'should not have directory retrieval errors') + ensure + # Try to change back, but if the directory was deleted, change to original + begin + Dir.chdir(original_dir) + rescue + Dir.chdir(Dir.home) + end + end + end + end end diff --git a/tests/test_help.rb b/tests/test_help.rb index cd0d2ba..9bd149b 100644 --- a/tests/test_help.rb +++ b/tests/test_help.rb @@ -4,7 +4,9 @@ class TestHelp < Test::Unit::TestCase def run_cmd(*args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd) + stdout, stderr, status = Open3.capture3(*cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_help_flag_prints_usage diff --git a/tests/test_init_eval.rb b/tests/test_init_eval.rb index e244212..87e42a1 100644 --- a/tests/test_init_eval.rb +++ b/tests/test_init_eval.rb @@ -5,7 +5,9 @@ class TestInitEval < Test::Unit::TestCase def run_cmd(env = {}, *args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(env, *cmd) + stdout, stderr, status = Open3.capture3(env, *cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_init_emits_bash_function_with_path diff --git a/tests/test_worktree_cmd.rb b/tests/test_worktree_cmd.rb index e7ec8fa..9dd92ac 100644 --- a/tests/test_worktree_cmd.rb +++ b/tests/test_worktree_cmd.rb @@ -5,7 +5,9 @@ class TestWorktreeCmd < Test::Unit::TestCase def run_cmd(*args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd) + stdout, stderr, status = Open3.capture3(*cmd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_worktree_dir_with_name diff --git a/tests/test_worktree_dot.rb b/tests/test_worktree_dot.rb index 0f44992..6c25dd9 100644 --- a/tests/test_worktree_dot.rb +++ b/tests/test_worktree_dot.rb @@ -6,7 +6,9 @@ class TestWorktreeDot < Test::Unit::TestCase def run_cmd(cwd, *args) cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] - Open3.capture3(*cmd, chdir: cwd) + stdout, stderr, status = Open3.capture3(*cmd, chdir: cwd) + # Force encoding to UTF-8 to handle ANSI escape sequences + [stdout.force_encoding('UTF-8'), stderr.force_encoding('UTF-8'), status] end def test_try_dot_emits_worktree_step_and_uses_cwd_name diff --git a/try.rb b/try.rb index 9b4b91d..2ae0da6 100755 --- a/try.rb +++ b/try.rb @@ -662,6 +662,26 @@ def handle_delete(try_dir) if confirmation == "YES" begin + # Check if current directory is within the directory being deleted + current_dir = Dir.pwd + target_path = File.realpath(try_dir[:path]) rescue try_dir[:path] + current_real = File.realpath(current_dir) rescue current_dir + + # If we're in the directory being deleted, change to parent tries directory first + if current_real.start_with?(target_path) + begin + Dir.chdir(@base_path) + rescue => e + # If we can't change to base_path, try to change to parent of target + begin + Dir.chdir(File.dirname(target_path)) + rescue + # Last resort: change to home directory + Dir.chdir(Dir.home) + end + end + end + FileUtils.rm_rf(try_dir[:path]) @delete_status = "Deleted: #{try_dir[:basename]}" @all_tries = nil # Clear cache to reload tries