withCorrectWorkingDir: Evaluate test code in appropriate working directory.

Description Usage Arguments Details Value Assumptions Examples

View source: R/code.R

Description

Wrapping test code with this function ensures that the appropriate working directory is chosen regardless of how test code is executed (see details). This is especially useful when test code itself is still at a development stage, i.e. not completely fit for execution via test_dir or similar testing routines yet.

Usage

1
2
withCorrectWorkingDir(code, pattern = "/tests/testthat$",
  target_wd = "tests/testthat")

Arguments

code

Test code containing expectations

pattern

character. Pattern to determine the working directory scenario.

target_wd

character. Working directory for tests.

Details

In case test code is executed via "stand-alone" calls to either expect_that (or functions calling that function) or test_that instead of a "global" call to test_dir, the working directory is temporarily set to "~/tests/testthat".

Value

Whatever the code returns.

Assumptions

The function assumes that your initial working directory points to a directory containing a valid R package (project) as created via functions like package.skeleton, create or the like.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## Not run: 
# Prepare:
old <- setwd(tempdir())
dir.create("tests/testthat/test_dir", recursive = TRUE)

# Assumes that the following file exists: ~/tests/testthat/test_dir
# Failing test when executed in a "stand-alone" manner:
try(expect_true(file.exists("test_dir")))

# Ensuring the appropriate working directory:
withCorrectWorkingDir(
  expect_true(file.exists("test_dir"))
)

# Clean up:
if (grepl(basename(getwd()), old)) {
  unlink("tests", recursive = TRUE)
}
setwd(old)

## End(Not run)

rappster/testhelper documentation built on May 27, 2019, 2:01 a.m.