Browse Source

ob-tangle: Fix tangling order

* lisp/ob-tangle.el (org-babel-tangle-collect-blocks): Preserver order
  of code blocks from the source document.
* testing/lisp/test-ob-tangle.el (ob-tangle/block-order): New test.

Reported-by: Kaushal Modi <kaushal.modi@gmail.com>
<http://lists.gnu.org/r/emacs-orgmode/2018-10/msg00062.html>
Nicolas Goaziou 1 year ago
parent
commit
2fda33bfef
2 changed files with 47 additions and 1 deletions
  1. 2 1
      lisp/ob-tangle.el
  2. 45 0
      testing/lisp/test-ob-tangle.el

+ 2 - 1
lisp/ob-tangle.el

@@ -407,7 +407,8 @@ can be used to limit the collected code blocks by target file."
 	      (if by-lang (setcdr by-lang (cons block (cdr by-lang)))
 		(push (cons src-lang (list block)) blocks)))))))
     ;; Ensure blocks are in the correct order.
-    (mapcar (lambda (b) (cons (car b) (nreverse (cdr b)))) blocks)))
+    (mapcar (lambda (b) (cons (car b) (nreverse (cdr b))))
+	    (nreverse blocks))))
 
 (defun org-babel-tangle-single-block (block-counter &optional only-this-block)
   "Collect the tangled source for current block.

+ 45 - 0
testing/lisp/test-ob-tangle.el

@@ -222,6 +222,51 @@ another block
                                    (buffer-string)))
         (delete-file "test-ob-tangle.org"))))))
 
+(ert-deftest ob-tangle/block-order ()
+  "Test order of tangled blocks."
+  ;; Order per language.
+  (should
+   (equal '("1" "2")
+	  (let ((file (make-temp-file "org-tangle-")))
+	    (unwind-protect
+		(progn
+		  (org-test-with-temp-text-in-file
+		      (format "#+property: header-args :tangle %S
+#+begin_src emacs-lisp
+1
+#+end_src
+
+#+begin_src emacs-lisp
+2
+#+end_src"
+			      file)
+		    (org-babel-tangle))
+		  (with-temp-buffer
+		    (insert-file-contents file)
+		    (org-split-string (buffer-string))))
+	      (delete-file file)))))
+  ;; Order per source block.
+  (should
+   (equal '("1" "2")
+	  (let ((file (make-temp-file "org-tangle-")))
+	    (unwind-protect
+		(progn
+		  (org-test-with-temp-text-in-file
+		      (format "#+property: header-args :tangle %S
+#+begin_src foo
+1
+#+end_src
+
+#+begin_src bar
+2
+#+end_src"
+			      file)
+		    (org-babel-tangle))
+		  (with-temp-buffer
+		    (insert-file-contents file)
+		    (org-split-string (buffer-string))))
+	      (delete-file file))))))
+
 (provide 'test-ob-tangle)
 
 ;;; test-ob-tangle.el ends here