Skip to content

Commit

Permalink
Fix FileUtils.ln_sf to override special file types (#13896)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Oct 19, 2023
1 parent a7f7520 commit 9e84f6e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions spec/std/file_utils_spec.cr
Expand Up @@ -715,6 +715,41 @@ describe "FileUtils" do
end
end

{% if flag?(:unix) %}
it "overwrites a destination named pipe" do
with_tempfile("ln_sf_src", "ln_sf_dst_pipe_exists") do |path1, path2|
test_with_string_and_path(path1, path2) do |arg1, arg2|
FileUtils.touch([path1])
`mkfifo #{path2}`
File.symlink?(path1).should be_false
File.symlink?(path2).should be_false

FileUtils.ln_sf(arg1, arg2)
File.symlink?(path1).should be_false
File.symlink?(path2).should be_true
FileUtils.rm_rf([path1, path2])
end
end
end
{% end %}

it "overwrites a destination dir in dir" do
with_tempfile("ln_sf_src", "ln_sf_dst_dir_exists") do |path1, path2|
test_with_string_and_path(path1, path2) do |arg1, arg2|
FileUtils.touch([path1])
dir_dest = File.join(path2, File.basename(path1))
Dir.mkdir_p(dir_dest)
File.symlink?(path1).should be_false
File.symlink?(path2).should be_false

FileUtils.ln_sf(arg1, arg2)
File.symlink?(path1).should be_false
File.symlink?(dir_dest).should be_true
FileUtils.rm_rf([path1, path2])
end
end
end

it "overwrites a destination file inside a dir" do
with_tempfile("ln_sf_dst_dir", "ln_sf_dst") do |dir, path2|
dir += File::SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion src/file_utils.cr
Expand Up @@ -223,7 +223,7 @@ module FileUtils
dest_path = File.join(dest_path, File.basename(src_path))
end

File.delete(dest_path) if File.file?(dest_path)
rm_rf(dest_path) if File.exists?(dest_path)
File.symlink(src_path, dest_path)
end

Expand Down

0 comments on commit 9e84f6e

Please sign in to comment.