Skip to content

Commit

Permalink
resolves #25
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbarbone committed Jul 31, 2021
1 parent 231b36a commit d6fe692
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* adds more `fact()` methods
* `fact.integer()` [#30](https://github.com/jmbarbone/mark/issues/30)
* `fact.haven_labelled()` [#31](https://github.com/jmbarbone/mark/issues/31)
* `todos()` and `fixmes()` gain an additional argument `path` to specify a directory or file to search within [#25](https://github.com/jmbarbone/mark/issues/25)

## Non visible changes

Expand Down
45 changes: 35 additions & 10 deletions R/todo.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#'
#' @param pattern A character string containing a regular expression to filter
#' for comments after tags; default `NULL` does not filter
#' @param path The file directory to search for the tags
#' @param ... Additional parameters passed to `grep` (Except for `pattern`, `x`,
#' and `value`)
#'
Expand All @@ -16,26 +17,48 @@
#'
#' @export

todos <- function(pattern = NULL, ...) {
do_todo("todo", pattern = pattern, ...)
todos <- function(pattern = NULL, path = ".", ...) {
do_todo("todo", pattern = pattern, path = path, ...)
}

#' @rdname todos
#' @export
fixmes <- function(pattern = NULL, ...) {
do_todo("fixme", pattern = pattern, ...)
fixmes <- function(pattern = NULL, path = ".", ...) {
do_todo("fixme", pattern = pattern, path = path, ...)
}

do_todo <- function(text, pattern = NULL, ...) {
do_todo <- function(text, pattern = NULL, path = path, ...) {
# fs::dir_ls() would be a lot quicker but would be a new dependency

if (missing(path) || length(path) != 1 || !is.character(path)) {
stop("path must be a character vector of length 1L", call. = FALSE)
}

if (!file.exists(path)) {
stop("path not found: ", path, call. = FALSE)
}

if (length(text) != 1L) {
stop("Length of text must be 1", call. = FALSE)
}

files <- list.files(pattern = "\\.r(md)?$", recursive = TRUE, ignore.case = TRUE)
file_list <- lapply(files, readLines, warn = FALSE)
files <- if (is_dir(path)) {
list.files(
path,
pattern = "\\.r(md)?$",
recursive = TRUE,
ignore.case = TRUE,
full.names = TRUE
)
} else {
if (!grepl(path, pattern = "\\.r(md)?$", ignore.case = TRUE)) {
stop("path is not a .R or .Rmd file", .call = FALSE)
}
path
}

finds <- lapply(
file_list,
lapply(files, readLines, warn = FALSE),
function(x) {
ind <- grep(
pattern = sprintf("[#]\\s+%s[:]?\\s+", toupper(text)),
Expand All @@ -44,9 +67,10 @@ do_todo <- function(text, pattern = NULL, ...) {
quick_df(list(ind = ind, todo = x[ind]))
}
)

# names(finds) <- substr(files, path_n, vap_int(files, nchar))
names(finds) <- files
ind <- vap_lgl(finds, function(x) nrow(x) > 0)
finds <- finds[ind]
finds <- finds[vap_int(finds, nrow) > 0L]

if (identical(remove_names(finds), list())) {
message("No todos found")
Expand All @@ -71,6 +95,7 @@ do_todo <- function(text, pattern = NULL, ...) {

if (!is.null(pattern)) {
out <- out[grep(pattern, out[[text]], value = FALSE, ...), ]
attr(out, "row.names") <- seq_along(attr(out, "row.names"))
}

if (nrow(out) == 0L) {
Expand Down
6 changes: 4 additions & 2 deletions man/todos.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/todos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# todo() errors and messages

Code
todos(path = path)
Output
Found 2 TODO:
scripts/todos.R
[3] make x longer
[7] add another example

9 changes: 9 additions & 0 deletions tests/testthat/scripts/todos.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This is a file that doesn't do much

# TODO make x longer
x <- 1:10
length(x)

# TODO add another example

# FIXME This is a fixme
64 changes: 64 additions & 0 deletions tests/testthat/test-todos.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
test_that("todos() works", {
path <- test_path("scripts")
file <- test_path("scripts/todos.R")

res <- todos(path = path)
exp <- struct(
list(
line = c(3L, 7L),
file = rep(file, 2),
todo = c("make x longer", "add another example")
),
c("todos_df", "data.frame"),
todos_type = "todo",
row.names = 1:2,
.keep_attr = TRUE
)

expect_identical(res, exp)

res <- todos(pattern = "example", path = path)
exp <- struct(
list(
line = 7L,
file = file,
todo = "add another example"
),
c("todos_df", "data.frame"),
todos_type = "todo",
row.names = 1L,
.keep_attr = TRUE
)

expect_identical(res, exp)

res <- fixmes(path = path)
exp <- struct(
list(
line = 9L,
file = file,
fixme = "This is a fixme"
),
c("todos_df", "data.frame"),
todos_type = "fixme",
row.names = 1L,
.keep_attr = TRUE
)

expect_identical(res, exp)
})

test_that("todo() errors and messages", {
path <- test_path("scripts")

err <- "path must be a character vector of length 1L"
expect_error(todos(path = 1), err)
expect_error(todos(path = c("a", "b")), err)
expect_error(todos(path = "zzz"), "path not found: zzz")
expect_error(do_todo(c("todo", "fixme"), path = "."), "Length of text must be 1")

expect_message(res <- todos("zzzzzz", path = path), "No todos found")
expect_null(res)

expect_snapshot(todos(path = path))
})

0 comments on commit d6fe692

Please sign in to comment.