-
Notifications
You must be signed in to change notification settings - Fork 71
Fix: Handle current directory deletion gracefully to prevent shell errors #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
2
commits into
main
Choose a base branch
from
copilot/fix-c84d6bd7-1c63-458f-b1c3-73956f250398
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| require 'test/unit' | ||
| require 'open3' | ||
| require 'tmpdir' | ||
| require 'fileutils' | ||
|
|
||
| class TestDeleteCurrentDirectory < Test::Unit::TestCase | ||
| def run_cmd(cwd, *args) | ||
| cmd = [RbConfig.ruby, File.expand_path('../try.rb', __dir__), *args] | ||
| Open3.capture3(*cmd, chdir: cwd) | ||
| end | ||
|
|
||
| def test_delete_current_directory_should_not_cause_shell_error | ||
| Dir.mktmpdir do |tries_root| | ||
| # Create a test directory | ||
| test_dir_name = '2025-09-26-test-delete-current' | ||
| test_dir_path = File.join(tries_root, test_dir_name) | ||
| FileUtils.mkdir_p(test_dir_path) | ||
|
|
||
| # Run try cd from within the directory we're about to delete | ||
| # This simulates the user being inside a try directory and deleting it | ||
| stdout, stderr, status = run_cmd( | ||
| test_dir_path, # Run from within the directory to be deleted | ||
| 'cd', | ||
| '--and-type', 'test-delete-current', # Search for the directory | ||
| '--and-keys', 'CTRL-D,ESC', # Delete it (Ctrl-D) then exit (ESC) | ||
| '--and-confirm', 'YES', # Confirm deletion | ||
| '--path', tries_root | ||
| ) | ||
|
|
||
| # The directory should be deleted | ||
| refute(File.exist?(test_dir_path), 'directory should be deleted') | ||
|
|
||
| # There should be no shell errors about getcwd or current directory | ||
| combined_output = stdout.to_s + stderr.to_s | ||
| refute_match( | ||
| /shell-init: error retrieving current directory|getcwd: cannot access parent directories/, | ||
| combined_output, | ||
| 'should not have shell errors about current directory' | ||
| ) | ||
|
|
||
| # Should show successful deletion | ||
| clean_output = combined_output.gsub(/\e\[[0-9;?]*[ -\/]*[@-~]/, '') | ||
| assert_match(/Deleted: #{Regexp.escape(test_dir_name)}/, clean_output) | ||
| end | ||
| end | ||
|
|
||
| def test_delete_current_directory_changes_to_safe_location | ||
| Dir.mktmpdir do |tries_root| | ||
| # Create a test directory | ||
| test_dir_name = '2025-09-26-test-safe-nav' | ||
| test_dir_path = File.join(tries_root, test_dir_name) | ||
| FileUtils.mkdir_p(test_dir_path) | ||
|
|
||
| # Run try cd from within the directory we're about to delete | ||
| # After deletion, just exit (ESC) to avoid navigation complexity | ||
| stdout, stderr, status = run_cmd( | ||
| test_dir_path, # Run from within the directory to be deleted | ||
| 'cd', | ||
| '--and-type', 'test-safe-nav', # Search for the directory to delete | ||
| '--and-keys', 'CTRL-D,ESC', # Delete it then exit | ||
| '--and-confirm', 'YES', # Confirm deletion | ||
| '--path', tries_root | ||
| ) | ||
|
|
||
| # The directory should be deleted | ||
| refute(File.exist?(test_dir_path), 'directory should be deleted') | ||
|
|
||
| # Should not have shell errors about current directory | ||
| combined_output = stdout.to_s + stderr.to_s | ||
| refute_match( | ||
| /shell-init: error retrieving current directory|getcwd.*failed|getcwd: cannot access parent directories/, | ||
| combined_output, | ||
| 'should not have shell errors about current directory' | ||
| ) | ||
|
|
||
| # Should show successful deletion | ||
| clean_output = combined_output.gsub(/\e\[[0-9;?]*[ -\/]*[@-~]/, '') | ||
| assert_match(/Deleted: #{Regexp.escape(test_dir_name)}/, clean_output) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this entire thing could be done vastly simpler by just appending an cd .. to the end of the script if the current directory is the deleted one.