emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Need help on my writing org-mode test.
@ 2018-04-08 13:01 stardiviner
  2018-04-08 21:56 ` Nicolas Goaziou
  2018-04-10 10:00 ` stardiviner
  0 siblings, 2 replies; 4+ messages in thread
From: stardiviner @ 2018-04-08 13:01 UTC (permalink / raw)
  To: org-mode


[-- Attachment #1.1: Type: text/plain, Size: 4260 bytes --]

I'm try to write a test for following src block:

#+begin_src sh :results link :file "test.txt"
echo "hello" > test.txt
echo "test"
#+end_src

#+RESULTS[<2018-04-08 18:30:51> 22a787defa349526768722f4bfc8509dd16ab154]:
[[file:test.txt]]

And here is my ert test:

(ert-deftest test-ob/result-file-link-type-header-argument ()
  "Ensure that the result is a link to a file.
The file is just a link to :file value. Inhibit non-empty result write to :file."
  (org-test-with-temp-text
   "
#+begin_src sh :results link :file \"/tmp/test.txt\"
echo \"hello\" > /tmp/test.txt
echo \"test\"
#+end_src"
   (org-babel-next-src-block 1)
   (org-babel-execute-src-block)
   (goto-char (point-min))
   (should (search-forward "[[file:test.txt]]" nil nil))
   (should (with-temp-buffer
   	     (insert-file-contents "/tmp/test.txt")
   	     (string=
   	      "hello\n"
   	      (buffer-substring-no-properties (point-min) (point-max)))))
   ))

I run test with async shell command in Emacs:

make BTEST_RE="test-ob/result-file-link-type-header-argument" test-dirty
   
But I got error:

```
selected tests: test-ob/result-file-link-type-header-argument
Running 1 tests (2018-04-08 20:52:41+0800, selector ‘"test-ob/result-file-link-type-header-argument"’)
executing Sh code block...
Code block evaluation complete.
Test test-ob/result-file-link-type-header-argument backtrace:
  search-forward("[[file:test.txt]]" nil nil nil)
  apply(search-forward ("[[file:test.txt]]" nil nil))
  (setq value-1950 (apply fn-1948 args-1949))
  (unwind-protect (setq value-1950 (apply fn-1948 args-1949)) (setq fo
  (if (unwind-protect (setq value-1950 (apply fn-1948 args-1949)) (set
  (let (form-description-1952) (if (unwind-protect (setq value-1950 (a
  (let ((value-1950 'ert-form-evaluation-aborted-1951)) (let (form-des
  (let* ((fn-1948 (function search-forward)) (args-1949 (condition-cas
  (progn (org-mode) (let ((point (string-match "<point>" inside-text))
  (unwind-protect (progn (org-mode) (let ((point (string-match "<point
  (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn
  (let ((temp-buffer (generate-new-buffer " *temp*"))) (save-current-b
  (let ((inside-text (if (stringp "\n#+begin_src sh :results link :fil
  (lambda nil (let ((inside-text (if (stringp "\n#+begin_src sh :resul
  ert--run-test-internal(#s(ert--test-execution-info :test #s(ert-test
  ert-run-test(#s(ert-test :name test-ob/result-file-link-type-header-
  ert-run-or-rerun-test(#s(ert--stats :selector "test-ob/result-file-l
  ert-run-tests("test-ob/result-file-link-type-header-argument" #f(com
  ert-run-tests-batch("test-ob/result-file-link-type-header-argument")
  ert-run-tests-batch-and-exit("test-ob/result-file-link-type-header-a
  (let ((org-id-track-globally t) (org-test-selector (if org-test-sele
  org-test-run-batch-tests("test-ob/result-file-link-type-header-argum
  eval((org-test-run-batch-tests org-test-select-re))
  command-line-1(("--eval" "(setq vc-handled-backends nil org-startup-
  command-line()
  normal-top-level()
Test test-ob/result-file-link-type-header-argument condition:
    (search-failed "[[file:test.txt]]")
   FAILED  1/1  test-ob/result-file-link-type-header-argument (0.032747 sec)

Ran 1 tests, 0 results as expected, 1 unexpected (2018-04-08 20:52:42+0800, 0.877523 sec)

1 unexpected results:
   FAILED  test-ob/result-file-link-type-header-argument

make: *** [mk/targets.mk:105: test-dirty] Error 1
```

I tried many modification, I write this test based on existing test:

(ert-deftest test-ob/file-desc-header-argument ()
  "Test that the :file-desc header argument is used."
  (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  \"foo\"
#+end_src

#+begin_src emacs-lisp :results file :file-desc
  \"foo\"
#+end_src"
    (org-babel-execute-src-block)
    (org-babel-next-src-block 1)
    (org-babel-execute-src-block)
    (goto-char (point-min))
    (should (search-forward "[[file:foo][bar]]" nil t))
    (should (search-forward "[[file:foo][foo]]" nil t))))

Don't know why my test error. Can't find out what's wrong from truncated make test output.

I included my patch in attachment.


[-- Attachment #1.2: 0001-ob-core.el-add-results-link-type-result-for-babel.patch --]
[-- Type: text/x-patch, Size: 4080 bytes --]

From 51ed1e26852a0e7a0ef8fac4846c0f60ffcf4494 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
Date: Sun, 8 Apr 2018 20:56:28 +0800
Subject: [PATCH] ob-core.el: add :results link type result for babel.

* lisp/ob-core.el (org-babel-execute-src-block): handle :results link
  type. Like :results graphics, don't write result to :file specified file.

* etc/ORG-NEWS: declare new :results type "link".

* testing/lisp/test-ob.el (test-ob/result-file-link-type-header-argument):
  Add test for new added :results type "link".
---
 etc/ORG-NEWS            |  9 +++++++++
 lisp/ob-core.el         | 12 ++++++------
 testing/lisp/test-ob.el | 20 ++++++++++++++++++++
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 0edd77115..3833b57b2 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -113,6 +113,15 @@ now sort according to the locale’s collation rules instead of by
 code-point.
 
 ** New features
+*** Add ~:results link~ support for org-babel
+This will support only insert file link without writing result to file.
+Like this case:
+#+begin_src shell :dir "data/tmp" :results link :file "crackzor_1.0.c.gz"
+wget -c "http://ben.akrin.com/crackzor/crackzor_1.0.c.gz"
+#+end_src
+
+#+RESULTS:
+[[file:data/tmp/crackzor_1.0.c.gz]]
 *** Add ~:session~ support of ob-js for js-comint
 #+begin_src js :session "*Javascript REPL*"
 console.log("stardiviner")
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 129e17f62..2dce0639e 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -413,7 +413,7 @@ then run `org-babel-switch-to-session'."
     (padline	. ((yes no)))
     (post       . :any)
     (prologue   . :any)
-    (results	. ((file list vector table scalar verbatim)
+    (results	. ((file link list vector table scalar verbatim)
 		   (raw html latex org code pp drawer)
 		   (replace silent none append prepend)
 		   (output value)))
@@ -706,11 +706,11 @@ block."
 	      (let ((file (cdr (assq :file params))))
 		;; If non-empty result and :file then write to :file.
 		(when file
-		  (let ((graphics?
-			 (member "graphics" (cdr (assq :result-params params)))))
-		    ;; Handle :results graphics :file case.  Don't
-		    ;; write result to file if result is graphics.
-		    (when (and result (not graphics?))
+		  (let ((graphics? (member "graphics" (cdr (assq :result-params params))))
+			(file-link? (member "link" (cdr (assq :result-params params)))))
+		    ;; If :results are special types like `link', `graphics' etc.
+		    ;; don't write result to `:file'. Literately only insert link to :file.
+		    (when (and result (not graphics?) (not file-link?))
 		      (with-temp-file file
 			(insert (org-babel-format-result
 				 result (cdr (assq :sep params)))))))
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index a4a590d6a..c1f5f3517 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -996,6 +996,26 @@ trying to find the :END: marker."
     (should (search-forward "[[file:foo][bar]]" nil t))
     (should (search-forward "[[file:foo][foo]]" nil t))))
 
