From 856e0ec2f81bfe2f6721369a1df9a26f41e6d1a4 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Thu, 27 May 2021 01:19:36 +0300 Subject: * lisp/vc/diff-mode.el (diff-hunk-text): Test-driven fix for newlines. * lisp/vc/diff-mode.el (diff-hunk-text): Fix handling of newlines to cover all test cases according to new test. * test/lisp/vc/diff-mode-tests.el (diff-mode-test-hunk-text-no-newline): New test to cover cases with no newline at end of file. --- test/lisp/vc/diff-mode-tests.el | 142 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) (limited to 'test/lisp/vc/diff-mode-tests.el') diff --git a/test/lisp/vc/diff-mode-tests.el b/test/lisp/vc/diff-mode-tests.el index f4e5c89afb4..5bc4ad6dace 100644 --- a/test/lisp/vc/diff-mode-tests.el +++ b/test/lisp/vc/diff-mode-tests.el @@ -203,6 +203,148 @@ youthfulness (kill-buffer buf2) (delete-directory temp-dir 'recursive)))))) +(ert-deftest diff-mode-test-hunk-text-no-newline () + "Check output of `diff-hunk-text' with no newline at end of file." + + ;; First check unified change/remove/add cases with newline + (let ((hunk "\ +@@ -1 +1 @@ +-foo ++bar +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo +")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar +"))) + + (let ((hunk "\ +@@ -1 +0,0 @@ +-foo +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo +")) + (should (equal (diff-hunk-text hunk t nil) "\ +"))) + + (let ((hunk "\ +@@ -0,0 +1 @@ ++bar +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar +"))) + + ;; Check unified change/remove cases with no newline in old file + (let ((hunk "\ +@@ -1 +1 @@ +-foo +\\ No newline at end of file ++bar +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar +"))) + + (let ((hunk "\ +@@ -1 +0,0 @@ +-foo +\\ No newline at end of file +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo")) + (should (equal (diff-hunk-text hunk t nil) "\ +"))) + + ;; Check unified change/add cases with no newline in new file + (let ((hunk "\ +@@ -1 +1 @@ +-foo ++bar +\\ No newline at end of file +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo +")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar"))) + + (let ((hunk "\ +@@ -0,0 +1 @@ ++bar +\\ No newline at end of file +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar"))) + + ;; Check unified change case with no newline in both old/new file + (let ((hunk "\ +@@ -1 +1 @@ +-foo +\\ No newline at end of file ++bar +\\ No newline at end of file +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar"))) + + ;; Check context-after unified change case with no newline in both old/new file + (let ((hunk "\ +@@ -1,2 +1,2 @@ +-foo ++bar + baz +\\ No newline at end of file +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo +baz")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar +baz"))) + + (let ((hunk "\ +@@ -1,2 +1,2 @@ +-foo +-baz +\\ No newline at end of file ++bar ++baz +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo +baz")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar +baz +"))) + + (let ((hunk "\ +@@ -1,2 +1,2 @@ +-foo +-baz ++bar ++baz +\\ No newline at end of file +")) + (should (equal (diff-hunk-text hunk nil nil) "\ +foo +baz +")) + (should (equal (diff-hunk-text hunk t nil) "\ +bar +baz")))) + (ert-deftest diff-mode-test-font-lock () "Check font-locking of diff hunks." ;; See comments in diff-hunk-file-names about nonascii. -- cgit v1.2.3 From 3c053ef14248742726e4a44dc4a809874ce76c72 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 14 Aug 2021 15:50:55 +0200 Subject: Filter out -L foo labels in diff-hunk-file-names * lisp/vc/diff-mode.el (diff-hunk-file-names): Filter out "-L foo" labels (bug#10160). --- lisp/vc/diff-mode.el | 10 +++++----- test/lisp/vc/diff-mode-tests.el | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'test/lisp/vc/diff-mode-tests.el') diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index bb1c46c070a..eeb32f8fe50 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -969,11 +969,11 @@ If the OLD prefix arg is passed, tell the file NAME of the old file." (list (match-string 1))) header-files ;; this assumes that there are no spaces in filenames - (when (re-search-backward - "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" - nil t) - (list (if old (match-string 2) (match-string 4)) - (if old (match-string 4) (match-string 2))))))))) + (and (re-search-backward "^diff " nil t) + (looking-at + "^diff \\(-[^ \t\nL]+ +\\)*\\(-L +\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?") + (list (if old (match-string 3) (match-string 5)) + (if old (match-string 4) (match-string 3))))))))) (defun diff-find-file-name (&optional old noprompt prefix) "Return the file corresponding to the current patch. diff --git a/test/lisp/vc/diff-mode-tests.el b/test/lisp/vc/diff-mode-tests.el index 5bc4ad6dace..521865906e7 100644 --- a/test/lisp/vc/diff-mode-tests.el +++ b/test/lisp/vc/diff-mode-tests.el @@ -468,4 +468,16 @@ baz")))) (114 131 (diff-mode syntax face font-lock-string-face)) (134 140 (diff-mode syntax face font-lock-keyword-face)))))))) +(ert-deftest test-hunk-file-names () + (with-temp-buffer + (insert "diff -c /tmp/ange-ftp13518wvE.el /tmp/ange-ftp1351895K.el\n") + (goto-char (point-min)) + (should (equal (diff-hunk-file-names) + '("/tmp/ange-ftp1351895K.el" "/tmp/ange-ftp13518wvE.el")))) + (with-temp-buffer + (insert "diff -c -L /ftp\:slbhao\:/home/albinus/src/tramp/lisp/tramp.el -L /ftp\:slbhao\:/home/albinus/src/emacs/lisp/net/tramp.el /tmp/ange-ftp13518wvE.el /tmp/ange-ftp1351895K.el\n") + (goto-char (point-min)) + (should (equal (diff-hunk-file-names) + '("/tmp/ange-ftp1351895K.el" "/tmp/ange-ftp13518wvE.el"))))) + (provide 'diff-mode-tests) -- cgit v1.2.3 From 343dc9d41524cebfc88511a88df3969dbd766022 Mon Sep 17 00:00:00 2001 From: Mattias EngdegÄrd Date: Mon, 16 Aug 2021 11:37:19 +0200 Subject: ; * test/lisp/vc/diff-mode-tests.el: Remove useless \ --- test/lisp/vc/diff-mode-tests.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/lisp/vc/diff-mode-tests.el') diff --git a/test/lisp/vc/diff-mode-tests.el b/test/lisp/vc/diff-mode-tests.el index 521865906e7..fefe50d5173 100644 --- a/test/lisp/vc/diff-mode-tests.el +++ b/test/lisp/vc/diff-mode-tests.el @@ -475,7 +475,7 @@ baz")))) (should (equal (diff-hunk-file-names) '("/tmp/ange-ftp1351895K.el" "/tmp/ange-ftp13518wvE.el")))) (with-temp-buffer - (insert "diff -c -L /ftp\:slbhao\:/home/albinus/src/tramp/lisp/tramp.el -L /ftp\:slbhao\:/home/albinus/src/emacs/lisp/net/tramp.el /tmp/ange-ftp13518wvE.el /tmp/ange-ftp1351895K.el\n") + (insert "diff -c -L /ftp:slbhao:/home/albinus/src/tramp/lisp/tramp.el -L /ftp:slbhao:/home/albinus/src/emacs/lisp/net/tramp.el /tmp/ange-ftp13518wvE.el /tmp/ange-ftp1351895K.el\n") (goto-char (point-min)) (should (equal (diff-hunk-file-names) '("/tmp/ange-ftp1351895K.el" "/tmp/ange-ftp13518wvE.el"))))) -- cgit v1.2.3