2017-08-25 46 views
0

我正在构建一个R软件包RE,我想打包一个独立脚本并将其放在RE/exec/wrapper.R中。我已经为测试编写了一个测试函数,它在使用devtools::test("RE")从命令行运行时工作得很好,但在使用devtools::check("RE")运行时失败。如何使用testthat测试作为软件包一部分的独立R脚本

失败

相关的代码部分:

cmd <- "Rscript \"C:\\RE\\exec\\wrapper.R\" base print \"{\\\"x\\\": 2}\"" 
rv <- system(cmd, intern = FALSE, ignore.stderr = FALSE, show.output.on.console = FALSE) 

当为“测试”部分运行一切都正常运行和system回报0,因为它应该。当作为“检查”的一部分运行时,system会给出下面给出的错误消息并返回1

Error in file(filename, "r", encoding = encoding) : cannot open the connection 
Calls: local ... eval.parent -> eval -> eval -> eval -> eval -> source -> file 
In addition: Warning message: 
In file(filename, "r", encoding = encoding) : cannot open file 'startup.Rs': No such file or directory 
Execution halted 

我:

  1. 选中该文件存在(它在这两种情况下)
  2. 试过tryCatch。没有错误被发现,并system刚刚返回1

这让我觉得,这是systemR CMD CHECK的问题。

回答

0

答案在https://github.com/hadley/testthat/issues/144。必须在顶级测试脚本(package_root/tests/testthat.R)中通过Sys.setenv("R_TESTS" = "")取消设置环境变量R_TESTS,否则system在从R CMD CHECK调用时将无法正确运行。我离开了这个问题,因为在绊倒答案之前,我花了几个小时的时间进行试验和搜索。