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
4 changes: 3 additions & 1 deletion tests/test_cd_and_exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_clone_and_url_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_clone_echo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion tests/test_create_new_and_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion tests/test_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_init_eval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_worktree_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_worktree_dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down