+(ert-deftest test-ob/result-file-link-type-header-argument ()
+  "Ensure that the result is a link to a file.
+The file is just a link to :file value. Inhibit non-empty result write to :file."
+  (org-test-with-temp-text
+   "
+#+begin_src sh :results link :file \"/tmp/test.txt\"
+echo \"hello\" > /tmp/test.txt
+echo \"test\"
+#+end_src"
+   (org-babel-next-src-block 1)
+   (org-babel-execute-src-block)
+   (goto-char (point-min))
+   (should (search-forward "[[file:test.txt]]" nil nil))
+   (should (with-temp-buffer
+   	     (insert-file-contents "/tmp/test.txt")
+   	     (string=
+   	      "hello\n"
+   	      (buffer-substring-no-properties (point-min) (point-max)))))
+   ))
+
 (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
   (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\"  }")
 	(org-babel-inline-result-wrap "=%s="))
-- 
2.17.0


[-- Attachment #1.3: Type: text/plain, Size: 202 bytes --]


-- 
[ stardiviner ] don't need to convince with trends.
       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3
      

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: Need help on my writing org-mode test.
  2018-04-08 13:01 Need help on my writing org-mode test stardiviner
@ 2018-04-08 21:56 ` Nicolas Goaziou
  2018-04-10 10:00 ` stardiviner
  1 sibling, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2018-04-08 21:56 UTC (permalink / raw)
  To: stardiviner; +Cc: org-mode

Hello,

stardiviner <numbchild@gmail.com> writes:

> I'm try to write a test for following src block:
>
> #+begin_src sh :results link :file "test.txt"
> echo "hello" > test.txt
> echo "test"
> #+end_src
>
>
> #+RESULTS[<2018-04-08 18:30:51> 22a787defa349526768722f4bfc8509dd16ab154]:
> [[file:test.txt]]
>
> And here is my ert test:
>
> (ert-deftest test-ob/result-file-link-type-header-argument ()
>   "Ensure that the result is a link to a file.
> The file is just a link to :file value. Inhibit non-empty result write to :file."
>   (org-test-with-temp-text
>    "
>
> #+begin_src sh :results link :file \"/tmp/test.txt\"
> echo \"hello\" > /tmp/test.txt
> echo \"test\"
> #+end_src"
>
>    (org-babel-next-src-block 1)
>    (org-babel-execute-src-block)
>    (goto-char (point-min))
>    (should (search-forward "[[file:test.txt]]" nil nil))
>    (should (with-temp-buffer
>    	     (insert-file-contents "/tmp/test.txt")
>    	     (string=
>    	      "hello\n"
>    	      (buffer-substring-no-properties (point-min) (point-max)))))
>    ))

I would check the result of

    (let ((output-file (make-temp-file "org-test")))
        (org-test-with-temp-text
            (format "
    #+begin_src emacs-lisp :results link :file %S<point>
      (with-temp-file %S (insert \"foo\"))
    #+end_src"
                    output-file
                    output-file)
          (org-babel-execute-src-block)
          (goto-char (point-max))
          (buffer-substring-no-properties
           (line-beginning-position 0)
           (line-end-position 0))))

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Need help on my writing org-mode test.
  2018-04-08 13:01 Need help on my writing org-mode test stardiviner
  2018-04-08 21:56 ` Nicolas Goaziou
@ 2018-04-10 10:00 ` stardiviner
  2018-04-23 10:29   ` Nicolas Goaziou
  1 sibling, 1 reply; 4+ messages in thread
From: stardiviner @ 2018-04-10 10:00 UTC (permalink / raw)
  To: org-mode, Nicolas Goaziou

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

After long time Edebug and many print info try.
Unbelievable that this is caused by my ert test is using:

#+begin_src sh

(which I copy from other existing testing example.)
I remember this "sh" is deprecated.
After I change to "shell", test passed.

@Nicolas, does current existing testing should be updated to #+begin_src
shell? Even though they all test passed currently.

- --
[ stardiviner ] don't need to convince with trends.
       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3
-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAlrMiz8ACgkQG13xyVro
msNyKAf+M8h7amXbvHnGBQ9zJ9AXx9Jj+rdPcS9Pv95RBvo7NYyTIaHYip808SPt
dPxqZLE9SPjGlPWCdfgSwmq4+zmx6kcXDTYVwAQGiw+CqsPwJtMdvDLccLX/Nsjh
/Wk2dnsJ/J0xpwX/gBfGY3I08GOOI9L5MX/ZLmKYbt0Vavuz5wOK00ejMZ1Vd0/m
G1QFRpEgB5t5Zx5Mncjh1ZmzLv++7kWajG0pt33SbdBloZB/aXEjtpSrHELCGzfN
V9jDBEdAQHfGVsd72e2lJEgDvA7oGZQmdK7Nuc50iy9USSor5cg0MRSIL/MIExDE
8QuAZAg27izVQhNXVoF/t9lZpSzIvA==
=6Mc1
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Need help on my writing org-mode test.
  2018-04-10 10:00 ` stardiviner
@ 2018-04-23 10:29   ` Nicolas Goaziou
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2018-04-23 10:29 UTC (permalink / raw)
  To: stardiviner; +Cc: org-mode

Hello,

stardiviner <numbchild@gmail.com> writes:

> @Nicolas, does current existing testing should be updated to #+begin_src
> shell? Even though they all test passed currently.

I don't think so. "begin_src sh" and "begin_src shell" are not
equivalent, e.g., sometimes, "begin_src shell" could be "begin_src
bash". IOW, some tests might break if we changed "sh" into "shell".

Regards,

-- 
Nicolas Goaziou                                                0x80A93738

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-04-23 10:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-08 13:01 Need help on my writing org-mode test stardiviner
2018-04-08 21:56 ` Nicolas Goaziou
2018-04-10 10:00 ` stardiviner
2018-04-23 10:29   ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).