[英]Running Rdevtool::test_that on buffer save in ESS
My workflow:我的工作流程:
devtools::test()
重新评估devtools::test()
To me, this seems far too arduous.对我来说,这似乎太难了。 Why can't I get R to run devtools::test()
automatically when I save the buffer?为什么我在保存缓冲区时不能让 R 自动运行devtools::test()
? Please help, my fingers can barely take the strain of the seven extra keystrokes!请帮忙,我的手指几乎无法承受额外七次击键的压力!
This worked for a similar setup (switching to a shell
buffer):这适用于类似的设置(切换到shell
缓冲区):
(defun devtools-test ()
(interactive)
(when (string-match (rx-to-string `(: ".R" eos) t) (buffer-name))
(switch-to-buffer "*R*")
(end-of-buffer)
(insert "devtools::test()")
(comint-send-input)))
(add-hook 'after-save-hook 'devtools-test)
Does that work for you?那对你有用吗?
For completeness, there are easier way to do this in recent versions of ESS.为了完整起见,在最新版本的 ESS 中有更简单的方法来做到这一点。
The function/command ess-r-devtools-test
now runs devtools::test
.函数/命令ess-r-devtools-test
现在运行devtools::test
。
So you could achieve this with (untested):所以你可以用(未经测试)实现这一点:
(add-hook 'ess-r-mode-hook
(lambda ()
(add-hook 'after-save-hook 'devtools-test nil 'local)))
In addition, there are many other useful functions under ess-r-devtools-*
for build, install etc.此外, ess-r-devtools-*
下还有许多其他有用的功能,用于构建、安装等。
It's worth noting that calling ess-r-devtools-test
with the universal argument will filter tests by the current file.值得注意的是,使用通用参数调用ess-r-devtools-test
将按当前文件过滤测试。
So calling Cu Mx ess-r-devtools
from a file my-file.R
will run devtools::test(filter="my-file")
.因此,从文件my-file.R
调用Cu Mx ess-r-devtools
将运行devtools::test(filter="my-file")
。
This can be useful to bear in mind when choosing names for test files, or when rerunning only the current test file.在为测试文件选择名称或仅重新运行当前测试文件时,记住这一点很有用。
There is a ess-eval-linewise
function which you can use.您可以使用一个ess-eval-linewise
函数。
Something like this (not tested):像这样的东西(未测试):
(defun devtools-test ()
(interactive)
(when (and (equal ess-dialect "R")
(string-match "^test.*\\.[Rr]$" (buffer-name)))
(ess-eval-linewise "devtools::test()")))
(add-hook 'after-save-hook 'devtools-test)
I would not recommend this though, as for some packages tests take quite a while to run.不过,我不建议这样做,因为对于某些软件包,测试需要很长时间才能运行。 You don't want them running on every save.您不希望它们在每次保存时都运行。
There will be a dedicated devtools
functionality in ess soon. ess 很快就会有一个专门的devtools
功能。 Follow this issue .按照这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.