Subject: bug#25609: 25.1; Dired: Copy folder with 'dired-dwim-target t' results in nested copy

From: Tino Calancha <[email-masked]>
Date: Mon, 20 Feb 2017 14:32:17 +0900

Tino Calancha <[email-masked]> writes:

> Same behaviour in Emacs-24 and master branch.
> Following patch works to me:
Added a test.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>From 2795fd89177714712c2245294d79241d6dd36c70 Mon Sep 17 00:00:00 2001
From: Tino Calancha <[email-masked]>
Date: Mon, 20 Feb 2017 14:27:12 +0900
Subject: [PATCH] Choose the right target dir on dired operations

Prevent from changing the input target dir
when dired-dwim-target is non-nil (Bug#25609).
* lisp/dired-aux.el (dired-do-create-files):
If dired-dwim-target is non-nil, then bind 'default' to nil.
* test/lisp/dired-tests.el (dired-test-bug25609): Add test.
---
 lisp/dired-aux.el        |  1 +
 test/lisp/dired-tests.el | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index caa3b45705..d7ca052787 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1791,6 +1791,7 @@ dired-do-create-files
 	  (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
 	 (target-dir (dired-dwim-target-directory))
 	 (default (and dired-one-file
+		       (not dired-dwim-target) ; Bug#25609
 		       (expand-file-name (file-name-nondirectory (car fn-list))
 					 target-dir)))
 	 (defaults (dired-dwim-target-defaults fn-list target-dir))
diff --git a/test/lisp/dired-tests.el b/test/lisp/dired-tests.el
index 489f8fdfea..1863864abd 100644
--- a/test/lisp/dired-tests.el
+++ b/test/lisp/dired-tests.el
@@ -20,6 +20,7 @@
 ;;; Code:
 (require 'ert)
 (require 'dired)
+(require 'nadvice)

 (ert-deftest dired-autoload ()
@@ -52,5 +53,36 @@
       ;; Clean up
       (delete-directory dir 'recursive))))
 
+(ert-deftest dired-test-bug25609 ()
+  "Test for http://debbugs.gnu.org/25609 ."
+  (let* ((from (make-temp-file "foo" 'dir))
+         (to (make-temp-file "bar" 'dir))
+         (target (expand-file-name (file-name-nondirectory from) to))
+         (nested (expand-file-name (file-name-nondirectory from) target))
+         (dired-dwim-target t)
+         (dired-recursive-copies 'always)) ; Don't prompt me.
+    (advice-add 'dired-query ; Don't ask confirmation to overwrite a file.
+                :override
+                (lambda (sym prompt &rest args) (setq dired-query t))
+                '((name . "advice-dired-query")))
+    (advice-add 'completing-read ; Just return init.
+                :override
+                (lambda (prompt coll &optional pred match init hist def inherit keymap)
+                  init)
+                '((name . "advice-completing-read")))
+    (dired to)
+    (dired-other-window temporary-file-directory)
+    (dired-goto-file from)
+    (dired-do-copy)
+    (dired-do-copy); Again.
+    (unwind-protect
+        (progn
+          (should (file-exists-p target))
+          (should-not (file-exists-p nested)))
+      (delete-directory from 'recursive)
+      (delete-directory to 'recursive)
+      (advice-remove 'dired-query "advice-dired-query")
+      (advice-remove 'completing-read "advice-completing-read"))))
+
 (provide 'dired-tests)
 ;; dired-tests.el ends here
-- 
2.11.0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.7)
 of 2017-02-20
Repository revision: 0a670690f19bf263dadfe387e5bb22311e3b5231