Skip to content

Commit 8ebe143

Browse files
committed
Set (and assert) filetype, handle optional dot in file extension & encapsulate/re-organise with helper functions plus extend tests
1 parent f57a04f commit 8ebe143

File tree

10 files changed

+62
-23
lines changed

10 files changed

+62
-23
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Collate:
5454
'rules-spacing.R'
5555
'serialize.R'
5656
'serialized_tests.R'
57+
'set-args.R'
5758
'style_guides.R'
5859
'styler.R'
5960
'token-create.R'

R/serialized_tests.R

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,6 @@ copy_to_tempdir <- function(path_perm = testthat_file()) {
206206
#' @name set_args
207207
NULL
208208

209-
#' @describeIn set_args Sets the argument `write_tree` in
210-
#' [test_collection()] to be `TRUE` for R versions higher or equal to 3.2, and
211-
#' `FALSE` otherwise since the second-level dependency `DiagrammeR` from
212-
#' `data.table` is not available for R < 3.2.
213-
set_arg_write_tree <- function(write_tree) {
214-
sufficient_version <- getRversion() >= 3.2
215-
if (is.na(write_tree)) {
216-
write_tree <- ifelse(sufficient_version, TRUE, FALSE)
217-
} else if (!sufficient_version & write_tree) {
218-
stop_insufficient_r_version()
219-
}
220-
write_tree
221-
}
222-
223209
stop_insufficient_r_version <- function() {
224210
stop(paste0(
225211
"Can't write tree with R version ", getRversion(),

R/set-args.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#' @describeIn set_args Sets the argument `write_tree` in
3+
#' [test_collection()] to be `TRUE` for R versions higher or equal to 3.2, and
4+
#' `FALSE` otherwise since the second-level dependency `DiagrammeR` from
5+
#' `data.table` is not available for R < 3.2.
6+
set_arg_write_tree <- function(write_tree) {
7+
sufficient_version <- getRversion() >= 3.2
8+
if (is.na(write_tree)) {
9+
write_tree <- ifelse(sufficient_version, TRUE, FALSE)
10+
} else if (!sufficient_version & write_tree) {
11+
stop_insufficient_r_version()
12+
}
13+
write_tree
14+
}
15+
16+
set_and_assert_filetype <- function(filetype) {
17+
without_dot <- gsub("^\\.", "", tolower(filetype))
18+
assert_filetype(without_dot)
19+
paste0("\\.", without_dot)
20+
}
21+
22+
assert_filetype <- function(lowercase_filetype) {
23+
if (!all(lowercase_filetype %in% c("r", "rmd"))) {
24+
stop(
25+
"filetype must not contain other values than 'R'",
26+
"or 'Rmd' (case is ignored).", call. = FALSE
27+
)
28+
}
29+
}

R/ui.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ NULL
1919
#' conveniently constructed via the `style` argument and `...`. See
2020
#' 'Examples'.
2121
#' @param filetype Vector of file extensions indicating which filetypes should
22-
#' be styled e.g. c("R", "Rmd")
22+
#' be styled. Case is ignored, and the `.` is optional, e.g. c(".R", ".Rmd")
23+
#' or c("r", "rmd").
2324
#' @param exclude_files Character vector with paths to files that should be
2425
#' excluded from styling.
2526
#' @section Warning:
@@ -123,12 +124,10 @@ style_dir <- function(path = ".",
123124
#' should be styled as well.
124125
prettify_any <- function(transformers, filetype, recursive, exclude_files) {
125126
files <- dir(
126-
path = ".",
127-
pattern = paste0("(", paste(filetype, collapse = "|"), ")$"),
127+
path = ".", pattern = map_filetype_to_pattern(filetype),
128128
ignore.case = TRUE, recursive = recursive, full.names = TRUE
129129
)
130130
transform_files(setdiff(files, exclude_files), transformers)
131-
132131
}
133132

134133
#' Style `.R` and/or `.Rmd` files

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ extend_if_comment <- function(pd, pos) {
8585
pos
8686
}
8787
}
88+
89+
map_filetype_to_pattern <- function(filetype) {
90+
paste0("(", paste(set_and_assert_filetype(filetype), collapse = "|"), ")$")
91+
}

man/prettify_any.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_args.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_dir.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_pkg.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-public_api.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,28 @@ test_that("messages (via cat()) of style_file are correct", {
9797

9898
context("public API - Rmd in style_dir()")
9999

100-
test_that("styler can style Rmd fie via style_dir()", {
100+
test_that("styler can style R and Rmd files via style_dir()", {
101101
msg <- capture_messages(
102102
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
103103
filetype = c("R", "Rmd"))
104104
)
105105
expect_true(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
106106
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
107107
})
108+
109+
test_that("styler can style Rmd files only via style_dir()", {
110+
msg <- capture_messages(
111+
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
112+
filetype = "Rmd")
113+
)
114+
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
115+
})
116+
117+
test_that("styler can style .r and .rmd files via style_dir()", {
118+
msg <- capture_messages(
119+
style_dir(testthat_file("public-api", "xyz-r-and-rmd-dir"),
120+
filetype = c(".r", ".rmd"))
121+
)
122+
expect_true(any(grepl("random-script-in-sub-dir.R", msg, fixed = TRUE)))
123+
expect_true(any(grepl("random-rmd-script.Rmd", msg, fixed = TRUE)))
124+
})

0 commit comments

Comments
 (0)