R/check-wd.R

Defines functions test_file_exists check_wd

Documented in check_wd

#' Check whether a file exists
#' 
#' @param path Path to the file you want to check
#' @param missing_msg Custom feedback message in case the file is missing
#' @param state the state to start from
#' 
#' @examples
#' \dontrun{
#' # Example 1 solution code:
#' # write("hello", file = "test.txt")
#' 
#' # SCT
#' ex() %>% check_wd("test.txt")
#' }
#' @export
check_wd <- function(state, path, missing_msg = NULL) {
  assert_state(state)
  file_state <- FileState$new(state)
  file_state$add_details(type = 'file',
                         case = 'available',
                         file = basename(path),
                         folder = dirname(path),
                         message = missing_msg)
  check_that(is_true(file.exists(path)), feedback = file_state$details)
  return(state)
}

test_file_exists <- function(path, incorrect_msg = NULL) {
  fail_if_v2_only()
  ex() %>% check_wd(path = path, missing_msg = incorrect_msg)
}
datacamp/testwhat documentation built on June 26, 2022, 9:07 a.m.