Commit 2262432d authored by Andreas Tille's avatar Andreas Tille

New upstream version 1.3-2

parent f0ff916e
Package: rprojroot
Title: Finding Files in Project Subdirectories
Version: 1.2
Version: 1.3-2
Authors@R: person(given = "Kirill", family = "Müller", role = c("aut",
"cre"), email = "krlmlr+r@mailbox.org")
Description: Robust, reliable and flexible paths to files below a
......@@ -9,7 +9,7 @@ Description: Robust, reliable and flexible paths to files below a
regular file.
Depends: R (>= 3.0.0)
Imports: backports
Suggests: testthat, knitr, withr, rmarkdown
Suggests: testthat, mockr, knitr, withr, rmarkdown
VignetteBuilder: knitr
License: GPL-3
LazyData: true
......@@ -17,12 +17,12 @@ Encoding: UTF-8
URL: https://github.com/krlmlr/rprojroot,
https://krlmlr.github.io/rprojroot
BugReports: https://github.com/krlmlr/rprojroot/issues
RoxygenNote: 5.0.1.9000
RoxygenNote: 6.0.1
Collate: 'rrmake.R' 'criterion.R' 'file.R' 'has-file.R' 'root.R'
'rprojroot-package.R' 'shortcut.R'
'rprojroot-package.R' 'shortcut.R' 'thisfile.R'
NeedsCompilation: no
Packaged: 2017-01-16 11:50:23 UTC; muelleki
Packaged: 2018-01-03 13:27:15 UTC; muelleki
Author: Kirill Müller [aut, cre]
Maintainer: Kirill Müller <krlmlr+r@mailbox.org>
Repository: CRAN
Date/Publication: 2017-01-16 14:16:04
Date/Publication: 2018-01-03 15:36:24 UTC
09fc8e111b014442f6569ac39cfff43f *DESCRIPTION
04765b617541cee07297676a210d25d3 *NAMESPACE
2d0469a219c83ed66c4e2695d4ff2d1c *NEWS.md
da5b8d6d5d1ea8a0aa1c4a1cfed314ff *R/criterion.R
3283d4a6f17606ad0193bace804ed012 *R/file.R
1050ecdd048dce13bbf81615de85489f *R/has-file.R
d87a8501549770bd80068475e78671fe *R/root.R
d0b3abe3383fbe1f56462195b14233a2 *DESCRIPTION
df6dc7704eb1557c3f1aa6ab378d7b83 *NAMESPACE
11c6882f2f6e7567029434d2a555608b *NEWS.md
68cbd2cce6bbda071f1279dd350f66fc *R/criterion.R
7144083716912088331ca9488db40c63 *R/file.R
5b7e4b29d509c702d2f625f8d7ac7d17 *R/has-file.R
05b6a4f0f6bd67e4ed61a516033fb677 *R/root.R
0688a81a507c7639e2b2cec0edad2dbf *R/rprojroot-package.R
dd227290335dea57e2a1031a5b5beb35 *R/rrmake.R
4de9a9bbf48372e1505dd1550bee0418 *R/shortcut.R
0415d9530ea3a983814164d735e5db9e *build/vignette.rds
0634afa63b0e9c96760bcdce54655942 *inst/doc/rprojroot.R
7855fbb459b9e137bc458294b20db80c *inst/doc/rprojroot.Rmd
9e32800aeeef65fcddc218dcaa367682 *inst/doc/rprojroot.html
fa8799827f070a4fe1632f606c876f70 *R/thisfile.R
521e2e3e8ce08f8e9186d76a6c8d1324 *build/vignette.rds
9513fbabdcab89737e27a52092b3b66f *inst/doc/rprojroot.R
57960a65eb4df41bfabc23f3e3bb4f5f *inst/doc/rprojroot.Rmd
319f6bd51fe92f18e7310b416db7c21a *inst/doc/rprojroot.html
6a14df38876e8de0b3a24e689aa98cf7 *man/criteria.Rd
df64533edc92ee73830a13070b418d54 *man/find_root.Rd
5accd629c3b4aa22c6a484b2a52ed0a2 *man/find_root_file.Rd
4a89be5e4f191328f2a7e8b6f31a8ba6 *man/root_criterion.Rd
5256c80494573721ce5baa623ba0906d *man/find_root.Rd
3697cb7d1313279aac3b0abdda1f1a28 *man/find_root_file.Rd
96f94ac13cabb29dc4399de5702bbc70 *man/root_criterion.Rd
a7d033b76a25c7abfb137de00b3da294 *man/rprojroot-package.Rd
9fb39ae7ae2d3b958f7eb6623ba04c0e *man/thisfile.Rd
1d802d92f687ffbb11ee26ae68396d94 *tests/testthat.R
3e6320af0b5c1a4ab2bc220759c31553 *tests/testthat/hierarchy/DESCRIPTION
d41d8cd98f00b204e9800998ecf8427e *tests/testthat/hierarchy/a/b/a
......@@ -29,10 +31,15 @@ d41d8cd98f00b204e9800998ecf8427e *tests/testthat/hierarchy/c
d41d8cd98f00b204e9800998ecf8427e *tests/testthat/package/DESCRIPTION
d41d8cd98f00b204e9800998ecf8427e *tests/testthat/package/tests/testthat.R
d41d8cd98f00b204e9800998ecf8427e *tests/testthat/package/tests/testthat/test-something.R
06e74a052aa3a2ab9a51a5135802a55b *tests/testthat/test-criterion.R
f66f033a6ae6f3edfbc2b234a70031da *tests/testthat/scripts/thisfile-cat.R
1842225c060e51ad4fd7a44cf6dc2c68 *tests/testthat/scripts/thisfile.R
b4eea69f30e5e903f792b50357b04e40 *tests/testthat/scripts/thisfile.Rmd
d41d8cd98f00b204e9800998ecf8427e *tests/testthat/startup.Rs
42d75002ec598f586b253378d63092d8 *tests/testthat/test-criterion.R
ce3ca96d5d38e1c0972d639a81f659c1 *tests/testthat/test-make.R
006eb32f50098d057c0a9cfb6ac4d0ba *tests/testthat/test-root.R
8bfd90319b3d590a0fdb99edbdefa703 *tests/testthat/test-root.R
ec33c4973b9fac9b680322d60d6807ac *tests/testthat/test-testthat.R
e2b585b214585bd7391e0b94fcd7eb3a *tests/testthat/test-thisfile.R
d8879d25ee21b0f84992020d95500aaf *tests/testthat/vcs/git.zip
28e9555632b66c264d026cd065dda0db *tests/testthat/vcs/svn.zip
7855fbb459b9e137bc458294b20db80c *vignettes/rprojroot.Rmd
57960a65eb4df41bfabc23f3e3bb4f5f *vignettes/rprojroot.Rmd
......@@ -31,5 +31,11 @@ export(is_svn_root)
export(is_testthat)
export(is_vcs_root)
export(root_criterion)
export(thisfile)
export(thisfile_knit)
export(thisfile_r)
export(thisfile_rscript)
export(thisfile_source)
import(backports)
importFrom(utils,str)
importFrom(utils,tail)
## rprojroot 1.3-2 (2017-12-22)
- Availability of suggested packages knitr and rmarkdown, and pandoc, is now checked before running the corresponding tests.
# rprojroot 1.3-1 (2017-12-18)
- Adapt to testthat 2.0.0.
- New `thisfile()`, moved from kimisc (#8).
- Add more examples to vignette (#26, @BarkleyBG).
- Detect `.git` directories created with `git clone --separate-git-dir=...` (#24, @karldw).
# rprojroot 1.2 (2017-01-15)
- New root criteria
......
......@@ -101,7 +101,7 @@ is.root_criterion <- function(x) {
as.root_criterion <- function(x) UseMethod("as.root_criterion", x)
#' @details
#' The `as.root_criterion` function accepts objects of class
#' The `as.root_criterion()` function accepts objects of class
#' `root_criterion`, and character values; the latter will be
#' converted to criteria using `has_file`.
#'
......
......@@ -3,7 +3,7 @@
#' Append an arbitrary number of path components to the root using
#' [base::file.path()].
#'
#' The `find_root_file` function is a simple wrapper around
#' The `find_root_file()` function is a simple wrapper around
#' [find_root()] that
#' appends an arbitrary number of path components to the root using
#' [base::file.path()].
......
......@@ -3,7 +3,7 @@ format_lines <- function(n) {
}
#' @details
#' The `has_file` function constructs a criterion that checks for the
#' 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.
#'
......@@ -37,7 +37,7 @@ has_file <- function(filepath, contents = NULL, n = -1L) {
}
#' @details
#' The `has_dir` function constructs a criterion that checks for the
#' The `has_dir()` function constructs a criterion that checks for the
#' existence of a specific directory.
#'
#' @rdname root_criterion
......@@ -58,7 +58,7 @@ has_dir <- function(filepath) {
}
#' @details
#' The `has_file_pattern` function constructs a criterion that checks for the
#' The `has_file_pattern()` function constructs a criterion that checks for the
#' existence of a file that matches a pattern, with specific contents.
#'
#' @rdname root_criterion
......@@ -92,7 +92,7 @@ has_file_pattern <- function(pattern, contents = NULL, n = -1L) {
}
#' @details
#' The `has_dirname` function constructs a criterion that checks if the
#' The `has_dirname()` function constructs a criterion that checks if the
#' [base::dirname()] has a specific name.
#'
#' @rdname root_criterion
......@@ -123,7 +123,7 @@ is_remake_project <- has_file("remake.yml")
is_projectile_project <- has_file(".projectile")
#' @export
is_git_root <- has_dir(".git")
is_git_root <- has_dir(".git") | has_file(".git", contents = "^gitdir: ")
#' @export
is_svn_root <- has_dir(".svn")
......
......@@ -5,7 +5,7 @@
#' The search for a root starts at a given directory (the working directory
#' by default), and proceeds up the directory hierarchy.
#'
#' Starting from the working directory, the `find_root` function searches
#' Starting from the working directory, the `find_root()` function searches
#' for the root.
#' If a root is found, the `...` arguments are used to construct a path;
#' thus, if no extra arguments are given, the root is returned.
......
#' @title Determines the path of the currently running script
#' @description \R does not store nor export the path of the currently running
#' script. This is an attempt to circumvent this limitation by applying
#' heuristics (such as call stack and argument inspection) that work in many
#' cases.
#' **CAVEAT**: Use this function only if your workflow does not permit other
#' solution: if a script needs to know its location, it should be set outside
#' the context of the script if possible.
#'
#' @details This functions currently work only if the script was `source`d,
#' processed with `knitr`,
#' or run with `Rscript` or using the `--file` parameter to the
#' `R` executable. For code run with `Rscript`, the exact value
#' of the parameter passed to `Rscript` is returned.
#' @return The path of the currently running script, NULL if it cannot be
#' determined.
#' @seealso [base::source()], [utils::Rscript()], [base::getwd()]
#' @references [http://stackoverflow.com/q/1815606/946850]()
#' @author Kirill Müller, Hadley Wickham, Michael R. Head
#' @examples
#' \dontrun{thisfile()}
#' @export
thisfile <- function() {
if (!is.null(res <- thisfile_source())) res
else if (!is.null(res <- thisfile_r())) res
else if (!is.null(res <- thisfile_rscript())) res
else if (!is.null(res <- thisfile_knit())) res
else NULL
}
#' @rdname thisfile
#' @export
thisfile_source <- function() {
for (i in -(1:sys.nframe())) {
if (identical(args(sys.function(i)), args(base::source)))
return (normalizePath(sys.frame(i)$ofile))
}
NULL
}
#' @rdname thisfile
#' @importFrom utils tail
#' @export
thisfile_r <- function() {
cmd_args <- commandArgs(trailingOnly = FALSE)
if (!grepl("^R(?:|term)(?:|[.]exe)$", basename(cmd_args[[1L]]), ignore.case = TRUE))
return (NULL)
cmd_args_trailing <- commandArgs(trailingOnly = TRUE)
leading_idx <-
seq.int(from=1, length.out=length(cmd_args) - length(cmd_args_trailing))
cmd_args <- cmd_args[leading_idx]
file_idx <- c(which(cmd_args == "-f") + 1, which(grepl("^--file=", cmd_args)))
res <- gsub("^(?:|--file=)(.*)$", "\\1", cmd_args[file_idx])
# If multiple --file arguments are given, R uses the last one
res <- tail(res[res != ""], 1)
if (length(res) > 0)
return (res)
NULL
}
#' @rdname thisfile
#' @importFrom utils tail
#' @export
thisfile_rscript <- function() {
cmd_args <- commandArgs(trailingOnly = FALSE)
if (!grepl("^R(?:term|script)(?:|[.]exe)$", basename(cmd_args[[1L]]), ignore.case = TRUE))
return(NULL)
cmd_args_trailing <- commandArgs(trailingOnly = TRUE)
leading_idx <-
seq.int(from=1, length.out=length(cmd_args) - length(cmd_args_trailing))
cmd_args <- cmd_args[leading_idx]
res <- gsub("^(?:--file=(.*)|.*)$", "\\1", cmd_args)
# If multiple --file arguments are given, R uses the last one
res <- tail(res[res != ""], 1)
if (length(res) > 0)
return (res)
NULL
}
#' @rdname thisfile
#' @export
thisfile_knit <- function() {
if (requireNamespace("knitr")) {
return (knitr::current_input(dir = TRUE))
}
NULL
}
No preview for this file type
......@@ -31,8 +31,36 @@ library(rprojroot)
# List all files and directories below the root
dir(find_root(has_file("DESCRIPTION")))
## ---- eval = FALSE-------------------------------------------------------
# rel_path_from_vignettes <- "../R/rrmake.R"
# rel_path_from_vignettes <- file.path("..", "R", "rrmake.R") ##identical
#
## ---- eval = FALSE-------------------------------------------------------
# rel_path_from_root <- "R/rrmake.R"
# rel_path_from_root <- file.path("R", "rrmake.R") ##identical
## ------------------------------------------------------------------------
has_file("DESCRIPTION")
## ------------------------------------------------------------------------
# Specify a path/to/file relative to the root
rel_path_from_root <- find_root_file("R", "rrmake.R", criterion = has_file("DESCRIPTION"))
## ---- eval = FALSE-------------------------------------------------------
# rel_path_from_testthat <- "../../R/rrmake.R"
## ------------------------------------------------------------------------
# Specify a path/to/file relative to the root
rel_path_from_root <- find_root_file("R", "rrmake.R", criterion = has_file("DESCRIPTION"))
## ------------------------------------------------------------------------
# Specify a path/to/file relative to the root
rel_path_from_root <- find_root_file("R", "rrmake.R", criterion = has_file("DESCRIPTION"))
# Find a file relative to the root
file.exists(find_root_file("R", "root.R", criterion = has_file("DESCRIPTION")))
file.exists(rel_path_from_root)
## ------------------------------------------------------------------------
has_file("DESCRIPTION")
......@@ -92,3 +120,44 @@ is_testthat
## ------------------------------------------------------------------------
dir(is_testthat$find_file("hierarchy", path = is_r_package$find_file()))
## ---- eval = FALSE-------------------------------------------------------
# my_fun_run <- do.call(my_fun, my_args)
#
# testthat::test_that(
# "my_fun() returns expected output",
# testthat::expect_equal(
# my_fun_run,
# expected_output
# )
# )
## ---- eval = FALSE-------------------------------------------------------
# ## saved to tests/testthat/helper.R
# get_my_path <- function(file_name) {
# rprojroot::find_testthat_root_file(
# "testing_data", filename
# )
# }
## ---- eval = FALSE-------------------------------------------------------
# ## Find the correct path with your custom rprojroot helper function
# path_to_my_args_file <- get_my_path("my_args.Rdata")
#
# ## Load the input arguments
# load(file = path_to_my_args_file)
#
# ## Run the function with those arguments
# my_fun_run <- do.call(my_fun,my_args)
#
# ## Load the historical expectation with the helper
# load(file = get_my_path("expected_output.Rdata"))
#
# ## Pass all tests and achieve nirvana
# testthat::test_that(
# "my_fun() returns expected output",
# testthat::expect_equal(
# my_fun_run,
# expected_output
# )
# )
......@@ -9,7 +9,7 @@ vignette: >
%\VignetteEncoding{UTF-8}
---
The `rprojroot` package solves a seemingly trivial but annoying problem
The *rprojroot* package solves a seemingly trivial but annoying problem
that occurs sooner or later
in any largish project:
How to find files in subdirectories?
......@@ -92,11 +92,11 @@ pass that absolute path to `root$make_fix_file()`:
root_file <- root$make_fix_file("C:\\Users\\User Name\\...")
```
Get the path of standalone R scripts or vignettes
using the `thisfile()` function in the `kimisc` package:
As a last resort, you can get the path of standalone R scripts or vignettes
using the `thisfile()` function:
```r
root_file <- root$make_fix_file(dirname(kimisc::thisfile()))
root_file <- root$make_fix_file(dirname(thisfile()))
```
The remainder of this vignette describes implementation details and advanced features.
......@@ -135,14 +135,80 @@ library(rprojroot)
# List all files and directories below the root
dir(find_root(has_file("DESCRIPTION")))
# Find a file relative to the root
file.exists(find_root_file("R", "root.R", criterion = has_file("DESCRIPTION")))
```
Note that the following code produces identical results when building the
vignette *and* when sourcing the chunk in RStudio,
provided that the current working directory is the project root
or anywhere below.
#### Relative paths to a stable root
Here we illustrate the power of *rprojroot* by demonstrating how to access the same file from two different working directories. Let your project be a package called `pkgname` and consider the desired file `rrmake.R` at `pkgname/R/rrmake.R`. First, we show how to access from the `vignettes` directory, and then from the `tests/testthat` directory.
##### Example A: From `vignettes`
When your working directory is `pkgname/vignettes`, you can access the `rrmake.R` file by:
1. Supplying a pathname relative to your working directory. Here's two ways to do that:
```{r, eval = FALSE}
rel_path_from_vignettes <- "../R/rrmake.R"
rel_path_from_vignettes <- file.path("..", "R", "rrmake.R") ##identical
```
2. Supplying a pathname to the file relative from the root of the package, e.g.,
```{r, eval = FALSE}
rel_path_from_root <- "R/rrmake.R"
rel_path_from_root <- file.path("R", "rrmake.R") ##identical
```
This second method requires finding the root of the package, which can be done with the `has_file()` function:
```{r}
has_file("DESCRIPTION")
```
So, using *rprojroot* you can specify the path relative from root in the following manner:
```{r}
# Specify a path/to/file relative to the root
rel_path_from_root <- find_root_file("R", "rrmake.R", criterion = has_file("DESCRIPTION"))
```
##### Example B: From `tests/testthat`
When your working directory is `pkgname/tests/testthat`, you can access the `rrmake.R` file by:
1. Supplying a pathname relative to your working directory.
```{r, eval = FALSE}
rel_path_from_testthat <- "../../R/rrmake.R"
```
Note that this is different than in the previous example! However, the second method is the same...
2. Supplying a pathname to the file relative from the root of the package. With *rprojroot*, this is the exact same as in the previous example.
```{r}
# Specify a path/to/file relative to the root
rel_path_from_root <- find_root_file("R", "rrmake.R", criterion = has_file("DESCRIPTION"))
```
##### Summary of Examples A and B
Since Examples A and B used different working directories, `rel_path_from_vignettes` and `rel_path_from_testthat` were different. This is an issue when trying to re-use the same code. This issue is solved by using *rprojroot*: the function `find_root_file()` finds a file relative from the root, where the root is determined from checking the criterion with `has_file()`.
Note that the follow code produces identical results when building the vignette *and* when sourcing the chunk in RStudio, provided that the current working directory is the project root or anywhere below. So, we can check to make sure that *rprojroot* has succesfully determined the correct path:
```{r}
# Specify a path/to/file relative to the root
rel_path_from_root <- find_root_file("R", "rrmake.R", criterion = has_file("DESCRIPTION"))
# Find a file relative to the root
file.exists(rel_path_from_root)
```
### Criteria
......@@ -251,8 +317,7 @@ withr::with_dir(
The `make_fix_file()` member function also accepts an optional `path` argument,
in case you know your project's root but the current working directory is somewhere outside.
Take a look at the `thisfile()` function in the `kimisc` package for getting
the path to the current script or `knitr` document.
The path to the current script or `knitr` document can be obtained using the `thisfile()` function, but it's much easier and much more robust to just run your scripts with the working directory somewhere below your project root.
## `testthat` files
......@@ -281,10 +346,65 @@ so that it also works when rendering the vignette (*sigh*):
dir(is_testthat$find_file("hierarchy", path = is_r_package$find_file()))
```
### Another example: custom testing utilities
The hassle of using saved data files for testing is made even easier by using *rprojroot* in a utility function. For example, suppose you have a testing file at `tests/testthat/test_my_fun.R` which tests the `my_fun()` function:
```{r, eval = FALSE}
my_fun_run <- do.call(my_fun, my_args)
testthat::test_that(
"my_fun() returns expected output",
testthat::expect_equal(
my_fun_run,
expected_output
)
)
```
There are two pieces of information that you'll need every time `test_my_fun.R` is run: `my_args` and `expected_output`. Typically, these objects are saved to `.Rdata` files and saved to the same subdirectory. For example, you could save them to `my_args.Rdata` and `expected_output.Rdata` under the `tests/testthat/testing_data` subdirectory. And, to find them easily in any contexts, you can use *rprojroot*!
Since all of the data files live in the same subdirectory, you can create a utility function `get_my_path()` that will always look in that directory for these types of files. And, since the *testthat* package will look for and source the `tests/testthat/helper.R` file before running any tests, you can place a `get_my_path()` in this file and use it throughout your tests:
```{r, eval = FALSE}
## saved to tests/testthat/helper.R
get_my_path <- function(file_name) {
rprojroot::find_testthat_root_file(
"testing_data", filename
)
}
```
Now you can ask `get_my_path()` to find your important data files by using the function within your test scripts!
```{r, eval = FALSE}
## Find the correct path with your custom rprojroot helper function
path_to_my_args_file <- get_my_path("my_args.Rdata")
## Load the input arguments
load(file = path_to_my_args_file)
## Run the function with those arguments
my_fun_run <- do.call(my_fun,my_args)
## Load the historical expectation with the helper
load(file = get_my_path("expected_output.Rdata"))
## Pass all tests and achieve nirvana
testthat::test_that(
"my_fun() returns expected output",
testthat::expect_equal(
my_fun_run,
expected_output
)
)
```
For an example in the wild, see the [`test_sheet()` function](https://github.com/tidyverse/readxl/blob/0d9ad4f570f6580ff716e0e9ba5048447048e9f0/tests/testthat/helper.R#L1-L3) in the *readxl* package.
## Summary
The `rprojroot` package allows easy access to files below a project root
The *rprojroot* package allows easy access to files below a project root
if the project root can be identified easily, e.g. if it is the only directory
in the whole hierarchy that contains a specific file.
This is a robust solution for finding files in largish projects
......
This diff is collapsed.
......@@ -30,7 +30,7 @@ for a root path. This is especially useful for composite root criteria
created with \code{\link[=|.root_criterion]{|.root_criterion()}}.
}
\details{
Starting from the working directory, the \code{find_root} function searches
Starting from the working directory, the \code{find_root()} function searches
for the root.
If a root is found, the \code{...} arguments are used to construct a path;
thus, if no extra arguments are given, the root is returned.
......
......@@ -36,7 +36,7 @@ Append an arbitrary number of path components to the root using
\code{\link[base:file.path]{base::file.path()}}.
}
\details{
The \code{find_root_file} function is a simple wrapper around
The \code{find_root_file()} function is a simple wrapper around
\code{\link[=find_root]{find_root()}} that
appends an arbitrary number of path components to the root using
\code{\link[base:file.path]{base::file.path()}}.
......
......@@ -88,7 +88,7 @@ given directory is a project root.
Construct criteria using \code{root_criterion} in a very general fashion
by specifying a function with a \code{path} argument, and a description.
The \code{as.root_criterion} function accepts objects of class
The \code{as.root_criterion()} function accepts objects of class
\code{root_criterion}, and character values; the latter will be
converted to criteria using \code{has_file}.
......@@ -96,17 +96,17 @@ Root criteria can be combined with the \code{|} operator. The result is a
composite root criterion that requires either of the original criteria to
match.
The \code{has_file} function constructs a criterion that checks for the
The \code{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 \code{has_dir} function constructs a criterion that checks for the
The \code{has_dir()} function constructs a criterion that checks for the
existence of a specific directory.
The \code{has_file_pattern} function constructs a criterion that checks for the
The \code{has_file_pattern()} function constructs a criterion that checks for the
existence of a file that matches a pattern, with specific contents.
The \code{has_dirname} function constructs a criterion that checks if the
The \code{has_dirname()} function constructs a criterion that checks if the
\code{\link[base:dirname]{base::dirname()}} has a specific name.
}
\examples{
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/thisfile.R
\name{thisfile}
\alias{thisfile}
\alias{thisfile_source}
\alias{thisfile_r}
\alias{thisfile_rscript}
\alias{thisfile_knit}
\title{Determines the path of the currently running script}
\usage{
thisfile()
thisfile_source()
thisfile_r()
thisfile_rscript()
thisfile_knit()
}
\value{
The path of the currently running script, NULL if it cannot be
determined.
}
\description{
\R does not store nor export the path of the currently running
script. This is an attempt to circumvent this limitation by applying
heuristics (such as call stack and argument inspection) that work in many
cases.
\strong{CAVEAT}: Use this function only if your workflow does not permit other
solution: if a script needs to know its location, it should be set outside
the context of the script if possible.
}
\details{
This functions currently work only if the script was \code{source}d,
processed with \code{knitr},
or run with \code{Rscript} or using the \code{--file} parameter to the
\code{R} executable. For code run with \code{Rscript}, the exact value
of the parameter passed to \code{Rscript} is returned.
}
\examples{
\dontrun{thisfile()}
}
\references{
\url{http://stackoverflow.com/q/1815606/946850}
}
\seealso{
\code{\link[base:source]{base::source()}}, \code{\link[utils:Rscript]{utils::Rscript()}}, \code{\link[base:getwd]{base::getwd()}}
}
\author{
Kirill Müller, Hadley Wickham, Michael R. Head
}
cat(rprojroot::thisfile(), "\n", sep="")
```{r echo=FALSE, results='markup', comment="", message=FALSE}
message(normalizePath(rprojroot::thisfile_knit()))
```
context("criterion")
test_that("root_criterion", {
expect_error(root_criterion(5, "Bogus"), "must have exactly one argument")
expect_error(
suppressWarnings(root_criterion(5, "Bogus")),
"must have exactly one argument"
)
expect_error(root_criterion(identity, "Bogus"), "must have exactly one argument")
expect_true(is.root_criterion(root_criterion(function(path) FALSE, "Never")))
})
......@@ -27,12 +30,10 @@ test_that("Formatting", {
})
test_that("Formatting criteria", {
ret <- character()
with_mock(
`base::cat` = function(..., sep = "") ret <<- c(ret, paste(..., sep = sep)),
str(criteria)
expect_output(
str(criteria),
"^List of "
)
expect_match(ret[[1]], "^List of ")
})
test_that("Combining criteria", {
......
......@@ -9,8 +9,8 @@ test_that("has_file", {
stop_path <- hierarchy(1L)
path <- hierarchy(4L)
with_mock(
`rprojroot:::is_root` = function(x) x == stop_path,
mockr::with_mock(
is_root = function(x) x == stop_path,
expect_equal(find_root("a", path = path), hierarchy(3L)),
expect_equal(find_root("b", path = path), hierarchy(3L)),
expect_equal(find_root("b/a", path = path), hierarchy(2L)),
......@@ -40,8 +40,8 @@ test_that("has_file_pattern", {
stop_path <- hierarchy(1L)
path <- hierarchy(4L)
with_mock(
`rprojroot:::is_root` = function(x) x == stop_path,
mockr::with_mock(
is_root = function(x) x == stop_path,
expect_equal(find_root(has_file_pattern(glob2rx("a")), path = path), hierarchy(3L)),
expect_equal(find_root(has_file_pattern(glob2rx("b")), path = path), hierarchy(3L)),
expect_equal(find_root(has_file_pattern("[ab]", "File b"), path = path),
......@@ -72,8 +72,8 @@ test_that("has_dir", {
stop_path <- hierarchy(1L)
path <- hierarchy(4L)
with_mock(
`rprojroot:::is_root` = function(x) x == stop_path,
mockr::with_mock(
is_root = function(x) x == stop_path,
expect_equal(find_root(has_dir("a"), path = path), hierarchy(1L)),
expect_equal(find_root(has_dir("b"), path = path), hierarchy(2L)),
expect_equal(find_root_file("c", criterion = has_dir("b"), path = path),
......@@ -96,8 +96,8 @@ test_that("has_dirname", {
stop_path <- hierarchy(1L)
path <- hierarchy(4L)
with_mock(
`rprojroot:::is_root` = function(x) x == stop_path,
mockr::with_mock(
is_root = function(x) x == stop_path,
expect_equal(find_root(has_dirname("a"), path = path), hierarchy(2L)),
expect_equal(find_root(has_dirname("b"), path = path), hierarchy(3L)),
expect_equal(find_root_file("c", criterion = has_dirname("b"), path = path),
......@@ -123,8 +123,8 @@ test_that("concrete criteria", {
stop_path <- hierarchy(0L)
path <- hierarchy(4L)
with_mock(
`rprojroot:::is_root` = function(x) x == stop_path,
mockr::with_mock(
is_root = function(x) x == stop_path,
expect_equal(find_root(is_rstudio_project, path = path), hierarchy(1L)),
expect_equal(find_root(is_remake_project, path = path), hierarchy(2L)),
expect_equal(find_root(is_projectile_project, path = path), hierarchy(3L)),
......@@ -144,8 +144,8 @@ test_that("is_svn_root", {
stop_path <- normalizePath(tempdir(), winslash = "/")
path <- hierarchy(