Skip to content

Commit be6e37f

Browse files
presidentbeefhsbt
authored andcommitted
Improve same directory detection in FileUtils
Closes: ruby/ruby#1425
1 parent a409ca7 commit be6e37f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/fileutils.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,10 +1563,13 @@ def join(dir, base)
15631563
else
15641564
DIRECTORY_TERM = "(?=/|\\z)"
15651565
end
1566-
SYSCASE = File::FNM_SYSCASE.nonzero? ? "-i" : ""
15671566

15681567
def descendant_directory?(descendant, ascendant)
1569-
/\A(?#{SYSCASE}:#{Regexp.quote(ascendant)})#{DIRECTORY_TERM}/ =~ File.dirname(descendant)
1568+
if File::FNM_SYSCASE.nonzero?
1569+
File.expand_path(File.dirname(descendant)).casecmp(File.expand_path(ascendant)) == 0
1570+
else
1571+
File.expand_path(File.dirname(descendant)) == File.expand_path(ascendant)
1572+
end
15701573
end
15711574
end # class Entry_
15721575

test/fileutils/test_fileutils.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,16 @@ def test_cp_r
381381
assert_same_file 'tmp/cpr_src/b', 'tmp/cpr_dest/b'
382382
assert_same_file 'tmp/cpr_src/c', 'tmp/cpr_dest/c'
383383
assert_directory 'tmp/cpr_dest/d'
384+
assert_raise(ArgumentError) do
385+
cp_r 'tmp/cpr_src', './tmp/cpr_src'
386+
end
387+
assert_raise(ArgumentError) do
388+
cp_r './tmp/cpr_src', 'tmp/cpr_src'
389+
end
390+
assert_raise(ArgumentError) do
391+
cp_r './tmp/cpr_src', File.expand_path('tmp/cpr_src')
392+
end
393+
384394
my_rm_rf 'tmp/cpr_src'
385395
my_rm_rf 'tmp/cpr_dest'
386396

0 commit comments

Comments
 (0)