Objects of the root_criterion class decide if a
given directory is a project root.
Usage
root_criterion(testfun, desc, subdir = NULL)
is_root_criterion(x)
as_root_criterion(x)
# S3 method for class 'character'
as_root_criterion(x)
# S3 method for class 'root_criterion'
as_root_criterion(x)
# S3 method for class 'root_criterion'
x | y
has_file(filepath, contents = NULL, n = -1L, fixed = FALSE)
has_dir(filepath)
has_file_pattern(pattern, contents = NULL, n = -1L, fixed = FALSE)
has_basename(basename, subdir = NULL)Arguments
- testfun
[function|list(function)]
A function with one parameter that returnsTRUEif the directory specified by this parameter is the project root, andFALSEotherwise. Can also be a list of such functions.- desc
[character]
A textual description of the test criterion, of the same length astestfun.- subdir
[character]
If given, the criterion will also be tested in the subdirectories defined by this argument, in the order given. The first existing directory will be used as a starting point. This is used for the is_testthat criterion that needs to descend intotests/testthatif starting at the package root, but stay insidetests/testthatif called from a testthat test.- x
[object]
An object.- y
[object]
An object.- filepath
[character(1)]
File path (can contain directories).- contents, fixed
[character(1)]
IfcontentsisNULL(the default), file contents are not checked. Otherwise,contentsis a regular expression (iffixedisFALSE) or a search string (iffixedisTRUE), and file contents are checked matching lines.- n
[integerish(1)]
Maximum number of lines to read to check file contents.- pattern
[character(1)]
Regular expression to match the file name against.- basename
[character(1)]
The required name of the root directory.
Value
An S3 object of class root_criterion with the following members:
testfunThe
testfunargumentdescThe
descargumentsubdirThe
subdirargumentfind_fileA function with
...andpatharguments that returns a path relative to the root, as specified by this criterion. The optionalpathargument specifies the starting directory, which defaults to".". The function forwards tofind_root_file(), which passes...directly tofile.path()if the first argument is an absolute path.make_fix_fileA function with a
pathargument that returns a function that finds paths relative to the root. For a criterioncr, the result ofcr$make_fix_file(".")(...)is identical tocr$find_file(...). The function created bymake_fix_file()can be saved to a variable to be more independent of the current working directory.
Details
Construct criteria using root_criterion in a very general fashion
by specifying a function with a path argument, and a description.
The as_root_criterion() function accepts objects of class
root_criterion, and character values; the latter will be
converted to criteria using has_file.
Root criteria can be combined with the | operator. The result is a
composite root criterion that requires either of the original criteria to
match.
The has_file() function constructs a criterion that checks for the
existence of a specific file (which itself can be in a subdirectory of the
root) with specific contents.
The has_dir() function constructs a criterion that checks for the
existence of a specific directory.
The has_file_pattern() function constructs a criterion that checks for the
existence of a file that matches a pattern, with specific contents.
The has_basename() function constructs a criterion that checks if the
base::basename() of the root directory has a specific name,
with support for case-insensitive file systems.
Examples
root_criterion(function(path) file.exists(file.path(path, "somefile")), "has somefile")
#> Root criterion: has somefile
has_file("DESCRIPTION")
#> Root criterion: contains a file 'DESCRIPTION'
is_r_package
#> Root criterion: contains a file 'DESCRIPTION' with contents matching '^Package: '
if (FALSE) { # \dontrun{
is_r_package$find_file
is_r_package$make_fix_file(".")
} # }