'Trying to test non-package R file, source() fails

I've got a (mainly Python) workflow that uses some (likely woefully Pythonesque) R scripts for visualization purposes and wanted to set up testing for said R scripts. Now, I've worked out that I should still be able to use testthat without setting everything up as a package; however, unlike the answer there, I set my code up in a nested structure:

project_root
   |
   Rscripts
      +single_script.R
      |
      +tests
         |
         +test_single_script.R
         +run_tests.R

To handle that structure, I tried using here and set things up in a way that to my understanding should be analogous to what's described in that answer:

  • in test_single_script.R:
library(here)

here::i_am("Rscripts/tests/test_single_script.R")

path.to.single.script <- here("Rscripts", "single_script.R")
source(path.to.single.script)
  • in run_tests.R:
library(here)
library(testthat)

here::i_am("Rscripts/tests/run_tests.R")

test.single.script <- here("Rscripts", "tests", "test_single_script.R")

test_file(test.single.script)

It looks like I get the absolute path of single_script.R (print(path.to.single.script) returns the absolute path anyway). However, when trying to run run_tests.R, I get the following error:

══ Testing test_single_script.R ═══════════════════════════════════════════
[ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]

── Error (test_single_script.R:8:1): (code run outside of `test_that()`) ───────────────────────────
Error in `if (length(res) == 0 || res == -1) {
    return(list(path = path, extension = ""))
}`: missing value where TRUE/FALSE needed
Backtrace:
  1. base::source(path.to.single.script) test_single_script.R:8:0
 10. vroom:::split_path_ext(basename(path))

[ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]

I've tried running it in tests, in the project root,... with the same result. I've looked up the error and found nothing. I've tried digging in vroom's code to see what it could choke on, but can't find anything.

Am I just missing something embarrassingly basic?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source