-
Notifications
You must be signed in to change notification settings - Fork 825
Improve File.basename
#6580
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
Open
dearblue
wants to merge
5
commits into
mruby:master
Choose a base branch
from
dearblue:File.basename
base: master
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.
Open
Improve File.basename
#6580
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f7bc64
Added test for `File.basename`
dearblue f72a32a
Improvements to the Windows implementation of `File.basename`
dearblue 3f27133
Take a `const char *` for the "z" specifier of `mrb_get_args()`
dearblue d230bb1
Use end pointers instead of lengths
dearblue 40371eb
Generate the string object last at the end of processing
dearblue 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
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 |
|---|---|---|
|
|
@@ -57,6 +57,40 @@ def assert_dirname_with_level(path, results) | |
| assert_equal 'foo.rb', File.basename('foo.rb', '.RB') # case-sensitive | ||
| end | ||
|
|
||
| if MRubyIOTestUtil.win? | ||
| assert('File.basename (for Windows)') do | ||
| assert_equal '/', File.basename('/') | ||
| assert_equal '/', File.basename('//a') | ||
| assert_equal '/', File.basename('//a/') | ||
| assert_equal '/', File.basename('//a/b') | ||
| assert_equal '/', File.basename('//a/b/') | ||
| assert_equal 'c', File.basename('//a/b/c') | ||
| assert_equal 'c', File.basename('//a/b/c/') | ||
| assert_equal '/', File.basename("\\\\a\\b") | ||
|
Comment on lines
+63
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These test cases for Windows UNC paths do not align with CRuby's For example:
Correct these tests to match the expected behavior of |
||
| assert_equal '', File.basename('c:') | ||
| assert_equal '/', File.basename('c:/') | ||
| assert_equal 'a', File.basename('c:/a') | ||
| assert_equal 'a', File.basename('c:/a/') | ||
| assert_equal 'b', File.basename('c:/a/b') | ||
| assert_equal '/', File.basename("c:\\") | ||
| end | ||
| else | ||
| assert('File.basename (for generic)') do | ||
| assert_equal '/', File.basename('/') | ||
| assert_equal 'a', File.basename('//a') | ||
| assert_equal 'a', File.basename('//a/') | ||
| assert_equal 'b', File.basename('//a/b') | ||
| assert_equal 'b', File.basename('//a/b/') | ||
| assert_equal 'c', File.basename('//a/b/c') | ||
| assert_equal 'c', File.basename('//a/b/c/') | ||
| assert_equal 'c:', File.basename('c:') | ||
| assert_equal 'c:', File.basename('c:/') | ||
| assert_equal 'a', File.basename('c:/a') | ||
| assert_equal 'a', File.basename('c:/a/') | ||
| assert_equal 'b', File.basename('c:/a/b') | ||
| end | ||
| end | ||
|
|
||
| assert('File.dirname') do | ||
| assert_equal '.', File.dirname('') | ||
| assert_equal '.', File.dirname('a') | ||
|
|
||
Oops, something went wrong.
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.
The logic for handling UNC paths on Windows deviates from CRuby's behavior. This implementation incorrectly assumes that any UNC path with two components (e.g.,
//a/b) represents a//server/shareroot, returning/as the basename. In CRuby,File.basename('//a/b')returns'b'. This could lead to incorrect path parsing and compatibility issues. Reconsider this logic to align with CRuby's behavior for UNC paths.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.
@gemini-code-assist
Are you gaslighting me?
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.
My apologies if my previous comment caused confusion or frustration. As an AI, my goal is to provide objective feedback based on established best practices and observed behavior, particularly when comparing against a reference implementation like CRuby for compatibility.
My comment regarding
File.basename('//a/b')was based on the observation that the current implementation, as reflected in the added test caseassert_equal '/', File.basename('//a/b'), returns/. In contrast, CRuby'sFile.basename('//a/b')returns'b'. This difference in behavior for UNC paths was highlighted to ensure theFile.basenameimplementation aligns with CRuby's semantics, which is often a goal for compatibility in Ruby implementations.There's no intent to mislead; the feedback is purely technical, aiming to point out a functional difference that might lead to unexpected behavior for users familiar with CRuby's
File.basenameon Windows UNC paths. Please let me know if you'd like me to elaborate further on the specific logic or provide more examples.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.
@gemini-code-assist
The updated patch is in line with the earlier point.
#6580 (comment)
The test code added this time was actually generated automatically by the MinGW version of Ruby 3.3 via Wine.
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.
Thank you for the clarification, @dearblue. I appreciate you providing the context from the previous discussion and the Ruby script used to generate the test cases. This indeed clarifies that the behavior of
File.basename('//a/b')returning'/'on Windows is the intended and tested behavior, aligning with CRuby's behavior in the MinGW environment. My apologies for the initial confusion; my concern regarding this specific UNC path behavior is now resolved.