File: checkFileExists.R

package info (click to toggle)
r-cran-checkmate 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,512 kB
  • sloc: ansic: 2,211; sh: 9; makefile: 8
file content (97 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#' Check existence and access rights of files
#'
#' @note
#' The functions without the suffix \dQuote{exists} are deprecated and will be removed
#' from the package in a future version due to name clashes.
#' \code{test_file} has been unexported already.
#'
#' @templateVar fn FileExists
#' @template x
#' @inheritParams checkAccess
#' @param extension [\code{character}]\cr
#'  Vector of allowed file extensions, matched case insensitive.
#' @template checker
#' @family filesystem
#' @export
#' @examples
#' # Check if R's COPYING file is readable
#' testFileExists(file.path(R.home(), "COPYING"), access = "r")
#'
#' # Check if R's COPYING file is readable and writable
#' testFileExists(file.path(R.home(), "COPYING"), access = "rw")
checkFileExists = function(x, access = "", extension = NULL) {
  if (!qtest(x, "S+"))
    return("No file provided")

  w = wf(dir.exists(x))
  if (length(w) > 0L)
    return(sprintf("File expected, but directory in place: '%s'", x[w]))

  w = wf(!file.exists(x))
  if (length(w) > 0L)
    return(sprintf("File does not exist: '%s'", x[w]))

  checkAccess(x, access) %and% checkFileExtension(x, extension)
}

checkFileExtension = function(x, extension = NULL) {
  if (!is.null(extension)) {
    qassert(extension, "S+")
    ii = Reduce(`|`, lapply(tolower(extension), endsWith, x = tolower(x)))
    if (!all(ii))
      return(sprintf(
        "File extension must be in {'%s'} (case insensitive), but file name is '%s'",
        paste0(extension, collapse = "','"), x[wf(!ii)]
      ))
  }
  return(TRUE)
}

#' @export
#' @rdname checkFileExists
check_file_exists = checkFileExists

#' @export
#' @include makeAssertion.R
#' @template assert
#' @rdname checkFileExists
assertFileExists = makeAssertionFunction(checkFileExists, use.namespace = FALSE)

#' @export
#' @rdname checkFileExists
assert_file_exists = assertFileExists

#' @export
#' @include makeTest.R
#' @rdname checkFileExists
testFileExists = makeTestFunction(checkFileExists)

#' @export
#' @rdname checkFileExists
test_file_exists = testFileExists

#' @export
#' @include makeExpectation.R
#' @template expect
#' @rdname checkFileExists
expect_file_exists = makeExpectationFunction(checkFileExists, use.namespace = FALSE)

#' @export
#' @rdname checkFileExists
checkFile = checkFileExists

#' @export
#' @rdname checkFileExists
assertFile = assertFileExists

#' @export
#' @rdname checkFileExists
assert_file = assert_file_exists

#' @export
#' @rdname checkFileExists
testFile = testFileExists

#' @export
#' @rdname checkFileExists
expect_file = expect_file_exists