Skip to content

Commit 14aebe0

Browse files
Merge pull request #811 from lorenzwalthert/issue-806
- Allow to exclude sub-directories (#811).
2 parents a58a411 + 69c701e commit 14aebe0

File tree

7 files changed

+68
-33
lines changed

7 files changed

+68
-33
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
* `#>` is recognized as an output marker and no space is added after `#` (#771).
6262
* multi-expressions containing multiple assignments no longer remove line breaks
6363
if they are not causing blank lines (#809).
64+
* `exclude_dirs` in `style_pkg()` is now properly respected if it is a
65+
sub-directory of a directory that is scheduled for styling (e.g.
66+
`test/testthat/some/dir`) (#811).
6467
* R code chunks in nested non-R chunks in R markdown don't yield an error
6568
anymore when document is styled, chunks are still not styled (#788, #794).
6669
* `cache_activate()` and `cache_deactivate()` now respect the R

R/ui-styling.R

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,47 +110,42 @@ prettify_pkg <- function(transformers,
110110
include_roxygen_examples,
111111
base_indention,
112112
dry) {
113-
filetype <- set_and_assert_arg_filetype(filetype)
113+
filetype_ <- set_and_assert_arg_filetype(filetype)
114114
r_files <- rprofile_files <- vignette_files <- readme <- NULL
115-
exclude_files <- set_arg_paths(exclude_files)
116-
exclude_dirs <- set_arg_paths(exclude_dirs)
117-
without_excluded <- purrr::partial(setdiff, y = exclude_dirs)
118-
if ("\\.r" %in% filetype) {
115+
exclude_files <- c(
116+
set_arg_paths(exclude_files),
117+
dir_without_.(exclude_dirs, pattern = map_filetype_to_pattern(filetype))
118+
)
119+
if ("\\.r" %in% filetype_) {
119120
r_files <- dir_without_.(
120-
path = without_excluded(c("R", "tests", "data-raw", "demo")),
121-
pattern = "\\.r$",
122-
ignore.case = TRUE,
123-
recursive = TRUE
121+
path = c("R", "tests", "data-raw", "demo"),
122+
pattern = "\\.r$"
124123
)
125124
}
126125

127-
if ("\\.rprofile" %in% filetype) {
126+
if ("\\.rprofile" %in% filetype_) {
128127
rprofile_files <- dir_without_.(
129-
path = without_excluded("."), pattern = "^\\.rprofile$",
130-
ignore.case = TRUE, recursive = FALSE, all.files = TRUE
128+
path = ".", pattern = "^\\.rprofile$"
131129
)
132130
}
133-
if ("\\.rmd" %in% filetype) {
131+
if ("\\.rmd" %in% filetype_) {
134132
vignette_files <- dir_without_.(
135-
path = without_excluded("vignettes"), pattern = "\\.rmd$",
136-
ignore.case = TRUE, recursive = TRUE
133+
path = "vignettes", pattern = "\\.rmd$"
137134
)
138135
readme <- dir_without_.(
139136
path = ".",
140-
pattern = without_excluded("^readme\\.rmd$"), ignore.case = TRUE
137+
pattern = "^readme\\.rmd$"
141138
)
142139
}
143140

144-
if ("\\.rnw" %in% filetype) {
141+
if ("\\.rnw" %in% filetype_) {
145142
vignette_files <- append(
146143
vignette_files,
147144
dir_without_.(
148-
path = without_excluded("vignettes"), pattern = "\\.rnw$",
149-
ignore.case = TRUE, recursive = TRUE
145+
path = "vignettes", pattern = "\\.rnw$"
150146
)
151147
)
152148
}
153-
154149
files <- setdiff(
155150
c(r_files, rprofile_files, vignette_files, readme),
156151
exclude_files
@@ -277,8 +272,7 @@ prettify_any <- function(transformers,
277272
setdiff(c("", exclude_dirs)) %>%
278273
dir_without_.(
279274
pattern = map_filetype_to_pattern(filetype),
280-
ignore.case = TRUE, recursive = FALSE,
281-
all.files = TRUE
275+
recursive = FALSE
282276
)
283277
} else {
284278
files_other <- c()

R/utils-files.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,36 @@ map_filetype_to_pattern <- function(filetype) {
2727
paste0("(", paste(set_and_assert_arg_filetype(filetype), collapse = "|"), ")$")
2828
}
2929

30-
#' `dir()`, but without dot-prefix
30+
#' `dir()`, but without dot-prefix and different defaults
3131
#'
3232
#' When using `dir()`, you can set `full.names = FALSE`, but then you can only
3333
#' pass a character vector of length one as `path` to not loose the information
3434
#' about where the files are. This function solves that case. It's needed when
3535
#' one wants to standardize paths to use set operations on them, i.e. when the
3636
#' user supplied input does not have a dot prefix. See 'Examples'.
37+
#'
38+
#' For different defaults, see `dir_without_._one`.
3739
#' @param path A path.
3840
#' @param ... Passed to [base::dir()].
3941
#' @seealso set_and_assert_arg_paths
4042
#' @keywords internal
4143
#' @examples
4244
#' setdiff("./file.R", "file.R") # you want to standardize first.
43-
dir_without_. <- function(path, ...) {
44-
purrr::map(path, dir_without_._one, ...) %>%
45+
dir_without_. <- function(path, recursive = TRUE, ...) {
46+
purrr::map(path, dir_without_._one, recursive = recursive, ...) %>%
4547
unlist()
4648
}
4749

48-
dir_without_._one <- function(path, ...) {
50+
#' `dir()`, but with full names, ignored case, and included hidden files and
51+
#' recursive.
52+
#' @keywords internal
53+
dir_without_._one <- function(path, recursive, ...) {
4954
relative <- dir(
5055
path = path,
5156
full.names = FALSE,
57+
ignore.case = TRUE,
58+
recursive = recursive,
59+
all.files = TRUE,
5260
...
5361
)
5462
if (path == ".") {

man/combine_children.Rd

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

man/dir_without_..Rd

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

man/dir_without_._one.Rd

Lines changed: 14 additions & 0 deletions
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: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@ test_that("styler can style package", {
1010
})
1111

1212
test_that("styler can style package and exclude some directories", {
13-
capture_output(expect_true({
13+
capture_output(
1414
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
1515
exclude_dirs = "tests"
1616
)
17-
nrow(styled) == 1
18-
}))
17+
)
18+
expect_true(nrow(styled) == 1)
19+
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
20+
})
21+
22+
test_that("styler can style package and exclude some sub-directories", {
23+
capture_output(
24+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
25+
exclude_dirs = "tests/testthat"
26+
)
27+
)
28+
expect_true(nrow(styled) == 2)
29+
expect_true(any(grepl("tests/testthat.R", styled$file)))
30+
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
1931
})
2032

2133

34+
2235
test_that("styler can style package and exclude some directories and files", {
2336
capture_output(expect_true({
2437
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),

0 commit comments

Comments
 (0)