Skip to content

Commit 8151d52

Browse files
brubaryantar92
authored andcommitted
ob-shell: Use `process-file' when stdin or cmdline
lisp/ob-shell.el (org-babel-sh-evaluate): Use `process-file' (instead of `call-process-shell-command') so that `org-babel-sh-evaluate' will invoke file name handlers based on `default-directory', if needed, like when using a remote directory. lisp/org-compat.el (with-connection-local-variables): New compatibility macro. testing/lisp/test-ob-shell.el (ob-shell/remote-with-stdin-or-cmdline): New test. testing/org-test.el (org-test-with-tramp-remote-dir): New macro. Fixes https://list.orgmode.org/CKMOBWBK709F.1RUN69SRWB64U@laptop/.
1 parent eeb4fa8 commit 8151d52

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

lisp/ob-shell.el

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,16 @@ return the value of the last statement in BODY."
283283
(set-file-modes script-file #o755)
284284
(with-temp-file stdin-file (insert (or stdin "")))
285285
(with-temp-buffer
286-
(call-process-shell-command
287-
(concat (if shebang script-file
288-
(format "%s %s" shell-file-name script-file))
289-
(and cmdline (concat " " cmdline)))
290-
stdin-file
291-
(current-buffer))
286+
(with-connection-local-variables
287+
(apply #'process-file
288+
(if shebang (file-local-name script-file)
289+
shell-file-name)
290+
stdin-file
291+
(current-buffer)
292+
nil
293+
(if shebang (when cmdline (list cmdline))
294+
(list shell-command-switch
295+
(concat (file-local-name script-file) " " cmdline)))))
292296
(buffer-string))))
293297
(session ; session evaluation
294298
(mapconcat

lisp/org-compat.el

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ extension beyond end of line was not controllable."
224224
(define-obsolete-function-alias 'org-babel-edit-distance 'org-string-distance
225225
"9.5")
226226

227+
(unless (fboundp 'with-connection-local-variables)
228+
;; Added in Emacs 27: commit:21f54feee8, 2019-03-09.
229+
;; Redefining it using the old function `with-connection-local-profiles'.
230+
(defmacro with-connection-local-variables (&rest body)
231+
"Apply connection-local variables according to `default-directory'.
232+
Execute BODY, and unwind connection-local variables."
233+
(declare (debug t))
234+
`(with-connection-local-profiles (connection-local-get-profiles)
235+
,@body)))
236+
227237

228238
;;; Emacs < 26.1 compatibility
229239

testing/lisp/test-ob-shell.el

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,55 @@ ob-comint.el, which was not previously tested."
106106
"#+BEGIN_SRC sh :results output :var l='(1 2)\necho ${l}\n#+END_SRC"
107107
(org-trim (org-babel-execute-src-block))))))
108108

109+
(ert-deftest ob-shell/remote-with-stdin-or-cmdline ()
110+
"Test :stdin and :cmdline with a remote directory."
111+
;; We assume `default-directory' is a local directory.
112+
(skip-unless (not (memq system-type '(ms-dos windows-nt))))
113+
(org-test-with-tramp-remote-dir remote-dir
114+
(dolist (spec `( ()
115+
(:dir ,remote-dir)
116+
(:dir ,remote-dir :cmdline t)
117+
(:dir ,remote-dir :stdin t)
118+
(:dir ,remote-dir :cmdline t :shebang t)
119+
(:dir ,remote-dir :stdin t :shebang t)
120+
(:dir ,remote-dir :cmdline t :stdin t :shebang t)
121+
(:cmdline t)
122+
(:stdin t)
123+
(:cmdline t :shebang t)
124+
(:stdin t :shebang t)
125+
(:cmdline t :stdin t :shebang t)))
126+
(let ((default-directory (or (plist-get spec :dir) default-directory))
127+
(org-confirm-babel-evaluate nil)
128+
(params-line "")
129+
(who-line " export who=tramp")
130+
(args-line " echo ARGS: --verbose 23 71"))
131+
(when-let ((dir (plist-get spec :dir)))
132+
(setq params-line (concat params-line " " ":dir " dir)))
133+
(when (plist-get spec :stdin)
134+
(setq who-line " read -r who")
135+
(setq params-line (concat params-line " :stdin input")))
136+
(when (plist-get spec :cmdline)
137+
(setq args-line " echo \"ARGS: $*\"")
138+
(setq params-line (concat params-line " :cmdline \"--verbose 23 71\"")))
139+
(when (plist-get spec :shebang)
140+
(setq params-line (concat params-line " :shebang \"#!/bin/sh\"")))
141+
(let* ((result (org-test-with-temp-text
142+
(mapconcat #'identity
143+
(list "#+name: input"
144+
"tramp"
145+
""
146+
(concat "<point>"
147+
"#+begin_src sh :results output " params-line)
148+
args-line
149+
who-line
150+
" echo \"hello $who from $(pwd)/\""
151+
"#+end_src")
152+
"\n")
153+
(org-trim (org-babel-execute-src-block))))
154+
(expected (concat "ARGS: --verbose 23 71"
155+
"\nhello tramp from " (file-local-name default-directory))))
156+
(should (equal result expected)))))))
157+
109158
(provide 'test-ob-shell)
110159

111160
;;; test-ob-shell.el ends here

testing/org-test.el

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,35 @@ setting `pp-escape-newlines' to nil manually."
284284
;; on multiple lines in the ERT results buffer.
285285
(setq pp-escape-newlines back)))))
286286

287+
(defun org-test-with-tramp-remote-dir--worker (body)
288+
"Worker for `org-test-with-tramp-remote-dir'."
289+
(let ((env-def (getenv "REMOTE_TEMPORARY_FILE_DIRECTORY")))
290+
(cond
291+
(env-def (funcall body env-def))
292+
((eq system-type 'windows-nt) (funcall body null-device))
293+
(t (require 'tramp)
294+
(let ((tramp-methods
295+
(cons '("mock"
296+
(tramp-login-program "sh")
297+
(tramp-login-args (("-i")))
298+
(tramp-remote-shell "/bin/sh")
299+
(tramp-remote-shell-args ("-c"))
300+
(tramp-connection-timeout 10))
301+
tramp-methods))
302+
(tramp-default-host-alist
303+
`(("\\`mock\\'" nil ,(system-name)))))
304+
(funcall body (format "/mock::%s" temporary-file-directory)))))))
305+
306+
(defmacro org-test-with-tramp-remote-dir (dir &rest body)
307+
"Bind the symbol DIR to a remote directory and execute BODY.
308+
Return the value of the last form in BODY. The directory DIR
309+
will be something like \"/mock::/tmp/\", which allows to test
310+
Tramp related features. We mostly follow
311+
`tramp-test-temporary-file-directory' from GNU Emacs tests."
312+
(declare (debug (sexp body)) (indent 2))
313+
`(org-test-with-tramp-remote-dir--worker (lambda (,dir) ,@body)))
314+
315+
287316

288317
;;; Navigation Functions
289318

0 commit comments

Comments
 (0)