Skip to content

Commit 279ea2a

Browse files
committed
Fix File.cp_r/3 in case of a file coping to itself
1 parent f36e7a6 commit 279ea2a

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

lib/elixir/lib/file.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ defmodule File do
536536
537537
"""
538538
@spec cp_r(Path.t, Path.t, (Path.t, Path.t -> boolean)) :: {:ok, [binary]} | {:error, posix, binary}
539-
def cp_r(source, destination, callback \\ fn(_, _) -> true end) when is_function(callback) do
539+
def cp_r(source, destination, callback \\ &path_differs?/2) when is_function(callback) do
540540
source = IO.chardata_to_string(source)
541541
destination = IO.chardata_to_string(destination)
542542

@@ -551,7 +551,7 @@ defmodule File do
551551
Returns the list of copied files otherwise.
552552
"""
553553
@spec cp_r!(Path.t, Path.t, (Path.t, Path.t -> boolean)) :: [binary] | no_return
554-
def cp_r!(source, destination, callback \\ fn(_, _) -> true end) do
554+
def cp_r!(source, destination, callback \\ &path_differs?/2) do
555555
case cp_r(source, destination, callback) do
556556
{:ok, files} -> files
557557
{:error, reason, file} ->

lib/elixir/test/elixir/file_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,15 @@ defmodule FileTest do
129129
end
130130
end
131131

132-
test :cp_file_to_itself do
132+
test :copy_file_to_itself do
133133
src = dest = tmp_path("tmp.file")
134134

135135
File.write!(src, "here")
136136

137137
try do
138138
assert File.cp(src, dest) == :ok
139139
assert File.read!(dest) == "here"
140+
assert File.cp_r(src, dest) == {:ok, []}
140141
after
141142
File.rm(dest)
142143
end

0 commit comments

Comments
 (0)